A handy list, I'll have to read through it more completely some time. There are some surprising speed differences. For instance, array.drop is hella-faster than array.clear, and array.shift(n) is faster yet (what?).
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'm not a Ruby programmer, so I'm interested to hear your view on this: as a non-Rubyist reading Ruby code, the "assign variable to a new array" method is much more readable to me than than the .shift(n) method and is only 2% slower. Is shift(length-of-array) an idiomatic way to clear an array in Ruby? From my perspective the site looks like a great way to encourage confusing code for little gain.
No, it's just a benchmark testing many ways to clear an array; there's nothing idiomatic about shift(n) that I'm aware of. Benchmarking code should always be taken with a grain of salt, it can get pretty needlessly esoteric at times in the pursuit of speed.
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?
Totally agree that benchmarks should be taken with a grain of salt. I was scanning the ruby docs for Array and saw shift(n) so I thought it'd be fun to try it.
I'm pretty ignorant about how the GC affects these benchmarks - I'd like to investigate that further in the future.
It depends on what you're benchmarking. If you're benching wall time, then yes, you need to have GC on, and you need to do a ridiculous number of iterations to average out GC runtimes. You also need to invoke the GC before you begin your benchmark; doing a collection pass early in the benchmark for all the objects created on startup/setup skews the benchmark.
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.
On the other hand, 10k+ iterations likely isn't anything you normally experience, and may incur abnormal GC cost due to the sheer number. Though that's typically detectable by doing multiple sizes of tests.
In some cases the gain is more dramatic, and in other cases, that 2% gain may be worth it. If you're simply learning a language I don't believe looking at benchmarks is a good way to do so. Typically the most performant options are hacky at best (in most all languages), imo.
If I'm reading right, he's not surprised regez literals are faster than regex strings. He's surprised that regex search-and-replace is faster than a literal string search-and-replace.
It does convert string literals to regexps, which is kind of disappointing. I once did some benchmarking with Tcl, and there, glob patterns were significantly faster than regexps, let along string literals.
I wouldn't be surprised if the code was making mistakes - it hasn't had much peer review. I think the percentage calculation may be wrong? We can always fix it if it's broken and resubmit the benchmarks.
It doesn't seem to throw out / properly mark insignificant results: http://rubybenchmark.com/reports/10 (is there any reason these 2 variants could get different bytecode results?)
This is always rather ambiguous terminology. For some people, it makes no sense for thing a to be more than 100% faster than thing b. Other people see it clearly meaning that thing a take half the time of thing b.
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.
Although it usually only matters when compared code scores similarly, it is important to be able to know when benchmark comparisons are statistically significant[1]. I wrote a library[2] a while back to do this. This becomes even more of a concern when the benchmarked code is running on a machine which is not well-isolated and dedicated to running the benchmark.
Funny, just finished installing rails 3 and the latest ruby. What a headache that was, really. In spite of following a tutorial I ran in to a whole series of showstoppers and only just now finally got it to work.
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.
If you're entirely new to the ecosystem, I would not be running Ruby 1.9.x or Rails 3. It may seem counter-productive to learn 'legacy' software, but these version will be around for a while I'm sure, you'll not hit nearly the snags you currently are, and you won't have /that/ much to learn when the later version are ready for you (and you them).
If you're quite familiar with Ruby/Rails, but you left the ecosystem for a while (before 1.9 existed), would it be better to re-acquaint yourself with 1.8/Rails 2 first, or just dive into the new stuff?
[Setting Rails aside, ]the Ruby community needs help to move forward into the world of 1.9. 1.9 has many improvements that most people would benefit from, but many folks are either afraid to get their feet wet with 1.9, or have to support legacy applications. If anyone would be writing anything new in Ruby, I encourage them to write it for 1.9 and to leave 1.8 behind. For the past year or so, only about 5% of my stuff has been written to support 1.8. The rest has been written for 1.9, usually with syntax or feature usage that will not work under 1.8.
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.
I think it's fine to go to Rails 3 at this point, so long as you're aware of the potential pain points (and as a older seasoned Rails dev, I'm sure you are). I would still probably shy away from Ruby 1.9.x unless you knew that all the tooling/libraries you would need were supported, or wanted to go through the upgrade process on other people's libraries/write your own.
That said, if you're willing to do that, Ruby 1.9 is quite nice and a bit more performant as well.
Rails 3RC works pretty well, but you should stick with latest Ruby 1.8.7 for now.
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.
That's a great idea. I'd really like to see a "raw" format option on the server that would give the code and metadata in a structured format.
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.
Cool, I'll keep that in mind. The site is intended to have user-generated content so if you find the exact thing you'd like to benchmark, use the bench_press gem to test locally, then publish it to share your findings.
I am, uh, highly sceptical of the value of this. That is, even ignoring the variance between runs of the same code, differences between implementations and within an implementation with slightly different objects.
Writing code based on "what is fastest" is an overall awful strategy.
Nitpick on their array tests, however:
Ah, no. If o == false, that won't delete it. Also, you're calling object == true; how about a.delete_if {true}? 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?