From: "Michael C. Libby" Date: 2002-12-05T22:18:45+09:00 Subject: Re: speed test -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 05 December 2002 06:19, Shannon Fang wrote: > 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 > 2. 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. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE971HL4ClW9KMwqnMRAhxZAKCS8UWG1vu/6LmDsHj1TlYuQLlCTgCgnNU5 z7bM8hz5WM0DgUOnz1fay0o= =cjWS -----END PGP SIGNATURE-----