rubysl-benchmark 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1b82b3855dd96f263431f587c6c361fb16f4dfa
4
- data.tar.gz: c6e38012efa3927a68c5b97ef3f2b311eba94fde
3
+ metadata.gz: 6d61b47569977aa80fafad0deee1ec0e6860834f
4
+ data.tar.gz: ba701e8eae7045941ea54a87d306bb6cbd491d98
5
5
  SHA512:
6
- metadata.gz: 9b264eb055effe19d150da7d4c167c1e076d3c7b806ec8629aab6757c5c09b3cc3dad9be71fe2016659b1b15eceb4eded8578419d7fb4f312c4896253ea5edef
7
- data.tar.gz: f014782da4a3a6ae5955c4e3aaddcf0f4d02cdd4a95168dd81b150da4a9bdf034a5c0109712000673e814e90cfecd323151009f30abde117fb1129f04f501ca2
6
+ metadata.gz: 8f746a698b9a5422c45c4d0ea935dd6ba1963370f53f1755f2f3775b5bd2edd6653ddb0af552edfa4e0816fb08e8e029dc37fa9c2b2314c6455b1961c120b80f
7
+ data.tar.gz: 7b9847e18efb173c4e38ccd2ee84ea00f4c469473822a250ec99c02d250d9b924e2ebc2781c6631e4f51f84c5d166bc09848fe79498a0005823f91018d2c6c7c
@@ -1,7 +1,14 @@
1
1
  language: ruby
2
2
  env:
3
3
  - RUBYLIB=lib
4
- script: bundle exec mspec
4
+ - RUBYLIB=
5
+ script: mspec spec
5
6
  rvm:
6
- - 1.9.3
7
- - rbx-nightly-19mode
7
+ - 2.0.0
8
+ - rbx-2.1.1
9
+ matrix:
10
+ exclude:
11
+ - rvm: 2.0.0
12
+ env: RUBYLIB=lib
13
+ - rvm: rbx-2.1.1
14
+ env: RUBYLIB=
@@ -1,16 +1,14 @@
1
- =begin
2
- #
1
+ #--
3
2
  # benchmark.rb - a performance benchmarking library
4
3
  #
5
- # $Id: benchmark.rb 11708 2007-02-12 23:01:19Z shyouhei $
4
+ # $Id: benchmark.rb 43002 2013-09-20 16:05:48Z zzak $
6
5
  #
7
6
  # Created by Gotoken (gotoken@notwork.org).
8
7
  #
9
8
  # Documentation by Gotoken (original RD), Lyle Johnson (RDoc conversion), and
10
9
  # Gavin Sinclair (editing).
10
+ #++
11
11
  #
12
- =end
13
-
14
12
  # == Overview
15
13
  #
16
14
  # The Benchmark module provides methods for benchmarking Ruby code, giving
@@ -21,15 +19,15 @@
21
19
  # used to execute Ruby code.
22
20
  #
23
21
  # * Measure the time to construct the string given by the expression
24
- # <tt>"a"*1_000_000</tt>:
22
+ # <code>"a"*1_000_000_000</code>:
25
23
  #
26
24
  # require 'benchmark'
27
25
  #
28
- # puts Benchmark.measure { "a"*1_000_000 }
26
+ # puts Benchmark.measure { "a"*1_000_000_000 }
29
27
  #
30
- # On my machine (FreeBSD 3.2 on P5, 100MHz) this generates:
28
+ # On my machine (OSX 10.8.3 on i5 1.7 Ghz) this generates:
31
29
  #
32
- # 1.166667 0.050000 1.216667 ( 0.571355)
30
+ # 0.350000 0.400000 0.750000 ( 0.835234)
33
31
  #
34
32
  # This report shows the user CPU time, system CPU time, the sum of
35
33
  # the user and system CPU times, and the elapsed real time. The unit
@@ -39,7 +37,7 @@
39
37
  #
40
38
  # require 'benchmark'
41
39
  #
42
- # n = 50000
40
+ # n = 5000000
43
41
  # Benchmark.bm do |x|
44
42
  # x.report { for i in 1..n; a = "1"; end }
