1

require 'timeout'

2

require 'bigdecimal'

3


4

$VERBOSE = nil

5

M = 5

6

TIMEOUT = 1.0

7


8

def measure(proc)

9

(1..M).map do

10

n = 0

11

Timeout.timeout(TIMEOUT) do

12

loop do

13

proc.()

14

n += 1

15

end

16

end rescue nil

17

n

18

end.max

19

end

20


21

r = Rational(1, 3)

22

b1, b2 = (1 << 64) + 1, (1 << 64) + 3

23

r2 = Rational(b1)

24

r3 = r

25

f, f2 = 10.0, 3.14

26

i = 10

27

bd = BigDecimal('0.1')

28

[

29

['Integer#gcd', >{b1.gcd(b2)}],

30

['+', >{r + r}],

31

['+ huge rational', >{r + r2}],

32

['+ float', >{r + f}],

33

['+ fixnum', >{r + i}],

34

['+ bignum', >{r + b1}],

35

['', >{r  r}],

36

[' huge rational', >{r  r2}],

37

[' float', >{r  f}],

38

[' fixnum', >{r  i}],

39

[' bignum', >{r  b1}],

40

['*', >{r * r}],

41

['* huge rational', >{r * r2}],

42

['* float', >{r * f}],

43

['* fixnum', >{r * i}],

44

['* bignum', >{r * b1}],

45

['/', >{r / r}],

46

['/ float', >{r / f}],

47

['/ huge rational', >{r / r2}],

48

['/ fixnum', >{r / i}],

49

['/ bignum', >{r / b1}],

50

['fdiv(0)', >{r.fdiv(0)}],

51

['fdiv(1)', >{r.fdiv(1)}],

52

['fdiv(2)', >{r.fdiv(2)}],

53

['** fixnum', >{r ** i}],

54

['** bignum', >{r ** b1}],

55

['** float', >{r ** f}],

56

['<=>', >{r <=> r}],

57

['<=> float', >{r <=> f}],

58

['<=> fixnum', >{r <=> i}],

59

['ctor with bignum', >{Rational(1, b1)}],

60

['ctor', >{Rational(1, 3)}],

61

['==', >{r == r2}],

62

['== bignum', >{r2 == b1}],

63

['coerce(float)', >{r.coerce(f)}],

64

['floor', >{r.floor}],

65

['ceil', >{r.ceil}],

66

['round', >{r.round}],

67

['to_i', >{r.to_i}],

68

['to_f', >{r.to_f}],

69

['floor(n)', >{r.floor(1)}],

70

['ceil(n)', >{r.ceil(1)}],

71

['round(n)', >{r.round(1)}],

72

['truncate(n)', >{r.truncate(1)}],

73

['Numeric#numerator', >{bd.numerator}],

74

['Numeric#denominator', >{bd.denominator}],

75

['Numeric#quo(integer)', >{2.quo(3)}],

76

['Float#numerator', >{f.numerator}],

77

['Float#denominator', >{f.denominator}],

78

['Float#to_r', >{f.to_r}],

79

['Float#to_r (0.1)', >{0.1.to_r}],

80

['1.0.rationalize', >{1.0.rationalize}],

81

['1.0.rationalize', >{1.0.rationalize}],

82

['10.0.rationalize(0.0)', >{f.rationalize(0.0)}],

83

['0.0.rationalize', >{0.0.rationalize}],

84

['10.0.rationalize', >{f.rationalize}],

85

['3.14.rationalize', >{f2.rationalize}],

86

['Rational(\'1\')', >{Rational('1')}],

87

['Rational(\'3.141592\')', >{Rational('3.141592')}],

88

['Rational(\'1e10\')', >{Rational('1e10')}],

89

['negative?', >{r.negative?}],

90

['rational.rationalize(some)', >{r3.rationalize(r)}],

91

['Integer#lcm', >{3.lcm(7)}],

92

['@', >{r}],

93

].each do label, proc

94

puts label + "\t%d" % measure(proc)

95

STDOUT.flush

96

end
