News for hackers. Stuff that matters.
Re:Ruby Benchmark by Groxx on Sunday August 01, 2010 @03:58PM (#1565953)
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?

Re:Ruby Benchmark by wzdd on Sunday August 01, 2010 @05:03PM (#1566099)
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.
Re:Ruby Benchmark by Groxx on Sunday August 01, 2010 @06:51PM (#1566294)
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?

Re:Ruby Benchmark by sandrot on Monday August 02, 2010 @01:43AM (#1566937)
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.

Re:Ruby Benchmark by cheald on Sunday August 01, 2010 @08:54PM (#1566497)
GC should be disabled for proper benchmarks anyhow, honestly. Or at least measured. REE has handy switches to measure that sort of thing.
Re:Ruby Benchmark by rue on Monday August 02, 2010 @09:23AM (#1567557)
Disabling the GC means your benchmark is worthless.

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.

Re:Ruby Benchmark by cheald on Monday August 02, 2010 @06:03PM (#1569260)
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.

Re:Ruby Benchmark by Groxx on Monday August 02, 2010 @10:01AM (#1567657)
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.
Re:Ruby Benchmark by rbxbx on Sunday August 01, 2010 @05:24PM (#1566132)
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.
Re:Ruby Benchmark by chc on Sunday August 01, 2010 @04:37PM (#1566044)
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.
Re:Ruby Benchmark by Groxx on Sunday August 01, 2010 @06:57PM (#1566307)
Aaaah, that's a good point. That's quite a bit more rational.
Re:Ruby Benchmark by rbxbx on Sunday August 01, 2010 @04:05PM (#1565973)
You should use http://github.com/sandro/bench_press and share your findings :)
Re:Ruby Benchmark by Groxx on Sunday August 01, 2010 @04:12PM (#1565994)
changed the example and sent a pull request. Thanks for pointing that out :)
Re:Ruby Benchmark by davidw on Monday August 02, 2010 @06:57AM (#1567336)
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.
Re:Ruby Benchmark by sandrot on Monday August 02, 2010 @02:00AM (#1566971)
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.