45
43
  # x.report { n.times do ; a = "1"; end }
@@ -49,15 +47,15 @@
49
47
  # The result:
50
48
  #
51
49
  # user system total real
52
- # 1.033333 0.016667 1.016667 ( 0.492106)
53
- # 1.483333 0.000000 1.483333 ( 0.694605)
54
- # 1.516667 0.000000 1.516667 ( 0.711077)
50
+ # 1.010000 0.000000 1.010000 ( 1.014479)
51
+ # 1.000000 0.000000 1.000000 ( 0.998261)
52
+ # 0.980000 0.000000 0.980000 ( 0.981335)
55
53
  #
56
54
  # * Continuing the previous example, put a label in each report:
57
55
  #
58
56
  # require 'benchmark'
59
57
  #
60
- # n = 50000
58
+ # n = 5000000
61
59
  # Benchmark.bm(7) do |x|
62
60
  # x.report("for:") { for i in 1..n; a = "1"; end }
63
61
  # x.report("times:") { n.times do ; a = "1"; end }
@@ -67,10 +65,9 @@
67
65
  # The result:
68
66
  #
69
67
  # user system total real
70
- # for: 1.050000 0.000000 1.050000 ( 0.503462)
71
- # times: 1.533333 0.016667 1.550000 ( 0.735473)
72
- # upto: 1.500000 0.016667 1.516667 ( 0.711239)
73
- #
68
+ # for: 1.010000 0.000000 1.010000 ( 1.015688)
69
+ # times: 1.000000 0.000000 1.000000 ( 1.003611)
70
+ # upto: 1.030000 0.000000 1.030000 ( 1.028098)
74
71
  #
75
72
  # * The times for some benchmarks depend on the order in which items
76
73
  # are run. These differences are due to the cost of memory
@@ -90,22 +87,22 @@
90
87
  # The result:
91
88
  #
92
89
  # Rehearsal -----------------------------------------
93
- # sort! 11.928000 0.010000 11.938000 ( 12.756000)
94
- # sort 13.048000 0.020000 13.068000 ( 13.857000)
95
- # ------------------------------- total: 25.006000sec
90
+ # sort! 1.490000 0.010000 1.500000 ( 1.490520)
91
+ # sort 1.460000 0.000000 1.460000 ( 1.463025)
92
+ # -------------------------------- total: 2.960000sec
96
93
  #
97
94
  # user system total real
98
- # sort! 12.959000 0.010000 12.969000 ( 13.793000)
99
- # sort 12.007000 0.000000 12.007000 ( 12.791000)
100
- #
95
+ # sort! 1.460000 0.000000 1.460000 ( 1.460465)
96
+ # sort 1.450000 0.010000 1.460000 ( 1.448327)
101
97
  #
102
98
  # * Report statistics of sequential experiments with unique labels,
103
99
  # using the #benchmark method:
104
100
  #
105
101
  # require 'benchmark'
102
+ # include Benchmark # we need the CAPTION and FORMAT constants
106
103
  #
107
- # n = 50000
108
- # Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
104
+ # n = 5000000
105
+ # Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
109
106
  # tf = x.report("for:") { for i in 1..n; a = "1"; end }
110
107
  # tt = x.report("times:") { n.times do ; a = "1"; end }
111
108
  # tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
@@ -115,29 +112,26 @@
115
112
  # The result:
116
113
  #
117
114
  # user system total real
118
- # for: 1.016667 0.016667 1.033333 ( 0.485749)
119
- # times: 1.450000 0.016667 1.466667 ( 0.681367)
120
- # upto: 1.533333 0.000000 1.533333 ( 0.722166)
121
- # >total: 4.000000 0.033333 4.033333 ( 1.889282)
122
- # >avg: 1.333333 0.011111 1.344444 ( 0.629761)
115
+ # for: 0.950000 0.000000 0.950000 ( 0.952039)
116
+ # times: 0.980000 0.000000 0.980000 ( 0.984938)
117
+ # upto: 0.950000 0.000000 0.950000 ( 0.946787)
118
+ # >total: 2.880000 0.000000 2.880000 ( 2.883764)
119
+ # >avg: 0.960000 0.000000 0.960000 ( 0.961255)
123
120
 
