* Franz Hartmann <porschefranz / hotmail.com> [2005-05-11 20:43:45 +0900]:

> 1800 mips and it needs 14 min 43 secfor the calculation of 100000 
> multiplications.
> 14 x 60 + 43 sec = 883 sec
> 100000 operations / 883 sec = 113 flops???
> c64 basic calculated 10000 sqrts within less than 20 minutes, that amount 
> to 10000 / 1200 = 8 flops. at 0.9 mhz, that is 1/1000.
> (8 / 113) / (0.9 / 900) = 70.8
> Does this mean that ruby is 70 (seventy) times slower than c64 basic, not 
> counting the advantages of 32 vs 8 bit????
> Or did i just make some very stupid error? Here's the program:
> 
> 
> def fac( n )
>  f = 1
>  (1..n).each { |x| f *= x }
>  return f
> end
> 
> print "%d\n" % fac( ARGV[0].to_i )

I can't answer your question exactly, but there are faster
ways to write the code. (BTW, did c64 basic support
arbitrarily large numbers and their math?)

And one other thing, I removed printing from the timing since
it can take some time to print out such a large number.

  % 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