motion-benchmark 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +33 -0
- data/lib/benchmark.rb +577 -0
- data/lib/motion-benchmark.rb +7 -0
- metadata +48 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Watson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
## What's this?
|
2
|
+
|
3
|
+
This is benchmark library for RubyMotion.
|
4
|
+
This library provides methods to measure and report the time used to execute your code.
|
5
|
+
|
6
|
+
### Install
|
7
|
+
|
8
|
+
```
|
9
|
+
$ [sudo] gem install motion-benchmark
|
10
|
+
```
|
11
|
+
|
12
|
+
### Usage
|
13
|
+
|
14
|
+
This gem can be used in `Rakefile` of your RubyMotion project by requiring.
|
15
|
+
|
16
|
+
```
|
17
|
+
require 'rubygems'
|
18
|
+
require 'motion-benchmark'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then, you would write the code to measure, like:
|
22
|
+
|
23
|
+
```
|
24
|
+
n = 50000
|
25
|
+
Benchmark.bm do |x|
|
26
|
+
x.report { for i in 1..n; a = "1"; end }
|
27
|
+
x.report { n.times do ; a = "1"; end }
|
28
|
+
x.report { 1.upto(n) do ; a = "1"; end }
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
This library provides the same APIs as standard Ruby.
|
33
|
+
You could see the APIs references at [http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html](http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html)
|
data/lib/benchmark.rb
ADDED
@@ -0,0 +1,577 @@
|
|
1
|
+
=begin
|
2
|
+
#
|
3
|
+
# benchmark.rb - a performance benchmarking library
|
4
|
+
#
|
5
|
+
# $Id: benchmark.rb 15426 2008-02-10 15:29:00Z naruse $
|
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
|
+
sync = STDOUT.sync
|
172
|
+
STDOUT.sync = true
|
173
|
+
label_width ||= 0
|
174
|
+
fmtstr ||= FMTSTR
|
175
|
+
raise ArgumentError, "no block" unless iterator?
|
176
|
+
print "\n"
|
177
|
+
print caption
|
178
|
+
results = yield(Report.new(label_width, fmtstr))
|
179
|
+
Array === results and results.grep(Tms).each {|t|
|
180
|
+
print((labels.shift || t.label || "").ljust(label_width),
|
181
|
+
t.format(fmtstr))
|
182
|
+
}
|
183
|
+
STDOUT.sync = sync
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
# A simple interface to the #benchmark method, #bm is generates sequential reports
|
188
|
+
# with labels. The parameters have the same meaning as for #benchmark.
|
189
|
+
#
|
190
|
+
# require 'benchmark'
|
191
|
+
#
|
192
|
+
# n = 50000
|
193
|
+
# Benchmark.bm(7) do |x|
|
194
|
+
# x.report("for:") { for i in 1..n; a = "1"; end }
|
195
|
+
# x.report("times:") { n.times do ; a = "1"; end }
|
196
|
+
# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
|
197
|
+
# end
|
198
|
+
#
|
199
|
+
# <i>Generates:</i>
|
200
|
+
#
|
201
|
+
# user system total real
|
202
|
+
# for: 1.050000 0.000000 1.050000 ( 0.503462)
|
203
|
+
# times: 1.533333 0.016667 1.550000 ( 0.735473)
|
204
|
+
# upto: 1.500000 0.016667 1.516667 ( 0.711239)
|
205
|
+
#
|
206
|
+
|
207
|
+
def bm(label_width = 0, *labels, &blk) # :yield: report
|
208
|
+
benchmark(" "*label_width + CAPTION, label_width, FMTSTR, *labels, &blk)
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
# Sometimes benchmark results are skewed because code executed
|
213
|
+
# earlier encounters different garbage collection overheads than
|
214
|
+
# that run later. #bmbm attempts to minimize this effect by running
|
215
|
+
# the tests twice, the first time as a rehearsal in order to get the
|
216
|
+
# runtime environment stable, the second time for
|
217
|
+
# real. <tt>GC.start</tt> is executed before the start of each of
|
218
|
+
# the real timings; the cost of this is not included in the
|
219
|
+
# timings. In reality, though, there's only so much that #bmbm can
|
220
|
+
# do, and the results are not guaranteed to be isolated from garbage
|
221
|
+
# collection and other effects.
|
222
|
+
#
|
223
|
+
# Because #bmbm takes two passes through the tests, it can
|
224
|
+
# calculate the required label width.
|
225
|
+
#
|
226
|
+
# require 'benchmark'
|
227
|
+
#
|
228
|
+
# array = (1..1000000).map { rand }
|
229
|
+
#
|
230
|
+
# Benchmark.bmbm do |x|
|
231
|
+
# x.report("sort!") { array.dup.sort! }
|
232
|
+
# x.report("sort") { array.dup.sort }
|
233
|
+
# end
|
234
|
+
#
|
235
|
+
# <i>Generates:</i>
|
236
|
+
#
|
237
|
+
# Rehearsal -----------------------------------------
|
238
|
+
# sort! 11.928000 0.010000 11.938000 ( 12.756000)
|
239
|
+
# sort 13.048000 0.020000 13.068000 ( 13.857000)
|
240
|
+
# ------------------------------- total: 25.006000sec
|
241
|
+
#
|
242
|
+
# user system total real
|
243
|
+
# sort! 12.959000 0.010000 12.969000 ( 13.793000)
|
244
|
+
# sort 12.007000 0.000000 12.007000 ( 12.791000)
|
245
|
+
#
|
246
|
+
# #bmbm yields a Benchmark::Job object and returns an array of
|
247
|
+
# Benchmark::Tms objects.
|
248
|
+
#
|
249
|
+
def bmbm(width = 0, &blk) # :yield: job
|
250
|
+
job = Job.new(width)
|
251
|
+
yield(job)
|
252
|
+
width = job.width
|
253
|
+
sync = STDOUT.sync
|
254
|
+
STDOUT.sync = true
|
255
|
+
|
256
|
+
# rehearsal
|
257
|
+
print "\n"
|
258
|
+
print "Rehearsal "
|
259
|
+
puts '-'*(width+CAPTION.length - "Rehearsal ".length)
|
260
|
+
list = []
|
261
|
+
job.list.each{|label,item|
|
262
|
+
print(label.ljust(width))
|
263
|
+
res = Benchmark::measure(&item)
|
264
|
+
print res.format()
|
265
|
+
list.push res
|
266
|
+
}
|
267
|
+
sum = Tms.new; list.each{|i| sum += i}
|
268
|
+
ets = sum.format("total: %tsec")
|
269
|
+
printf("%s %s\n\n",
|
270
|
+
"-"*(width+CAPTION.length-ets.length-1), ets)
|
271
|
+
|
272
|
+
# take
|
273
|
+
print ' '*width, CAPTION
|
274
|
+
list = []
|
275
|
+
ary = []
|
276
|
+
job.list.each{|label,item|
|
277
|
+
GC::start
|
278
|
+
print label.ljust(width)
|
279
|
+
res = Benchmark::measure(&item)
|
280
|
+
print res.format()
|
281
|
+
ary.push res
|
282
|
+
list.push [label, res]
|
283
|
+
}
|
284
|
+
|
285
|
+
ary
|
286
|
+
ensure
|
287
|
+
STDOUT.sync = sync unless sync.nil?
|
288
|
+
end
|
289
|
+
|
290
|
+
#
|
291
|
+
# Returns the time used to execute the given block as a
|
292
|
+
# Benchmark::Tms object.
|
293
|
+
#
|
294
|
+
def measure(label = "") # :yield:
|
295
|
+
t0 = Benchmark.times.retain # FIXME: it seems to me that RubyMotion needs retain
|
296
|
+
r0 = NSDate.date
|
297
|
+
yield
|
298
|
+
t1 = Benchmark.times.retain # FIXME: it seems to me that RubyMotion needs retain
|
299
|
+
r1 = NSDate.date
|
300
|
+
Benchmark::Tms.new(t1.utime - t0.utime,
|
301
|
+
t1.stime - t0.stime,
|
302
|
+
t1.cutime - t0.cutime,
|
303
|
+
t1.cstime - t0.cstime,
|
304
|
+
r1.timeIntervalSinceDate(r0).to_f,
|
305
|
+
label)
|
306
|
+
end
|
307
|
+
|
308
|
+
#
|
309
|
+
# Returns the elapsed real time used to execute the given block.
|
310
|
+
#
|
311
|
+
def realtime(&blk) # :yield:
|
312
|
+
r0 = Time.now
|
313
|
+
yield
|
314
|
+
r1 = Time.now
|
315
|
+
r1.to_f - r0.to_f
|
316
|
+
end
|
317
|
+
|
318
|
+
|
319
|
+
|
320
|
+
#
|
321
|
+
# A Job is a sequence of labelled blocks to be processed by the
|
322
|
+
# Benchmark.bmbm method. It is of little direct interest to the user.
|
323
|
+
#
|
324
|
+
class Job # :nodoc:
|
325
|
+
#
|
326
|
+
# Returns an initialized Job instance.
|
327
|
+
# Usually, one doesn't call this method directly, as new
|
328
|
+
# Job objects are created by the #bmbm method.
|
329
|
+
# _width_ is a initial value for the label offset used in formatting;
|
330
|
+
# the #bmbm method passes its _width_ argument to this constructor.
|
331
|
+
#
|
332
|
+
def initialize(width)
|
333
|
+
@width = width
|
334
|
+
@list = []
|
335
|
+
end
|
336
|
+
|
337
|
+
#
|
338
|
+
# Registers the given label and block pair in the job list.
|
339
|
+
#
|
340
|
+
def item(label = "", &blk) # :yield:
|
341
|
+
raise ArgumentError, "no block" unless block_given?
|
342
|
+
label += ' '
|
343
|
+
w = label.length
|
344
|
+
@width = w if @width < w
|
345
|
+
@list.push [label, blk]
|
346
|
+
self
|
347
|
+
end
|
348
|
+
|
349
|
+
alias report item
|
350
|
+
|
351
|
+
# An array of 2-element arrays, consisting of label and block pairs.
|
352
|
+
attr_reader :list
|
353
|
+
|
354
|
+
# Length of the widest label in the #list, plus one.
|
355
|
+
attr_reader :width
|
356
|
+
end
|
357
|
+
|
358
|
+
module_function :benchmark, :measure, :realtime, :bm, :bmbm
|
359
|
+
|
360
|
+
|
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(&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
|
+
arg0 ? Kernel::format(fmtstr, *args) : fmtstr
|
512
|
+
end
|
513
|
+
|
514
|
+
#
|
515
|
+
# Same as #format.
|
516
|
+
#
|
517
|
+
def to_s
|
518
|
+
format
|
519
|
+
end
|
520
|
+
|
521
|
+
#
|
522
|
+
# Returns a new 6-element array, consisting of the
|
523
|
+
# label, user CPU time, system CPU time, children's
|
524
|
+
# user CPU time, children's system CPU time and elapsed
|
525
|
+
# real time.
|
526
|
+
#
|
527
|
+
def to_a
|
528
|
+
[@label, @utime, @stime, @cutime, @cstime, @real]
|
529
|
+
end
|
530
|
+
|
531
|
+
protected
|
532
|
+
def memberwise(op, x)
|
533
|
+
case x
|
534
|
+
when Benchmark::Tms
|
535
|
+
Benchmark::Tms.new(utime.__send__(op, x.utime),
|
536
|
+
stime.__send__(op, x.stime),
|
537
|
+
cutime.__send__(op, x.cutime),
|
538
|
+
cstime.__send__(op, x.cstime),
|
539
|
+
real.__send__(op, x.real)
|
540
|
+
)
|
541
|
+
else
|
542
|
+
Benchmark::Tms.new(utime.__send__(op, x),
|
543
|
+
stime.__send__(op, x),
|
544
|
+
cutime.__send__(op, x),
|
545
|
+
cstime.__send__(op, x),
|
546
|
+
real.__send__(op, x)
|
547
|
+
)
|
548
|
+
end
|
549
|
+
end
|
550
|
+
end
|
551
|
+
|
552
|
+
# The default caption string (heading above the output times).
|
553
|
+
CAPTION = Benchmark::Tms::CAPTION
|
554
|
+
|
555
|
+
# The default format string used to display times. See also Benchmark::Tms#format.
|
556
|
+
FMTSTR = Benchmark::Tms::FMTSTR
|
557
|
+
end
|
558
|
+
|
559
|
+
if __FILE__ == $0
|
560
|
+
include Benchmark
|
561
|
+
|
562
|
+
n = ARGV[0].to_i.nonzero? || 50000
|
563
|
+
puts %Q([#{n} times iterations of `a = "1"'])
|
564
|
+
benchmark(" " + CAPTION, 7, FMTSTR) do |x|
|
565
|
+
x.report("for:") {for i in 1..n; a = "1"; end} # Benchmark::measure
|
566
|
+
x.report("times:") {n.times do ; a = "1"; end}
|
567
|
+
x.report("upto:") {1.upto(n) do ; a = "1"; end}
|
568
|
+
end
|
569
|
+
|
570
|
+
benchmark do
|
571
|
+
[
|
572
|
+
measure{for i in 1..n; a = "1"; end}, # Benchmark::measure
|
573
|
+
measure{n.times do ; a = "1"; end},
|
574
|
+
measure{1.upto(n) do ; a = "1"; end}
|
575
|
+
]
|
576
|
+
end
|
577
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: motion-benchmark
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Watson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Benchmark library for RubyMotion
|
15
|
+
email: watson1978@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- lib/motion-benchmark.rb
|
23
|
+
- lib/benchmark.rb
|
24
|
+
homepage: http://github.com/watson1978/motion-benchmark
|
25
|
+
licenses: []
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.23
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: Benchmark library for RubyMotion
|
48
|
+
test_files: []
|