124
121
  module Benchmark
125
122
 
126
123
  BENCHMARK_VERSION = "2002-04-25" # :nodoc:
127
124
 
128
- def Benchmark.times # :nodoc:
129
- Process.times
130
- end
131
-
132
-
133
- # Invokes the block with a <tt>Benchmark::Report</tt> object, which
125
+ # Invokes the block with a Benchmark::Report object, which
134
126
  # may be used to collect and report on the results of individual
135
- # benchmark tests. Reserves <i>label_width</i> leading spaces for
136
- # labels on each line. Prints _caption_ at the top of the
137
- # report, and uses _fmt_ to format each line.
127
+ # benchmark tests. Reserves +label_width+ leading spaces for
128
+ # labels on each line. Prints +caption+ at the top of the
129
+ # report, and uses +format+ to format each line.
130
+ # Returns an array of Benchmark::Tms objects.
131
+ #
138
132
  # If the block returns an array of
139
- # <tt>Benchmark::Tms</tt> objects, these will be used to format
140
- # additional lines of output. If _label_ parameters are
133
+ # Benchmark::Tms objects, these will be used to format
134
+ # additional lines of output. If +label+ parameters are
141
135
  # given, these are used to label these extra lines.
142
136
  #
143
137
  # _Note_: Other methods provide a simpler interface to this one, and are
@@ -147,72 +141,67 @@ module Benchmark
147
141
  # Example:
148
142
  #
149
143
  # require 'benchmark'
150
- # include Benchmark # we need the CAPTION and FMTSTR constants
144
+ # include Benchmark # we need the CAPTION and FORMAT constants
151
145
  #
152
- # n = 50000
153
- # Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
146
+ # n = 5000000
147
+ # Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x|
154
148
  # tf = x.report("for:") { for i in 1..n; a = "1"; end }
155
149
  # tt = x.report("times:") { n.times do ; a = "1"; end }
156
150
  # tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
157
151
  # [tf+tt+tu, (tf+tt+tu)/3]
158
152
  # end
159
153
  #
160
- # <i>Generates:</i>
154
+ # Generates:
161
155
  #
162
156
  # user system total real
163
- # for: 1.016667 0.016667 1.033333 ( 0.485749)
164
- # times: 1.450000 0.016667 1.466667 ( 0.681367)
165
- # upto: 1.533333 0.000000 1.533333 ( 0.722166)
166
- # >total: 4.000000 0.033333 4.033333 ( 1.889282)
167
- # >avg: 1.333333 0.011111 1.344444 ( 0.629761)
157
+ # for: 0.970000 0.000000 0.970000 ( 0.970493)
158
+ # times: 0.990000 0.000000 0.990000 ( 0.989542)
159
+ # upto: 0.970000 0.000000 0.970000 ( 0.972854)
160
+ # >total: 2.930000 0.000000 2.930000 ( 2.932889)
161
+ # >avg: 0.976667 0.000000 0.976667 ( 0.977630)
168
162
  #
169
163
 
170
- def benchmark(caption="", label_width=nil, fmtstr=nil, *labels) # :yield: report
171
- raise ArgumentError, "no block" unless block_given?
172
-
173
- # swap
174
- sync, STDOUT.sync = STDOUT.sync, true
175
-
164
+ def benchmark(caption = "", label_width = nil, format = nil, *labels) # :yield: report
165
+ sync = STDOUT.sync
166
+ STDOUT.sync = true
176
167
  label_width ||= 0
177
- fmtstr ||= FMTSTR
178
-
179
- print caption
180
- results = yield(Report.new(label_width, fmtstr))
181
-
182
- if Array === results
183
- results.grep(Tms).each do |t|
184
- lbl = labels.shift || t.label || ""
185
- print lbl.ljust(label_width), t.format(fmtstr)
186
- end
187
- end
188
-
189
- STDOUT.sync = sync
190
- results
168
+ label_width += 1
169
+ format ||= FORMAT
170
+ print ' '*label_width + caption unless caption.empty?
171
+ report = Report.new(label_width, format)
172
+ results = yield(report)
173
+ Array === results and results.grep(Tms).each {|t|
174
+ print((labels.shift || t.label || "").ljust(label_width), t.format(format))
175
+ }
176
+ report.list
177
+ ensure
178
+ STDOUT.sync = sync unless sync.nil?
191
179
  end
