# Bench 1.0.0 released

**URL:** <https://www.ruby-forum.com/t/bench-1-0-0-released/140101>\
**Category:** Ruby\
**Created:** [June 10, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101 "2008-06-10T21:35:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 10, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/1 "2008-06-10T21:35:38Z")

</div>

Bench version 1.0.0 released!  
[http://bench.rubyforge.org](http://bench.rubyforge.org)

== DESCRIPTION

Do you remeber how to use the benchmark library from the Ruby standard  
lib? I don’t.

Now you need not to remember, there is Bench: A DSL around the benchmark  
lib of the Ruby  
standard lib with the goal to make benchmarking as easy as possible.

== SYNOPSIS

Adapted example of the benchmark documentation from the pickaxe version  
2  
page 657

require ‘bench’

string = ‘Stormy Weather’  
m = string.method(:length)

benchmark ‘code’ do  
m.call  
end

benchmark ‘send’ do  
string.send(:length)  
end

benchmark ‘eval’ do  
eval “string.length”  
end

run 10\_000

== CREDITS

Copyright 2008 by Jan F. (removed\_email\_address@domain.invalid)

== LICENSE

Ruby’s license.

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 10, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/2 "2008-06-10T23:12:37Z")

</div>

On Jun 10, 2008, at 12:34 , Jan F. wrote:

> Do you remeber how to use the benchmark library from the Ruby standard  
> lib? I don’t.
> 
> Now you need not to remember, there is Bench: A DSL around the  
> benchmark  
> lib of the Ruby standard lib with the goal to make benchmarking as  
> easy as possible.

If I don’t remember how to use the benchmark library, why would I  
remember how to use your DSL?

That is what ri is for. `ri Benchmark` obviates the need for Bench.

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 11, 2008, 2:50am UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/3 "2008-06-11T02:50:43Z")

</div>

On Jun 10, 3:11 pm, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I don’t remember how to use the benchmark library, why would I  
> remember how to use your DSL?
> 
> That is what ri is for. `ri Benchmark` obviates the need for Bench.

Agreed. However, I would like a nicer interface:

> **[BenchUnit?](https://djberg96.livejournal.com/52734.html)**
>
> I'm in the process of writing a hefty benchmark suite for Ruby. I'm doing this for several reasons - so we have a baseline to compare minor (or major) releases against each other, heavy iteration testing, checks for pathological slowdowns, and as a...

I’ve submitted “bench-unit” as a RubyForge project.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 11, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/4 "2008-06-11T20:11:34Z")

</div>

On Wed, Jun 11, 2008 at 5:04 AM, Jan F.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ryan D. wrote:
> 
> > If I don’t remember how to use the benchmark library, why would I  
> > remember how to use your DSL?  
> > Because it has only 2 commands: benchmark and run. 😉

Agree 🙂

Good job!

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 11, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/5 "2008-06-11T22:53:02Z")

</div>

On Jun 10, 3:34 pm, Jan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> benchmark ‘code’ do
> 
> run 10\_000

What is the underlying translation of benchmark? I.e. the original  
Benchmark library has a few different methods, which did you use? I  
like the simplicity of your DSL. In the long run it might be nice to  
see this advance beyond a dependency on the original benchmark  
library.

T.

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 11, 2008, 10:06am UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/6 "2008-06-11T10:06:58Z")

</div>

Ryan D. wrote:

> If I don’t remember how to use the benchmark library, why would I  
> remember how to use your DSL?  
> Because it has only 2 commands: benchmark and run. 😉

> That is what ri is for. `ri Benchmark` obviates the need for Bench.  
> Maybe, but with bench it’s a pleasure to doing benchmarks interactive  
> with irb:

> > require ‘bench’

> > benchmark ‘simple’ do  
> > ?\> /ll/ =~ ‘hello world’
> 
> > end

> > benchmark ‘freezed’ do  
> > ?\> /ll/.freeze =~ ‘hello world’
> 
> > end

> > run 1000  
> > user system total real  
> > simple 0.000000 0.000000 0.000000 ( 0.003960)  
> > freezed 0.010000 0.000000 0.010000 ( 0.004870)

> > run 1000  
> > user system total real  
> > simple 0.010000 0.000000 0.010000 ( 0.003969)  
> > freezed 0.000000 0.000000 0.000000 ( 0.004624)

> > # let’s try more iterations

?\> run 10000  
user system total real  
simple 0.060000 0.000000 0.060000 ( 0.058049)  
freezed 0.060000 0.000000 0.060000 ( 0.058636)

> > run 100000  
> > user system total real  
> > simple 0.500000 0.000000 0.500000 ( 0.502427)  
> > freezed 0.540000 0.000000 0.540000 ( 0.533421)

> > # now another benchmark sample

?\> RE = /ll/

> > benchmark ‘constant’ do  
> > ?\> RE =~ ‘hello world’
> 
> > end

> > run  
> > user system total real  
> > simple 0.000000 0.000000 0.000000 ( 0.000031)  
> > freezed 0.000000 0.000000 0.000000 ( 0.000469)  
> > constant 0.000000 0.000000 0.000000 ( 0.000031)

But It’s certainly a question of taste. 🙂

Regards,  
Jan F.

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 12, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/7 "2008-06-12T22:10:30Z")

</div>

Jan F. wrote:

> ```
> x.report(bm.name) { count.times {bm.proc.call} }
> 
> ```

Suggestion:

```
       x.report(bm.name) { count.times(&bm.proc) }

```

This will run faster, and more importantly less of the reported time  
will be due to framework overhead.

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 12, 2008, 10:47am UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/8 "2008-06-12T10:47:48Z")

</div>

Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the underlying translation of benchmark? I.e. the original  
> Benchmark library has a few different methods, which did you use? I like  
> the simplicity of your DSL. In the long run it might be nice to see this  
> advance beyond a dependency on the original benchmark library.  
> Let’s look at the code:

def run count=1  
size = Bench.queue.inject(0) {|max, bm| size = bm.name.size; size \>  
max ? size : max}  
Benchmark.bm(size) do |x|  
Bench.queue.each do |bm|  
x.report(bm.name) { count.times {bm.proc.call} }  
end  
end  
end

Two methods of the original benchmark are called:

- Benchmark.bm with an automatic calculated label\_width (I don’t want to  
think about that).
- Benchmark::Report#report

You can call run as often as you want and do GC.start or whatever  
before,  
so you can simulate the Benchmark.bmbm method. That is enough for my  
needs.

Maybe there is a better approach? Feel free to contribute:  
[http://gitorious.org/projects/bench](http://gitorious.org/projects/bench) 🙂

Regards  
Jan F.

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [June 13, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/bench-1-0-0-released/140101/9 "2008-06-13T12:16:46Z")

</div>

Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> x.report(bm.name) { count.times(&bm.proc) }
> 
> ```
> 
> This will run faster, and more importantly less of the reported time  
> will be due to framework overhead.  
> Thanks a lot. 🙂

Regards  
Jan F.
