rubysl-benchmark 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63fdfb647ca73b466176a4fac07207687b50cc75
4
+ data.tar.gz: ba93edf6ca3b40f755d03032fb51560c6917f1a9
5
+ SHA512:
6
+ metadata.gz: 907ed9583d2546141865d79e1cdec8a1ff5115e890537b64eedb52e04dccdbef9bce75761651ac824ff5a54d54cc01021d7aaae8d908657311716b3f8c6525b1
7
+ data.tar.gz: d857d0f9fcac107bc8ab2807bce527de7a90374bdf4cbe7023babd3e789430a7c970497f49916c29b2426fa05b0ea60960c509e5654feae3028f882ef4fc773c
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update --system
4
+ - gem --version
5
+ - gem install rubysl-bundler
6
+ script: bundle exec mspec spec
7
+ rvm:
8
+ - rbx-nightly-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubysl-benchmark.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2013, Brian Shirai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. Neither the name of the library nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,29 @@
1
+ # Rubysl::Benchmark
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubysl-benchmark'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubysl-benchmark
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require "rubysl/benchmark"
@@ -0,0 +1,2 @@
1
+ require "rubysl/benchmark/benchmark"
2
+ require "rubysl/benchmark/version"
@@ -0,0 +1,578 @@
1
+ =begin
2
+ #
3
+ # benchmark.rb - a performance benchmarking library
4
+ #
5
+ # $Id: benchmark.rb 11708 2007-02-12 23:01:19Z shyouhei $
6
+ #
7
+ # Created by Gotoken (gotoken@notwork.org).
8
+ #
9
+ # Documentation by Gotoken (original RD), Lyle Johnson (RDoc conversion), and
10
+ # Gavin Sinclair (editing).
11
+ #
12
+ =end
13
+
14
+ # == Overview
15
+ #
16
+ # The Benchmark module provides methods for benchmarking Ruby code, giving
17
+ # detailed reports on the time taken for each task.
18
+ #
19
+
20
+ # The Benchmark module provides methods to measure and report the time
21
+ # used to execute Ruby code.
22
+ #
23
+ # * Measure the time to construct the string given by the expression
24
+ # <tt>"a"*1_000_000</tt>:
25
+ #
26
+ # require 'benchmark'
27
+ #
28
+ # puts Benchmark.measure { "a"*1_000_000 }
29
+ #
30
+ # On my machine (FreeBSD 3.2 on P5, 100MHz) this generates:
31
+ #
32
+ # 1.166667 0.050000 1.216667 ( 0.571355)
33
+ #
34
+ # This report shows the user CPU time, system CPU time, the sum of
35
+ # the user and system CPU times, and the elapsed real time. The unit
36
+ # of time is seconds.
37
+ #
38
+ # * Do some experiments sequentially using the #bm method:
39
+ #
40
+ # require 'benchmark'
41
+ #
42
+ # n = 50000
43
+ # Benchmark.bm do |x|
44
+ # x.report { for i in 1..n; a = "1"; end }
45
+ # x.report { n.times do ; a = "1"; end }
46
+ # x.report { 1.upto(n) do ; a = "1"; end }
47
+ # end
48
+ #
49
+ # The result:
50
+ #
51
+ # 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)
55
+ #
56
+ # * Continuing the previous example, put a label in each report:
57
+ #
58
+ # require 'benchmark'
59
+ #
60
+ # n = 50000
61
+ # Benchmark.bm(7) do |x|
62
+ # x.report("for:") { for i in 1..n; a = "1"; end }
63
+ # x.report("times:") { n.times do ; a = "1"; end }
64
+ # x.report("upto:") { 1.upto(n) do ; a = "1"; end }
65
+ # end
66
+ #
67
+ # The result:
68
+ #
69
+ # 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
+ #
74
+ #
75
+ # * The times for some benchmarks depend on the order in which items
76
+ # are run. These differences are due to the cost of memory
77
+ # allocation and garbage collection. To avoid these discrepancies,
78
+ # the #bmbm method is provided. For example, to compare ways to
79
+ # sort an array of floats:
80
+ #
81
+ # require 'benchmark'
82
+ #
83
+ # array = (1..1000000).map { rand }
84
+ #
85
+ # Benchmark.bmbm do |x|
86
+ # x.report("sort!") { array.dup.sort! }
87
+ # x.report("sort") { array.dup.sort }
88
+ # end
89
+ #
90
+ # The result:
91
+ #
92
+ # 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
96
+ #
97
+ # 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
+ #
101
+ #
102
+ # * Report statistics of sequential experiments with unique labels,
103
+ # using the #benchmark method:
104
+ #
105
+ # require 'benchmark'
106
+ #
107
+ # n = 50000
108
+ # Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
109
+ # tf = x.report("for:") { for i in 1..n; a = "1"; end }
110
+ # tt = x.report("times:") { n.times do ; a = "1"; end }
111
+ # tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
112
+ # [tf+tt+tu, (tf+tt+tu)/3]
113
+ # end
114
+ #
115
+ # The result:
116
+ #
117
+ # 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)
123
+
124
+ module Benchmark
125
+
126
+ BENCHMARK_VERSION = "2002-04-25" # :nodoc:
127
+
128
+ def Benchmark.times # :nodoc:
129
+ Process.times
130
+ end
131
+
132
+
133
+ # Invokes the block with a <tt>Benchmark::Report</tt> object, which
134
+ # 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.
138
+ # 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
141
+ # given, these are used to label these extra lines.
142
+ #
143
+ # _Note_: Other methods provide a simpler interface to this one, and are
144
+ # suitable for nearly all benchmarking requirements. See the examples in
145
+ # Benchmark, and the #bm and #bmbm methods.
146
+ #
147
+ # Example:
148
+ #
149
+ # require 'benchmark'
150
+ # include Benchmark # we need the CAPTION and FMTSTR constants
151
+ #
152
+ # n = 50000
153
+ # Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
154
+ # tf = x.report("for:") { for i in 1..n; a = "1"; end }
155
+ # tt = x.report("times:") { n.times do ; a = "1"; end }
156
+ # tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
157
+ # [tf+tt+tu, (tf+tt+tu)/3]
158
+ # end
159
+ #
160
+ # <i>Generates:</i>
161
+ #
162
+ # 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)
168
+ #
169
+
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
+
176
+ 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
191
+ end
192
+
193
+
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.
196
+ #
197
+ # require 'benchmark'
198
+ #
199
+ # n = 50000
200
+ # Benchmark.bm(7) do |x|
201
+ # x.report("for:") { for i in 1..n; a = "1"; end }
202
+ # x.report("times:") { n.times do ; a = "1"; end }
203
+ # x.report("upto:") { 1.upto(n) do ; a = "1"; end }
204
+ # end
205
+ #
206
+ # <i>Generates:</i>
207
+ #
208
+ # 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)
212
+ #
213
+
214
+ def bm(label_width=0, *labels, &blk) # :yield: report
215
+ benchmark " " * label_width + CAPTION, label_width, FMTSTR, *labels, &blk
216
+ end
217
+
218
+
219
+ # Sometimes benchmark results are skewed because code executed
220
+ # earlier encounters different garbage collection overheads than
221
+ # that run later. #bmbm attempts to minimize this effect by running
222
+ # the tests twice, the first time as a rehearsal in order to get the
223
+ # runtime environment stable, the second time for
224
+ # real. <tt>GC.start</tt> is executed before the start of each of
225
+ # the real timings; the cost of this is not included in the
226
+ # timings. In reality, though, there's only so much that #bmbm can
227
+ # do, and the results are not guaranteed to be isolated from garbage
228
+ # collection and other effects.
229
+ #
230
+ # Because #bmbm takes two passes through the tests, it can
231
+ # calculate the required label width.
232
+ #
233
+ # require 'benchmark'
234
+ #
235
+ # array = (1..1000000).map { rand }
236
+ #
237
+ # Benchmark.bmbm do |x|
238
+ # x.report("sort!") { array.dup.sort! }
239
+ # x.report("sort") { array.dup.sort }
240
+ # end
241
+ #
242
+ # <i>Generates:</i>
243
+ #
244
+ # 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
248
+ #
249
+ # user system total real
250
+ # sort! 12.959000 0.010000 12.969000 ( 13.793000)
251
+ # sort 12.007000 0.000000 12.007000 ( 12.791000)
252
+ #
253
+ # #bmbm yields a Benchmark::Job object and returns an array of
254
+ # Benchmark::Tms objects.
255
+ #
256
+ def bmbm(width=0, &blk) # :yield: job
257
+ job = Job.new(width)
258
+ yield job
259
+ width = job.width
260
+
261
+ sync, STDOUT.sync = STDOUT.sync, true
262
+
263
+ # 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
269
+ print label.ljust(width)
270
+ res = Benchmark.measure(label, &item)
271
+ 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
278
+
279
+ # take
280
+ print ' ' * width, CAPTION
281
+ list = []
282
+ ary = []
283
+
284
+ job.list.each do |label_item|
285
+ label, item = label_item
286
+ GC.start
287
+ 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
296
+ end
297
+
298
+ #
299
+ # Returns the time used to execute the given block as a
300
+ # Benchmark::Tms object.
301
+ #
302
+ def measure(label="") # :yield:
303
+ t0, r0 = Benchmark.times, Time.now
304
+ yield
305
+ t1, r1 = Benchmark.times, Time.now
306
+ Benchmark::Tms.new(t1.tutime - t0.tutime,
307
+ t1.tstime - t0.tstime,
308
+ t1.cutime - t0.cutime,
309
+ t1.cstime - t0.cstime,
310
+ r1.to_f - r0.to_f,
311
+ label)
312
+ end
313
+
314
+ #
315
+ # Returns the elapsed real time used to execute the given block.
316
+ #
317
+ def realtime(&blk) # :yield:
318
+ Benchmark.measure(&blk).real
319
+ end
320
+
321
+ #
322
+ # A Job is a sequence of labelled blocks to be processed by the
323
+ # Benchmark.bmbm method. It is of little direct interest to the user.
324
+ #
325
+ class Job # :nodoc:
326
+ #
327
+ # Returns an initialized Job instance.
328
+ # Usually, one doesn't call this method directly, as new
329
+ # 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.
332
+ #
333
+ def initialize(width)
334
+ @width = width
335
+ @list = []
336
+ end
337
+
338
+ #
339
+ # Registers the given label and block pair in the job list.
340
+ #
341
+ def item(label="", &blk) # :yield:
342
+ raise ArgmentError, "no block" unless block_given?
343
+
344
+ label.concat ' '
345
+ w = label.length
346
+ @width = w if @width < w
347
+ @list.push [label, blk]
348
+ self
349
+ end
350
+
351
+ alias report item
352
+
353
+ # An array of 2-element arrays, consisting of label and block pairs.
354
+ attr_reader :list
355
+
356
+ # Length of the widest label in the #list, plus one.
357
+ attr_reader :width
358
+ end
359
+
360
+ module_function :benchmark, :measure, :realtime, :bm, :bmbm
361
+
362
+ #
363
+ # This class is used by the Benchmark.benchmark and Benchmark.bm methods.
364
+ # It is of little direct interest to the user.
365
+ #
366
+ class Report # :nodoc:
367
+ #
368
+ # Returns an initialized Report instance.
369
+ # Usually, one doesn't call this method directly, as new
370
+ # Report objects are created by the #benchmark and #bm methods.
371
+ # _width_ and _fmtstr_ are the label offset and
372
+ # format string used by Tms#format.
373
+ #
374
+ def initialize(width=0, fmtstr=nil)
375
+ @width, @fmtstr = width, fmtstr
376
+ end
377
+
378
+ #
379
+ # Prints the _label_ and measured time for the block,
380
+ # formatted by _fmt_. See Tms#format for the
381
+ # formatting rules.
382
+ #
383
+ def item(label="", *fmt, &blk) # :yield:
384
+ print label.ljust(@width)
385
+ res = Benchmark.measure(label, &blk)
386
+ print res.format(@fmtstr, *fmt)
387
+ res
388
+ end
389
+
390
+ alias report item
391
+ end
392
+
393
+
394
+
395
+ #
396
+ # A data object, representing the times associated with a benchmark
397
+ # measurement.
398
+ #
399
+ class Tms
400
+ CAPTION = " user system total real\n"
401
+ FMTSTR = "%10.6u %10.6y %10.6t %10.6r\n"
402
+
403
+ # User CPU time
404
+ attr_reader :utime
405
+
406
+ # System CPU time
407
+ attr_reader :stime
408
+
409
+ # User CPU time of children
410
+ attr_reader :cutime
411
+
412
+ # System CPU time of children
413
+ attr_reader :cstime
414
+
415
+ # Elapsed real time
416
+ attr_reader :real
417
+
418
+ # Total time, that is _utime_ + _stime_ + _cutime_ + _cstime_
419
+ attr_reader :total
420
+
421
+ # Label
422
+ attr_reader :label
423
+
424
+ #
425
+ # 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.
430
+ #
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
433
+ @total = @utime + @stime + @cutime + @cstime
434
+ end
435
+
436
+ #
437
+ # 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_).
439
+ #
440
+ def add(&blk) # :yield:
441
+ self + Benchmark.measure(&blk)
442
+ end
443
+
444
+ #
445
+ # An in-place version of #add.
446
+ #
447
+ def add!(&blk)
448
+ t = Benchmark.measure(&blk)
449
+ @utime = utime + t.utime
450
+ @stime = stime + t.stime
451
+ @cutime = cutime + t.cutime
452
+ @cstime = cstime + t.cstime
453
+ @real = real + t.real
454
+ self
455
+ end
456
+
457
+ #
458
+ # Returns a new Tms object obtained by memberwise summation
459
+ # of the individual times for this Tms object with those of the other
460
+ # Tms object.
461
+ # This method and #/() are useful for taking statistics.
462
+ #
463
+ def +(other); memberwise(:+, other) end
464
+
465
+ #
466
+ # Returns a new Tms object obtained by memberwise subtraction
467
+ # of the individual times for the other Tms object from those of this
468
+ # Tms object.
469
+ #
470
+ def -(other); memberwise(:-, other) end
471
+
472
+ #
473
+ # Returns a new Tms object obtained by memberwise multiplication
474
+ # of the individual times for this Tms object by _x_.
475
+ #
476
+ def *(x); memberwise(:*, x) end
477
+
478
+ #
479
+ # Returns a new Tms object obtained by memberwise division
480
+ # of the individual times for this Tms object by _x_.
481
+ # This method and #+() are useful for taking statistics.
482
+ #
483
+ def /(x); memberwise(:/, x) end
484
+
485
+ #
486
+ # Returns the contents of this Tms object as
487
+ # a formatted string, according to a format string
488
+ # like that passed to Kernel.format. In addition, #format
489
+ # accepts the following extensions:
490
+ #
491
+ # <tt>%u</tt>:: Replaced by the user CPU time, as reported by Tms#utime.
492
+ # <tt>%y</tt>:: Replaced by the system CPU time, as reported by #stime (Mnemonic: y of "s*y*stem")
493
+ # <tt>%U</tt>:: Replaced by the children's user CPU time, as reported by Tms#cutime
494
+ # <tt>%Y</tt>:: Replaced by the children's system CPU time, as reported by Tms#cstime
495
+ # <tt>%t</tt>:: Replaced by the total CPU time, as reported by Tms#total
496
+ # <tt>%r</tt>:: Replaced by the elapsed real time, as reported by Tms#real
497
+ # <tt>%n</tt>:: Replaced by the label string, as reported by Tms#label (Mnemonic: n of "*n*ame")
498
+ #
499
+ # If _fmtstr_ is not given, FMTSTR is used as default value, detailing the
500
+ # user, system and real elapsed time.
501
+ #
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
513
+ end
514
+
515
+ #
516
+ # Same as #format.
517
+ #
518
+ def to_s
519
+ format
520
+ end
521
+
522
+ #
523
+ # Returns a new 6-element array, consisting of the
524
+ # label, user CPU time, system CPU time, children's
525
+ # user CPU time, children's system CPU time and elapsed
526
+ # real time.
527
+ #
528
+ def to_a
529
+ [@label, @utime, @stime, @cutime, @cstime, @real]
530
+ end
531
+
532
+ protected
533
+ def memberwise(op, x)
534
+ case x
535
+ when Benchmark::Tms
536
+ Benchmark::Tms.new(utime.__send__(op, x.utime),
537
+ stime.__send__(op, x.stime),
538
+ cutime.__send__(op, x.cutime),
539
+ cstime.__send__(op, x.cstime),
540
+ real.__send__(op, x.real)
541
+ )
542
+ else
543
+ Benchmark::Tms.new(utime.__send__(op, x),
544
+ stime.__send__(op, x),
545
+ cutime.__send__(op, x),
546
+ cstime.__send__(op, x),
547
+ real.__send__(op, x)
548
+ )
549
+ end
550
+ end
551
+ end
552
+
553
+ # The default caption string (heading above the output times).
554
+ CAPTION = Benchmark::Tms::CAPTION
555
+
556
+ # The default format string used to display times. See also Benchmark::Tms#format.
557
+ FMTSTR = Benchmark::Tms::FMTSTR
558
+ end
559
+
560
+ if __FILE__ == $0
561
+ include Benchmark
562
+
563
+ n = ARGV[0].to_i.nonzero? || 50000
564
+ 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}
569
+ end
570
+
571
+ benchmark do
572
+ [
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}
576
+ ]
577
+ end
578
+ end
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module Benchmark
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/benchmark/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-benchmark"
6
+ spec.version = RubySL::Benchmark::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library benchmark.}
10
+ spec.summary = %q{Ruby standard library benchmark.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-benchmark"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
22
+ spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysl-benchmark
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Shirai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
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: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Ruby standard library benchmark.
70
+ email:
71
+ - brixen@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/benchmark.rb
83
+ - lib/rubysl/benchmark.rb
84
+ - lib/rubysl/benchmark/benchmark.rb
85
+ - lib/rubysl/benchmark/version.rb
86
+ - rubysl-benchmark.gemspec
87
+ homepage: https://github.com/rubysl/rubysl-benchmark
88
+ licenses:
89
+ - BSD
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.7
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ruby standard library benchmark.
111
+ test_files: []