192
180
 
193
181
 
194
- # A simple interface to the #benchmark method, #bm is generates sequential reports
195
- # with labels. The parameters have the same meaning as for #benchmark.
182
+ # A simple interface to the #benchmark method, #bm generates sequential
183
+ # reports with labels. The parameters have the same meaning as for
184
+ # #benchmark.
196
185
  #
197
186
  # require 'benchmark'
198
187
  #
199
- # n = 50000
188
+ # n = 5000000
200
189
  # Benchmark.bm(7) do |x|
201
190
  # x.report("for:") { for i in 1..n; a = "1"; end }
202
191
  # x.report("times:") { n.times do ; a = "1"; end }
203
192
  # x.report("upto:") { 1.upto(n) do ; a = "1"; end }
204
193
  # end
205
194
  #
206
- # <i>Generates:</i>
195
+ # Generates:
207
196
  #
208
197
  # user system total real
209
- # for: 1.050000 0.000000 1.050000 ( 0.503462)
210
- # times: 1.533333 0.016667 1.550000 ( 0.735473)
211
- # upto: 1.500000 0.016667 1.516667 ( 0.711239)
198
+ # for: 0.960000 0.000000 0.960000 ( 0.957966)
199
+ # times: 0.960000 0.000000 0.960000 ( 0.960423)
200
+ # upto: 0.950000 0.000000 0.950000 ( 0.954864)
212
201
  #
213
202
 
214
- def bm(label_width=0, *labels, &blk) # :yield: report
215
- benchmark " " * label_width + CAPTION, label_width, FMTSTR, *labels, &blk
203
+ def bm(label_width = 0, *labels, &blk) # :yield: report
204
+ benchmark(CAPTION, label_width, FORMAT, *labels, &blk)
216
205
  end
217
206
 
218
207
 
@@ -221,7 +210,7 @@ module Benchmark
221
210
  # that run later. #bmbm attempts to minimize this effect by running
222
211
  # the tests twice, the first time as a rehearsal in order to get the
223
212
  # runtime environment stable, the second time for
224
- # real. <tt>GC.start</tt> is executed before the start of each of
213
+ # real. GC.start is executed before the start of each of
225
214
  # the real timings; the cost of this is not included in the
226
215
  # timings. In reality, though, there's only so much that #bmbm can
227
216
  # do, and the results are not guaranteed to be isolated from garbage
@@ -239,85 +228,75 @@ module Benchmark
239
228
  # x.report("sort") { array.dup.sort }
240
229
  # end
241
230
  #
242
- # <i>Generates:</i>
231
+ # Generates:
243
232
  #
244
233
  # Rehearsal -----------------------------------------
245
- # sort! 11.928000 0.010000 11.938000 ( 12.756000)
246
- # sort 13.048000 0.020000 13.068000 ( 13.857000)
247
- # ------------------------------- total: 25.006000sec
234
+ # sort! 1.440000 0.010000 1.450000 ( 1.446833)
235
+ # sort 1.440000 0.000000 1.440000 ( 1.448257)
236
+ # -------------------------------- total: 2.890000sec
248
237
  #
249
238
  # user system total real
250
- # sort! 12.959000 0.010000 12.969000 ( 13.793000)
251
- # sort 12.007000 0.000000 12.007000 ( 12.791000)
239
+ # sort! 1.460000 0.000000 1.460000 ( 1.458065)
240
+ # sort 1.450000 0.000000 1.450000 ( 1.455963)
252
241
  #
253
242
  # #bmbm yields a Benchmark::Job object and returns an array of
254
243
  # Benchmark::Tms objects.
255
244
  #
256
- def bmbm(width=0, &blk) # :yield: job
245
+ def bmbm(width = 0) # :yield: job
257
246
  job = Job.new(width)
258
- yield job
259
- width = job.width
260
-
261
- sync, STDOUT.sync = STDOUT.sync, true
247
+ yield(job)
248
+ width = job.width + 1
249
+ sync = STDOUT.sync
250
+ STDOUT.sync = true
262
251
 
263
252
  # rehearsal
264
- print "Rehearsal "
265
- puts '-' * (width+CAPTION.length - "Rehearsal ".length)
266
- list = []
267
- job.list.each do |label_item|
268
- label, item = label_item
253
+ puts 'Rehearsal '.ljust(width+CAPTION.length,'-')
254
+ ets = job.list.inject(Tms.new) { |sum,(label,item)|
269
255
  print label.ljust(width)
270
- res = Benchmark.measure(label, &item)
256
+ res = Benchmark.measure(&item)
271
257
  print res.format
272
- list.push res
273
- end
274
- sum = Tms.new; list.each{ |i| sum += i }
275
- ets = sum.format("total: %tsec")
276
-
277
- printf "%s %s\n\n", "-"*(width+CAPTION.length-ets.length-1), ets
258
+ sum + res
259
+ }.format("total: %tsec")
260
+ print " #{ets}\n\n".rjust(width+CAPTION.length+2,'-')
278
261
 
279
262
  # take
280
- print ' ' * width, CAPTION
281
- list = []
282
- ary = []
283
-
284
- job.list.each do |label_item|
285
- label, item = label_item
263
+ print ' '*width + CAPTION
264
+ job.list.map { |label,item|
286
265
  GC.start
287
266
  print label.ljust(width)
288
- res = Benchmark.measure(label, &item)
289
- print res.format
290
- ary.push res
291
- list.push [label, res]
292
- end
293
-
294
- STDOUT.sync = sync
295
- ary
267
+ Benchmark.measure(label, &item).tap { |res| print res }
268
+ }
269
+ ensure
270
+ STDOUT.sync = sync unless sync.nil?
296
271
  end
297
272
 
298
273
  #
299
274
  # Returns the time used to execute the given block as a
300
275
  # Benchmark::Tms object.
301
276
  #
302
- def measure(label="") # :yield:
303
- t0, r0 = Benchmark.times, Time.now
277
+ def measure(label = "") # :yield:
278
+ t0, r0 = Process.times, Time.now
304
279
  yield
