-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.rb
81 lines (73 loc) · 1.9 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'rubygems'
require 'fraction'
#pure version of fraction.gem
class Float
def ruby_fraction maxden = 10
return [self, 1.0, 0.0] unless self && self.kind_of?(Float)
m11 = m22 = 1
m12 = m21 = 0
x = self
while m21 * (ai = x.to_i) + m22 <= maxden
m12, m11 = [m11, (m11 * ai + m12).to_i]
m22, m21 = [m21, (m21 * ai + m22).to_i]
break if x == ai
break if (x - ai).abs < 0.000000000001
x = 1.0 / (x - ai)
end
return [m11, m21, self - m11.to_f/m21.to_f]
end
end
# Google for "ruby fraction" and you find this code
class Float
def number_decimal_places
self.to_s.length-2
end
def to_fraction
higher = 10**self.number_decimal_places
lower = self*higher
gcden = greatest_common_divisor(higher, lower)
return (lower/gcden).round, (higher/gcden).round
end
private
def greatest_common_divisor(a, b)
while a%b != 0
a,b = b.round,(a%b).round
end
return b
end
end
# make sure everything is up and running in cache with no startup delay
10_000.times {}
n,d=0.333.to_fraction
n,d=0.333.ruby_fraction
n,d=0.333.fraction
starttime = Time.now
1_000_000.times do
n,d=0.333.to_fraction
end
endtime = Time.now
feeling_lucky=endtime-starttime
puts "I'm Feeling Lucky: " + feeling_lucky.to_s + "s"
starttime = Time.now
1_000_000.times do
n,d=0.333.ruby_fraction
end
endtime = Time.now
fractionmod=endtime-starttime
puts "pure version of fraction: " + fractionmod.to_s + "s"
starttime = Time.now
1_000_000.times do
n,d=0.333.fraction
end
endtime = Time.now
fractionmod=endtime-starttime
puts "C version of fraction: " + fractionmod.to_s + "s"
starttime = Time.now
1_000_000.times do
n,d=[1,2]
end
endtime = Time.now
max_speed=endtime-starttime
n,d=(fractionmod-max_speed).fraction
puts "just running the loop requires #{max_speed.fraction[0]}/#{max_speed.fraction[1]} of a second,"
puts "so the algorithm itself requires only #{n}/#{d} of a second"