From: Jim Freeze Date: 2005-05-11T21:11:53+09:00 Subject: Re: ruby vs. java? * Jim Freeze [2005-05-11 20:59:42 +0900]: Let me point out one other thing. If you start with f = 1.0 instead of f = 1 that is, floating pt math instead of integer math, you get: % ruby fac 7500 0.03094 0.038541 0.068006 % ruby fac 17500 0.066106 0.075447 0.128814 % ruby fac 27500 0.10868 0.113806 0.198435 > % ruby fac 7500 > 0.498884 > 0.337408 > 0.283017 # this is faster than #2 > % ruby fac 17500 > 2.375539 > 2.075047 > 2.111601 # looks like a GC happened. Used to be faster. > % ruby fac 27500 > 5.997851 > 5.749802 > 5.797634 > > % cat fac > def fac1( n ) > f = 1 > (1..n).each { |x| f *= x } > return f > end > > def fac2( n ) > f = 1 > (1..n).each { |x| f *= x } > end > > def fac3( n ) > (1..n).inject(1) { |f,x| f * x } > end > > start = Time.now > fac1( ARGV[0].to_i ) > puts Time.now - start > > start = Time.now > fac2( ARGV[0].to_i ) > puts Time.now - start > > start = Time.now > fac3( ARGV[0].to_i ) > puts Time.now - start > > -- > Jim Freeze -- Jim Freeze Ruby: I can explain it to ya but I can't understand it fer ya.