305
- t1, r1 = Benchmark.times, Time.now
306
- Benchmark::Tms.new(t1.tutime - t0.tutime,
307
- t1.tstime - t0.tstime,
280
+ t1, r1 = Process.times, Time.now
281
+ Benchmark::Tms.new(t1.utime - t0.utime,
282
+ t1.stime - t0.stime,
308
283
  t1.cutime - t0.cutime,
309
284
  t1.cstime - t0.cstime,
310
- r1.to_f - r0.to_f,
285
+ r1 - r0,
311
286
  label)
312
287
  end
313
288
 
314
289
  #
315
290
  # Returns the elapsed real time used to execute the given block.
316
291
  #
317
- def realtime(&blk) # :yield:
318
- Benchmark.measure(&blk).real
292
+ def realtime # :yield:
293
+ r0 = Time.now
294
+ yield
295
+ Time.now - r0
319
296
  end
320
297
 
298
+ module_function :benchmark, :measure, :realtime, :bm, :bmbm
299
+
321
300
  #
322
301
  # A Job is a sequence of labelled blocks to be processed by the
323
302
  # Benchmark.bmbm method. It is of little direct interest to the user.
@@ -327,8 +306,8 @@ module Benchmark
327
306
  # Returns an initialized Job instance.
328
307
  # Usually, one doesn't call this method directly, as new
329
308
  # Job objects are created by the #bmbm method.
330
- # _width_ is a initial value for the label offset used in formatting;
331
- # the #bmbm method passes its _width_ argument to this constructor.
309
+ # +width+ is a initial value for the label offset used in formatting;
310
+ # the #bmbm method passes its +width+ argument to this constructor.
332
311
  #
333
312
  def initialize(width)
334
313
  @width = width
@@ -338,13 +317,12 @@ module Benchmark
338
317
  #
339
318
  # Registers the given label and block pair in the job list.
340
319
  #
341
- def item(label="", &blk) # :yield:
342
- raise ArgmentError, "no block" unless block_given?
343
-
344
- label.concat ' '
320
+ def item(label = "", &blk) # :yield:
321
+ raise ArgumentError, "no block" unless block_given?
322
+ label = label.to_s
345
323
  w = label.length
346
324
  @width = w if @width < w
347
- @list.push [label, blk]
325
+ @list << [label, blk]
348
326
  self
349
327
  end
350
328
 
@@ -353,12 +331,10 @@ module Benchmark
353
331
  # An array of 2-element arrays, consisting of label and block pairs.
354
332
  attr_reader :list
355
333
 
356
- # Length of the widest label in the #list, plus one.
334
+ # Length of the widest label in the #list.
357
335
  attr_reader :width
358
336
  end
359
337
 
360
- module_function :benchmark, :measure, :realtime, :bm, :bmbm
361
-
362
338
  #
363
339
  # This class is used by the Benchmark.benchmark and Benchmark.bm methods.
364
340
  # It is of little direct interest to the user.
@@ -368,26 +344,29 @@ module Benchmark
368
344
  # Returns an initialized Report instance.
369
345
  # Usually, one doesn't call this method directly, as new
370
346
  # Report objects are created by the #benchmark and #bm methods.
371
- # _width_ and _fmtstr_ are the label offset and
347
+ # +width+ and +format+ are the label offset and
372
348
  # format string used by Tms#format.
373
349
  #
374
- def initialize(width=0, fmtstr=nil)
375
- @width, @fmtstr = width, fmtstr
350
+ def initialize(width = 0, format = nil)
351
+ @width, @format, @list = width, format, []
376
352
  end
377
353
 
378
354
  #
379
- # Prints the _label_ and measured time for the block,
380
- # formatted by _fmt_. See Tms#format for the
355
+ # Prints the +label+ and measured time for the block,
356
+ # formatted by +format+. See Tms#format for the
381
357
  # formatting rules.
382
358
  #
383
- def item(label="", *fmt, &blk) # :yield:
384
- print label.ljust(@width)
385
- res = Benchmark.measure(label, &blk)
386
- print res.format(@fmtstr, *fmt)
359
+ def item(label = "", *format, &blk) # :yield:
360
+ print label.to_s.ljust(@width)
361
+ @list << res = Benchmark.measure(label, &blk)
362
+ print res.format(@format, *format)
387
363
  res
388
364
  end
389
365
 
390
366
  alias report item
367
+
368
+ # An array of Benchmark::Tms objects representing each item.
369
+ attr_reader :list
391
370
  end
392
371
 
393
372
 
@@ -397,8 +376,12 @@ module Benchmark
397
376
  # measurement.
398
377
  #
399
378
  class Tms
379
+
380
+ # Default caption, see also Benchmark::CAPTION
400
381
  CAPTION = " user system total real\n"
401
- FMTSTR = "%10.6u %10.6y %10.6t %10.6r\n"
382
+
383
+ # Default format string, see also Benchmark::FORMAT
384
+ FORMAT = "%10.6u %10.6y %10.6t %10.6r\n"
402
385
 
403
386
  # User CPU time
404
387
  attr_reader :utime
@@ -415,7 +398,7 @@ module Benchmark
415
398
  # Elapsed real time
416
399
  attr_reader :real
417
400
 
418
- # Total time, that is _utime_ + _stime_ + _cutime_ + _cstime_
401
+ # Total time, that is +utime+ + +stime+ + +cutime+ + +cstime+
419
402
  attr_reader :total
420
403
 
421
404
  # Label
@@ -423,19 +406,18 @@ module Benchmark
423
406
 
424
407
  #
425
408
  # Returns an initialized Tms object which has
426
- # _u_ as the user CPU time, _s_ as the system CPU time,
427
- # _cu_ as the children's user CPU time, _cs_ as the children's
428
- # system CPU time, _real_ as the elapsed real time and _l_
429
- # as the label.
409
+ # +utime+ as the user CPU time, +stime+ as the system CPU time,
410
+ # +cutime+ as the children's user CPU time, +cstime+ as the children's
411
+ # system CPU time, +real+ as the elapsed real time and +label+ as the label.
430
412
  #
431
- def initialize(u=0.0, s=0.0, cu=0.0, cs=0.0, real=0.0, l=nil)
432
- @utime, @stime, @cutime, @cstime, @real, @label = u, s, cu, cs, real, l
413
+ def initialize(utime = 0.0, stime = 0.0, cutime = 0.0, cstime = 0.0, real = 0.0, label = nil)
414
+ @utime, @stime, @cutime, @cstime, @real, @label = utime, stime, cutime, cstime, real, label.to_s
433
415
  @total = @utime + @stime + @cutime + @cstime
434
416
  end
435
417
 
436
418
  #
437
419
  # Returns a new Tms object whose times are the sum of the times for this
438
- # Tms object, plus the time required to execute the code block (_blk_).
420
+ # Tms object, plus the time required to execute the code block (+blk+).
439
421
  #
440
422
  def add(&blk) # :yield:
441
423
  self + Benchmark.measure(&blk)
@@ -496,20 +478,19 @@ module Benchmark
496
478
  # <tt>%r</tt>:: Replaced by the elapsed real time, as reported by Tms#real
497
479
  # <tt>%n</tt>:: Replaced by the label string, as reported by Tms#label (Mnemonic: n of "*n*ame")
498
480
  #
499
- # If _fmtstr_ is not given, FMTSTR is used as default value, detailing the
481
+ # If _format_ is not given, FORMAT is used as default value, detailing the
500
482
  # user, system and real elapsed time.
501
483
  #
502
- def format(arg0=nil, *args)
503
- fmtstr = (arg0 || FMTSTR).dup
504
- fmtstr.gsub!(/(%[-+\.\d]*)n/) { "#{$1}s" % label }
505
- fmtstr.gsub!(/(%[-+\.\d]*)u/) { "#{$1}f" % utime }
506
- fmtstr.gsub!(/(%[-+\.\d]*)y/) { "#{$1}f" % stime }
507
- fmtstr.gsub!(/(%[-+\.\d]*)U/) { "#{$1}f" % cutime }
508
- fmtstr.gsub!(/(%[-+\.\d]*)Y/) { "#{$1}f" % cstime }
509
- fmtstr.gsub!(/(%[-+\.\d]*)t/) { "#{$1}f" % total }
510
- fmtstr.gsub!(/(%[-+\.\d]*)r/) { "(#{$1}f)" % real }
511
-
512
- arg0 ? Kernel.format(fmtstr, *args) : fmtstr
484
+ def format(format = nil, *args)
485
+ str = (format || FORMAT).dup
486
+ str.gsub!(/(%[-+.\d]*)n/) { "#{$1}s" % label }
487
+ str.gsub!(/(%[-+.\d]*)u/) { "#{$1}f" % utime }
488
+ str.gsub!(/(%[-+.\d]*)y/) { "#{$1}f" % stime }
489
+ str.gsub!(/(%[-+.\d]*)U/) { "#{$1}f" % cutime }
490
+ str.gsub!(/(%[-+.\d]*)Y/) { "#{$1}f" % cstime }
491
+ str.gsub!(/(%[-+.\d]*)t/) { "#{$1}f" % total }
492
+ str.gsub!(/(%[-+.\d]*)r/) { "(#{$1}f)" % real }
493
+ format ? str % args : str
513
494
  end
514
495
 
515
496
  #
@@ -530,6 +511,15 @@ module Benchmark
530
511
  end
531
512
 
532
513
  protected
514
+
515
+ #
516
+ # Returns a new Tms object obtained by memberwise operation +op+
517
+ # of the individual times for this Tms object with those of the other
518
+ # Tms object.
519
+ #
520
+ # +op+ can be a mathematical operation such as <tt>+</tt>, <tt>-</tt>,
521
+ # <tt>*</tt>, <tt>/</tt>
522
+ #
533
523
  def memberwise(op, x)
534
524
  case x
535
525
  when Benchmark::Tms
@@ -538,14 +528,14 @@ module Benchmark
538
528
  cutime.__send__(op, x.cutime),
539
529
  cstime.__send__(op, x.cstime),
540
530
  real.__send__(op, x.real)
541
- )
531
+ )
542
532
  else
