Speed test

Hi,

I did some simple speed test (See below), found that

  1. a=“something #{aString}” is faster than a="somthing "+aString
  2. for … in is faster than until…end?

I’m not sure about the result especially the 2nd one. How can we make the
loop faster? I found that for … in is displayed as #each in the
profiler…

btw, the last question, I lost the post about Ruby++, because when I tried
i++ it doesn’t work, I need to use i+=1. I remembered there is a post by
some senior Rubyist here, can anyone give me the # of that post in rubytalk?
I can’t find it in www.rubytalk.org.

Thanks!
Shannon

– code below –

b="hello"
for i in 1…10000
a="test #{b}"
end

% cumulative self self total
time seconds seconds calls ms/call ms/call name
100.00 0.38 0.38 1 375.00 375.00 Range#each
0.00 0.38 0.00 1 0.00 375.00 #toplevel

b="hello"
for i in 1…10000
a="test "+b
end

% cumulative self self total
time seconds seconds calls ms/call ms/call name
71.82 0.83 0.83 1 831.00 1157.00 Range#each
28.18 1.16 0.33 10000 0.03 0.03 String#+
0.00 1.16 0.00 1 0.00 1157.00 #toplevel

b="hello"
i=1
until i>10000
a="test #{b}"
i+=1
end

% cumulative self self total
time seconds seconds calls ms/call ms/call name
20.36 0.44 0.44 10001 0.04 0.04 Fixnum#>
11.64 0.69 0.25 10000 0.03 0.03 Fixnum#+
0.00 0.69 0.00 1 0.00 2156.00 #toplevel

···

MSN 8 with e-mail virus protection service: 2 months FREE*
http://join.msn.com/?page=features/virus

Hi,

I did some simple speed test (See below), found that

  1. a=“something #{aString}” is faster than a="somthing "+aString

I answer benchmarking concerns below

  1. for … in is faster than until…end?

Neither of these is best for simple loop from 0 to some number. For that
use FixNum#times (examples in code).

As to your method for comparing algorithms, consider using the Benchmark
module instead of profiling the code like that. Get whatever you want to
compare into the tightest code possible, then check out the differences.
The profiling is better used when you have a script and you want to find
the areas which are bottlenecks (then you then pick apart the algorithms
and benchmark the alternatives).

Here is code I would use to compare interpolation and append with +.

require ‘benchmark’
include Benchmark

class StringTest

def StringTest.append(phrase)
  return "Hello" + phrase
end

def StringTest.interpolate(phrase)
  return "Hello #{phrase}"
end

end

[10,100,1000,10000,100000,1000000].each do |this_many|

puts "Running benchmark for #{this_many} times"

bm(12) do |test|
  test.report( 'Append with + ') do
    this_many.times { x = StringTest.append(", World!") }
  end
  test.report( 'String interp ') do
    this_many.times { x = StringTest.interpolate(", World!") }
  end
end

puts "-----"

end

And below is the output on my system.

ichimunki@greyhound:~/ruby-misc$ ruby bench_inter_append.rb
Running benchmark for 10 times
user system total real
Append with + 0.000000 0.000000 0.000000 ( 0.000065)
String interp 0.000000 0.000000 0.000000 ( 0.000038)


Running benchmark for 100 times
user system total real
Append with + 0.000000 0.000000 0.000000 ( 0.000294)
String interp 0.000000 0.010000 0.010000 ( 0.000292)


Running benchmark for 1000 times
user system total real
Append with + 0.000000 0.000000 0.000000 ( 0.004784)
String interp 0.000000 0.000000 0.000000 ( 0.002639)


Running benchmark for 10000 times
user system total real
Append with + 0.040000 0.000000 0.040000 ( 0.040618)
String interp 0.040000 0.000000 0.040000 ( 0.066886)


Running benchmark for 100000 times
user system total real
Append with + 0.400000 0.000000 0.400000 ( 0.446464)
String interp 0.360000 0.000000 0.360000 ( 0.362138)


Running benchmark for 1000000 times
user system total real
Append with + 3.950000 0.000000 3.950000 ( 3.944867)
String interp 3.620000 0.000000 3.620000 ( 3.701003)


It is also useful when comparing algorithms like this to vary the
algorithms, both in iterations performed and trivial details. In this
case, the difference is even more distinct if phrase is a FixNum and one
has to do FixNum#to_s during the string append process (interpolation
handles this for you).

One or two trivial examples of string handling might lead you to forswear
the + method and always use interpolation, but there are usually
trade-offs (one of which is readability).

In this case, I’d continue using whatever felt right, regardless of the
speed. Ruby’s strength is not in its super-speediness, but in it’s
flexibility, ease of use, and readability.

···

On Thursday 05 December 2002 06:19, Shannon Fang wrote: