Hi,
I did some simple speed test (See below), found that
- a=“something #{aString}” is faster than a="somthing "+aString
- 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