543
533
  Benchmark::Tms.new(utime.__send__(op, x),
544
534
  stime.__send__(op, x),
545
535
  cutime.__send__(op, x),
546
536
  cstime.__send__(op, x),
547
537
  real.__send__(op, x)
548
- )
538
+ )
549
539
  end
550
540
  end
551
541
  end
@@ -554,7 +544,7 @@ module Benchmark
554
544
  CAPTION = Benchmark::Tms::CAPTION
555
545
 
556
546
  # The default format string used to display times. See also Benchmark::Tms#format.
557
- FMTSTR = Benchmark::Tms::FMTSTR
547
+ FORMAT = Benchmark::Tms::FORMAT
558
548
  end
559
549
 
560
550
  if __FILE__ == $0
@@ -562,17 +552,17 @@ if __FILE__ == $0
562
552
 
563
553
  n = ARGV[0].to_i.nonzero? || 50000
564
554
  puts %Q([#{n} times iterations of `a = "1"'])
565
- benchmark(" " + CAPTION, 7, FMTSTR) do |x|
566
- x.report("for:") {for i in 1..n; a = "1"; end} # Benchmark.measure
567
- x.report("times:") {n.times do ; a = "1"; end}
568
- x.report("upto:") {1.upto(n) do ; a = "1"; end}
555
+ benchmark(CAPTION, 7, FORMAT) do |x|
556
+ x.report("for:") {for _ in 1..n; _ = "1"; end} # Benchmark.measure
557
+ x.report("times:") {n.times do ; _ = "1"; end}
558
+ x.report("upto:") {1.upto(n) do ; _ = "1"; end}
569
559
  end
570
560
 
571
561
  benchmark do
572
562
  [
573
- measure{for i in 1..n; a = "1"; end}, # Benchmark.measure
574
- measure{n.times do ; a = "1"; end},
575
- measure{1.upto(n) do ; a = "1"; end}
563
+ measure{for _ in 1..n; _ = "1"; end}, # Benchmark.measure
564
+ measure{n.times do ; _ = "1"; end},
565
+ measure{1.upto(n) do ; _ = "1"; end}
576
566
  ]
577
567
  end
578
568
  end
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Benchmark
3
- VERSION = "2.0.0"
3
+ VERSION = "2.0.1"
4
4
  end
5
5
  end
@@ -16,7 +16,10 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ["lib"]
18
18
 
19
+ spec.required_ruby_version = "~> 2.0"
20
+
19
21
  spec.add_development_dependency "bundler", "~> 1.3"
20
22
  spec.add_development_dependency "rake", "~> 10.0"
21
23
  spec.add_development_dependency "mspec", "~> 1.5"
24
+ spec.add_development_dependency "rubysl-prettyprint", "~> 2.0"
22
25
  end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-benchmark
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-03 00:00:00.000000000 Z
11
+ date: 2013-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubysl-prettyprint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
55
69
  description: Ruby standard library benchmark.
56
70
  email:
57
71
  - brixen@gmail.com
@@ -59,8 +73,8 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - .gitignore
63
- - .travis.yml
76
+ - ".gitignore"
77
+ - ".travis.yml"
64
78
  - Gemfile
65
79
  - LICENSE
66
80
  - README.md
@@ -80,12 +94,12 @@ require_paths:
80
94
  - lib
81
95
  required_ruby_version: !ruby/object:Gem::Requirement
82
96
  requirements:
83
- - - '>='
97
+ - - "~>"
84
98
  - !ruby/object:Gem::Version
85
- version: '0'
99
+ version: '2.0'
86
100
  required_rubygems_version: !ruby/object:Gem::Requirement
87
101
  requirements:
88
- - - '>='
102
+ - - ">="
89
103
  - !ruby/object:Gem::Version
90
104
  version: '0'
91
105
  requirements: []
@@ -95,3 +109,4 @@ signing_key:
95
109
  specification_version: 4
96
110
  summary: Ruby standard library benchmark.
97
111
  test_files: []
112
+ has_rdoc: