bench 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +109 -0
- data/lib/bench.rb +56 -0
- metadata +54 -0
data/README
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
== bench
|
2
|
+
|
3
|
+
* http://bench.rubyforge.org
|
4
|
+
* http://rubyforge.org/projects/bench
|
5
|
+
|
6
|
+
== DESCRIPTION
|
7
|
+
|
8
|
+
Do you remeber how to use the benchmark library from the Ruby standard lib? I don't.
|
9
|
+
|
10
|
+
Now you need not to remember, there is Bench: A DSL around the benchmark lib of the Ruby
|
11
|
+
standard lib with the goal to make benchmarking as easy as possible.'
|
12
|
+
|
13
|
+
== SYNOPSIS
|
14
|
+
|
15
|
+
First an adapted example of the benchmark documentation from the pickaxe version 2 page 657
|
16
|
+
|
17
|
+
require 'bench'
|
18
|
+
|
19
|
+
string = 'Stormy Weather'
|
20
|
+
m = string.method(:length)
|
21
|
+
|
22
|
+
benchmark 'code' do
|
23
|
+
m.call
|
24
|
+
end
|
25
|
+
|
26
|
+
benchmark 'send' do
|
27
|
+
string.send(:length)
|
28
|
+
end
|
29
|
+
|
30
|
+
benchmark 'eval' do
|
31
|
+
eval "string.length"
|
32
|
+
end
|
33
|
+
|
34
|
+
run 10_000
|
35
|
+
|
36
|
+
You call the run method more than once for identifying rought values.
|
37
|
+
|
38
|
+
It's also nice to use Bench interactive with irb:
|
39
|
+
>> require 'bench'
|
40
|
+
=> true
|
41
|
+
>> benchmark 'simple' do
|
42
|
+
?> /ll/ =~ 'hello world'
|
43
|
+
>> end
|
44
|
+
=> [#<OpenStruct name="simple", proc=#<Proc:0xb7bf7acc@(irb):2>]
|
45
|
+
>> benchmark 'freezed' do
|
46
|
+
?> /ll/.freeze =~ 'hello world'
|
47
|
+
>> end
|
48
|
+
=> [#<OpenStruct name="simple", proc=#<Proc:0xb7bf7acc@(irb):2>, #<OpenStruct na
|
49
|
+
me="freezed", proc=#<Proc:0xb7bf26d0@(irb):5>]
|
50
|
+
>> run 1000
|
51
|
+
user system total real
|
52
|
+
simple 0.000000 0.000000 0.000000 ( 0.003960)
|
53
|
+
freezed 0.010000 0.000000 0.010000 ( 0.004870)
|
54
|
+
=> true
|
55
|
+
>> run 1000
|
56
|
+
user system total real
|
57
|
+
simple 0.010000 0.000000 0.010000 ( 0.003969)
|
58
|
+
freezed 0.000000 0.000000 0.000000 ( 0.004624)
|
59
|
+
=> true
|
60
|
+
>> # let's try more iterations
|
61
|
+
?> run 10000
|
62
|
+
user system total real
|
63
|
+
simple 0.060000 0.000000 0.060000 ( 0.058049)
|
64
|
+
freezed 0.060000 0.000000 0.060000 ( 0.058636)
|
65
|
+
=> true
|
66
|
+
>> run 100000
|
67
|
+
user system total real
|
68
|
+
simple 0.500000 0.000000 0.500000 ( 0.502427)
|
69
|
+
freezed 0.540000 0.000000 0.540000 ( 0.533421)
|
70
|
+
=> true
|
71
|
+
>> # now another benchmark sample
|
72
|
+
?> RE = /ll/
|
73
|
+
=> ll
|
74
|
+
>> benchmark 'constant' do
|
75
|
+
?> RE =~ 'hello world'
|
76
|
+
>> end
|
77
|
+
=> [#<OpenStruct name="simple", proc=#<Proc:0xb7bf7acc@(irb):2>, #<OpenStruct name="freezed", proc=#<Proc:0xb7bf26d0@(irb):5>, #<OpenStruct name="constant", proc=#<Proc:0xb7c26250@(irb):15>]
|
78
|
+
>> run
|
79
|
+
user system total real
|
80
|
+
simple 0.000000 0.000000 0.000000 ( 0.000031)
|
81
|
+
freezed 0.000000 0.000000 0.000000 ( 0.000469)
|
82
|
+
constant 0.000000 0.000000 0.000000 ( 0.000031)
|
83
|
+
=> true
|
84
|
+
>> run 100000
|
85
|
+
user system total real
|
86
|
+
simple 0.500000 0.000000 0.500000 ( 0.507686)
|
87
|
+
freezed 0.540000 0.000000 0.540000 ( 0.537840)
|
88
|
+
constant 0.550000 0.000000 0.550000 ( 0.552103)
|
89
|
+
=> true
|
90
|
+
>> run 100000
|
91
|
+
user system total real
|
92
|
+
simple 0.510000 0.000000 0.510000 ( 0.506002)
|
93
|
+
freezed 0.520000 0.000000 0.520000 ( 0.542898)
|
94
|
+
constant 0.520000 0.020000 0.540000 ( 0.552802)
|
95
|
+
=> true
|
96
|
+
>> run 100000
|
97
|
+
user system total real
|
98
|
+
simple 0.510000 0.000000 0.510000 ( 0.504704)
|
99
|
+
freezed 0.530000 0.000000 0.530000 ( 0.536948)
|
100
|
+
constant 0.560000 0.000000 0.560000 ( 0.554470)
|
101
|
+
=> true
|
102
|
+
|
103
|
+
== CREDITS
|
104
|
+
|
105
|
+
Copyright 2008 by Jan Friedrich (janfri.rubyforge@gmail.com)
|
106
|
+
|
107
|
+
== LICENSE
|
108
|
+
|
109
|
+
Ruby's license.
|
data/lib/bench.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module Bench
|
5
|
+
|
6
|
+
# Create a new benchmark sample <tt>name</tt>.
|
7
|
+
# Within the block you specify the code to execute.
|
8
|
+
def benchmark name, &block
|
9
|
+
bm = OpenStruct.new
|
10
|
+
bm.name = name
|
11
|
+
bm.proc = block
|
12
|
+
Bench.queue << bm
|
13
|
+
end
|
14
|
+
|
15
|
+
# Runs the benchmarks. Parameter <tt>count</tt> is the number of iterations
|
16
|
+
# each sample code should be repeated.
|
17
|
+
def run count=1
|
18
|
+
size = Bench.queue.inject(0) {|max, bm| size = bm.name.size; size > max ? size : max}
|
19
|
+
Benchmark.bm(size) do |x|
|
20
|
+
Bench.queue.each do |bm|
|
21
|
+
x.report(bm.name) { count.times {bm.proc.call} }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module_function :benchmark, :run
|
27
|
+
|
28
|
+
@queue = []
|
29
|
+
|
30
|
+
class << self
|
31
|
+
attr_reader :queue
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
include Bench
|
37
|
+
|
38
|
+
if __FILE__ == $0
|
39
|
+
# You can define benchmarks with the benchmark method
|
40
|
+
# first parameter is the name of the benchmark
|
41
|
+
# in the block you write the code to benchmark
|
42
|
+
benchmark 'fac' do
|
43
|
+
(1..100).inject(1) {|p,i| p*i}
|
44
|
+
end
|
45
|
+
time = 0.00001
|
46
|
+
benchmark 'sleep' do
|
47
|
+
sleep time
|
48
|
+
end
|
49
|
+
# You can run more than once to identify rogue results
|
50
|
+
run 100
|
51
|
+
run 100
|
52
|
+
# You can also change the environment
|
53
|
+
# Be aware! ;)
|
54
|
+
time *= 1000
|
55
|
+
run 100
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bench
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Friedrich
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-10 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: janfri.rubyforge@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- lib/bench.rb
|
26
|
+
- README
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://bench.rubyforge.org
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options:
|
31
|
+
- -m README
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: "0"
|
39
|
+
version:
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
requirements: []
|
47
|
+
|
48
|
+
rubyforge_project: http://rubyforge.org/projects/bench
|
49
|
+
rubygems_version: 1.1.0
|
50
|
+
signing_key:
|
51
|
+
specification_version: 2
|
52
|
+
summary: A wrapper DSL around the benchmark lib of the Ruby standard lib with the goal to make benchmarking as easy as possible.
|
53
|
+
test_files: []
|
54
|
+
|