4 ms·
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 tha
by Groxx 16y ago
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?
- rbxbx 16y agoYou should use http://github.com/sandro/bench_press http://github.com/sandro/bench_press and share your findings :)
- Groxx 16y agochanged the example and sent a pull request. Thanks for pointing that out :)
- chc 16y agoIf 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.
- Groxx 16y agoAaaah, that's a good point. That's quite a bit more rational.
- wzdd 16y agoI'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.
- rbxbx 16y agoIn 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.
- Groxx 16y agoNo, 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?
- cheald 16y agoGC should be disabled for proper benchmarks anyhow, honestly. Or at least measured. REE has handy switches to measure that sort of thing.
- sandrot 16y agoI 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.
- davidw 16y agoIt 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.