Ruby Benchmark
rubybenchmark.com
rubybenchmark.com
Nitpick on their array tests, however:
measure "Array#delete_if" do
a = ARRAY.dup
a.delete_if {|o| o}
end
Ah, no. If o == false, that won't delete it. Also, you're calling object == true; how about a.delete_if {true}? "Array#delete_if{true}" is up to 30% faster over 10000 repetitions
------------------------------------------------------------------
Array#delete_if{true} 1.42472314834595 secs Fastest
Array#delete_if{|o| o} 2.03995013237 secs 30% Slower
Also:>gsubbing with string literals vs regex. For some reason regexp is faster. I suspect it converts string literals to a regexp anyway.
Really? You're surprised regex literals are faster than regex strings?
The gem however is interesting and nice and simple, I may end up using it for my future testing. Haven't looked through the code, however: anyone know if it's making any mistakes?
I would be interested in if anyone knows how the garbage collector handles a.drop vs a = []. Maybe a.drop collects earlier, and the gc is causing the slowdown?
I'm pretty ignorant about how the GC affects these benchmarks - I'd like to investigate that further in the future.
1. Valid GC during execution: you are removing an actual factor;
2. "Invalid" i.e. unrelated GC during execution: your benchmark is too narrow in scope.
Separate benchmarks should be conducted for execution speed (CPU time) and garbage generation. Saying "X is faster than Y" tends to suggest a CPU time benchmark. "X is faster than Y with W delta garbage and Z delta less time spent in GC" suggests a proper wall time benchmark.
Andy Stone has a benchmark file he's been maintaining that has some similarity to your project. Check it out if your looking for inspiration on additional tests: http://github.com/stonean/benchmark/blob/master/benchmarks.r...
That's the way echo -vs- print wars start...
Array#each_with_index 1.3768820762634277 secs 92% Slower
while 0.10703921318054199 secs Fastest
Don't you mean it's around 13x fast (or 1200% faster)?What we really want to measure is time quantities rather than speed quantities. I find comparisons of time much easier to grasp than comparisons of speed.
I've never played with ruby/rails before so I figured I'd better go and see what all the noise is all about but I had definitely not counted on so many installation issues.
I should probably clean this machine and do it all over again to be able to write up the exact steps to get it to work (from not having ruby before) but I'm too scared of having to go through another day of fiddling.
It looks like there is light at the end of the tunnel though.
also, ruby != rails ;)
When developing new Ruby stuff, one thing to watch for is interoperability with old gems that are not 1.9-ready. But the good news is, most gems work just fine under 1.9 -- at least in the non-Rails world. I think I recall only one or two gems I tried to use that weren't updated to work with 1.9 yet, out of many that I use or discovered.
That said, if you're willing to do that, Ruby 1.9 is quite nice and a bit more performant as well.
If you're having any gem issues make sure you're using ones from --pre since those will have likely have the changes required for the new rails. Bundler does a pretty good job at handling this for you though.
With that one could write a tool that suggests improvements to code as it tests it using this as a sort of crowd-sourced benchmark suite. It may not be 100% accurate, but at least it would give verified data instead of the educated-guess strategy most of us employ now.
Writing code based on "what is fastest" is an overall awful strategy.