Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: benchmark.rb
=begin
[0] Fix | Delete
#
[1] Fix | Delete
# benchmark.rb - a performance benchmarking library
[2] Fix | Delete
#
[3] Fix | Delete
# $Id: benchmark.rb 15425 2008-02-10 15:24:56Z naruse $
[4] Fix | Delete
#
[5] Fix | Delete
# Created by Gotoken (gotoken@notwork.org).
[6] Fix | Delete
#
[7] Fix | Delete
# Documentation by Gotoken (original RD), Lyle Johnson (RDoc conversion), and
[8] Fix | Delete
# Gavin Sinclair (editing).
[9] Fix | Delete
#
[10] Fix | Delete
=end
[11] Fix | Delete
[12] Fix | Delete
# == Overview
[13] Fix | Delete
#
[14] Fix | Delete
# The Benchmark module provides methods for benchmarking Ruby code, giving
[15] Fix | Delete
# detailed reports on the time taken for each task.
[16] Fix | Delete
#
[17] Fix | Delete
[18] Fix | Delete
# The Benchmark module provides methods to measure and report the time
[19] Fix | Delete
# used to execute Ruby code.
[20] Fix | Delete
#
[21] Fix | Delete
# * Measure the time to construct the string given by the expression
[22] Fix | Delete
# <tt>"a"*1_000_000</tt>:
[23] Fix | Delete
#
[24] Fix | Delete
# require 'benchmark'
[25] Fix | Delete
#
[26] Fix | Delete
# puts Benchmark.measure { "a"*1_000_000 }
[27] Fix | Delete
#
[28] Fix | Delete
# On my machine (FreeBSD 3.2 on P5, 100MHz) this generates:
[29] Fix | Delete
#
[30] Fix | Delete
# 1.166667 0.050000 1.216667 ( 0.571355)
[31] Fix | Delete
#
[32] Fix | Delete
# This report shows the user CPU time, system CPU time, the sum of
[33] Fix | Delete
# the user and system CPU times, and the elapsed real time. The unit
[34] Fix | Delete
# of time is seconds.
[35] Fix | Delete
#
[36] Fix | Delete
# * Do some experiments sequentially using the #bm method:
[37] Fix | Delete
#
[38] Fix | Delete
# require 'benchmark'
[39] Fix | Delete
#
[40] Fix | Delete
# n = 50000
[41] Fix | Delete
# Benchmark.bm do |x|
[42] Fix | Delete
# x.report { for i in 1..n; a = "1"; end }
[43] Fix | Delete
# x.report { n.times do ; a = "1"; end }
[44] Fix | Delete
# x.report { 1.upto(n) do ; a = "1"; end }
[45] Fix | Delete
# end
[46] Fix | Delete
#
[47] Fix | Delete
# The result:
[48] Fix | Delete
#
[49] Fix | Delete
# user system total real
[50] Fix | Delete
# 1.033333 0.016667 1.016667 ( 0.492106)
[51] Fix | Delete
# 1.483333 0.000000 1.483333 ( 0.694605)
[52] Fix | Delete
# 1.516667 0.000000 1.516667 ( 0.711077)
[53] Fix | Delete
#
[54] Fix | Delete
# * Continuing the previous example, put a label in each report:
[55] Fix | Delete
#
[56] Fix | Delete
# require 'benchmark'
[57] Fix | Delete
#
[58] Fix | Delete
# n = 50000
[59] Fix | Delete
# Benchmark.bm(7) do |x|
[60] Fix | Delete
# x.report("for:") { for i in 1..n; a = "1"; end }
[61] Fix | Delete
# x.report("times:") { n.times do ; a = "1"; end }
[62] Fix | Delete
# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
[63] Fix | Delete
# end
[64] Fix | Delete
#
[65] Fix | Delete
# The result:
[66] Fix | Delete
#
[67] Fix | Delete
# user system total real
[68] Fix | Delete
# for: 1.050000 0.000000 1.050000 ( 0.503462)
[69] Fix | Delete
# times: 1.533333 0.016667 1.550000 ( 0.735473)
[70] Fix | Delete
# upto: 1.500000 0.016667 1.516667 ( 0.711239)
[71] Fix | Delete
#
[72] Fix | Delete
#
[73] Fix | Delete
# * The times for some benchmarks depend on the order in which items
[74] Fix | Delete
# are run. These differences are due to the cost of memory
[75] Fix | Delete
# allocation and garbage collection. To avoid these discrepancies,
[76] Fix | Delete
# the #bmbm method is provided. For example, to compare ways to
[77] Fix | Delete
# sort an array of floats:
[78] Fix | Delete
#
[79] Fix | Delete
# require 'benchmark'
[80] Fix | Delete
#
[81] Fix | Delete
# array = (1..1000000).map { rand }
[82] Fix | Delete
#
[83] Fix | Delete
# Benchmark.bmbm do |x|
[84] Fix | Delete
# x.report("sort!") { array.dup.sort! }
[85] Fix | Delete
# x.report("sort") { array.dup.sort }
[86] Fix | Delete
# end
[87] Fix | Delete
#
[88] Fix | Delete
# The result:
[89] Fix | Delete
#
[90] Fix | Delete
# Rehearsal -----------------------------------------
[91] Fix | Delete
# sort! 11.928000 0.010000 11.938000 ( 12.756000)
[92] Fix | Delete
# sort 13.048000 0.020000 13.068000 ( 13.857000)
[93] Fix | Delete
# ------------------------------- total: 25.006000sec
[94] Fix | Delete
#
[95] Fix | Delete
# user system total real
[96] Fix | Delete
# sort! 12.959000 0.010000 12.969000 ( 13.793000)
[97] Fix | Delete
# sort 12.007000 0.000000 12.007000 ( 12.791000)
[98] Fix | Delete
#
[99] Fix | Delete
#
[100] Fix | Delete
# * Report statistics of sequential experiments with unique labels,
[101] Fix | Delete
# using the #benchmark method:
[102] Fix | Delete
#
[103] Fix | Delete
# require 'benchmark'
[104] Fix | Delete
#
[105] Fix | Delete
# n = 50000
[106] Fix | Delete
# Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
[107] Fix | Delete
# tf = x.report("for:") { for i in 1..n; a = "1"; end }
[108] Fix | Delete
# tt = x.report("times:") { n.times do ; a = "1"; end }
[109] Fix | Delete
# tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
[110] Fix | Delete
# [tf+tt+tu, (tf+tt+tu)/3]
[111] Fix | Delete
# end
[112] Fix | Delete
#
[113] Fix | Delete
# The result:
[114] Fix | Delete
#
[115] Fix | Delete
# user system total real
[116] Fix | Delete
# for: 1.016667 0.016667 1.033333 ( 0.485749)
[117] Fix | Delete
# times: 1.450000 0.016667 1.466667 ( 0.681367)
[118] Fix | Delete
# upto: 1.533333 0.000000 1.533333 ( 0.722166)
[119] Fix | Delete
# >total: 4.000000 0.033333 4.033333 ( 1.889282)
[120] Fix | Delete
# >avg: 1.333333 0.011111 1.344444 ( 0.629761)
[121] Fix | Delete
[122] Fix | Delete
module Benchmark
[123] Fix | Delete
[124] Fix | Delete
BENCHMARK_VERSION = "2002-04-25" #:nodoc"
[125] Fix | Delete
[126] Fix | Delete
def Benchmark::times() # :nodoc:
[127] Fix | Delete
Process::times()
[128] Fix | Delete
end
[129] Fix | Delete
[130] Fix | Delete
[131] Fix | Delete
# Invokes the block with a <tt>Benchmark::Report</tt> object, which
[132] Fix | Delete
# may be used to collect and report on the results of individual
[133] Fix | Delete
# benchmark tests. Reserves <i>label_width</i> leading spaces for
[134] Fix | Delete
# labels on each line. Prints _caption_ at the top of the
[135] Fix | Delete
# report, and uses _fmt_ to format each line.
[136] Fix | Delete
# If the block returns an array of
[137] Fix | Delete
# <tt>Benchmark::Tms</tt> objects, these will be used to format
[138] Fix | Delete
# additional lines of output. If _label_ parameters are
[139] Fix | Delete
# given, these are used to label these extra lines.
[140] Fix | Delete
#
[141] Fix | Delete
# _Note_: Other methods provide a simpler interface to this one, and are
[142] Fix | Delete
# suitable for nearly all benchmarking requirements. See the examples in
[143] Fix | Delete
# Benchmark, and the #bm and #bmbm methods.
[144] Fix | Delete
#
[145] Fix | Delete
# Example:
[146] Fix | Delete
#
[147] Fix | Delete
# require 'benchmark'
[148] Fix | Delete
# include Benchmark # we need the CAPTION and FMTSTR constants
[149] Fix | Delete
#
[150] Fix | Delete
# n = 50000
[151] Fix | Delete
# Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:", ">avg:") do |x|
[152] Fix | Delete
# tf = x.report("for:") { for i in 1..n; a = "1"; end }
[153] Fix | Delete
# tt = x.report("times:") { n.times do ; a = "1"; end }
[154] Fix | Delete
# tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
[155] Fix | Delete
# [tf+tt+tu, (tf+tt+tu)/3]
[156] Fix | Delete
# end
[157] Fix | Delete
#
[158] Fix | Delete
# <i>Generates:</i>
[159] Fix | Delete
#
[160] Fix | Delete
# user system total real
[161] Fix | Delete
# for: 1.016667 0.016667 1.033333 ( 0.485749)
[162] Fix | Delete
# times: 1.450000 0.016667 1.466667 ( 0.681367)
[163] Fix | Delete
# upto: 1.533333 0.000000 1.533333 ( 0.722166)
[164] Fix | Delete
# >total: 4.000000 0.033333 4.033333 ( 1.889282)
[165] Fix | Delete
# >avg: 1.333333 0.011111 1.344444 ( 0.629761)
[166] Fix | Delete
#
[167] Fix | Delete
[168] Fix | Delete
def benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) # :yield: report
[169] Fix | Delete
sync = STDOUT.sync
[170] Fix | Delete
STDOUT.sync = true
[171] Fix | Delete
label_width ||= 0
[172] Fix | Delete
fmtstr ||= FMTSTR
[173] Fix | Delete
raise ArgumentError, "no block" unless iterator?
[174] Fix | Delete
print caption
[175] Fix | Delete
results = yield(Report.new(label_width, fmtstr))
[176] Fix | Delete
Array === results and results.grep(Tms).each {|t|
[177] Fix | Delete
print((labels.shift || t.label || "").ljust(label_width),
[178] Fix | Delete
t.format(fmtstr))
[179] Fix | Delete
}
[180] Fix | Delete
STDOUT.sync = sync
[181] Fix | Delete
end
[182] Fix | Delete
[183] Fix | Delete
[184] Fix | Delete
# A simple interface to the #benchmark method, #bm is generates sequential reports
[185] Fix | Delete
# with labels. The parameters have the same meaning as for #benchmark.
[186] Fix | Delete
#
[187] Fix | Delete
# require 'benchmark'
[188] Fix | Delete
#
[189] Fix | Delete
# n = 50000
[190] Fix | Delete
# Benchmark.bm(7) do |x|
[191] Fix | Delete
# x.report("for:") { for i in 1..n; a = "1"; end }
[192] Fix | Delete
# x.report("times:") { n.times do ; a = "1"; end }
[193] Fix | Delete
# x.report("upto:") { 1.upto(n) do ; a = "1"; end }
[194] Fix | Delete
# end
[195] Fix | Delete
#
[196] Fix | Delete
# <i>Generates:</i>
[197] Fix | Delete
#
[198] Fix | Delete
# user system total real
[199] Fix | Delete
# for: 1.050000 0.000000 1.050000 ( 0.503462)
[200] Fix | Delete
# times: 1.533333 0.016667 1.550000 ( 0.735473)
[201] Fix | Delete
# upto: 1.500000 0.016667 1.516667 ( 0.711239)
[202] Fix | Delete
#
[203] Fix | Delete
[204] Fix | Delete
def bm(label_width = 0, *labels, &blk) # :yield: report
[205] Fix | Delete
benchmark(" "*label_width + CAPTION, label_width, FMTSTR, *labels, &blk)
[206] Fix | Delete
end
[207] Fix | Delete
[208] Fix | Delete
[209] Fix | Delete
# Sometimes benchmark results are skewed because code executed
[210] Fix | Delete
# earlier encounters different garbage collection overheads than
[211] Fix | Delete
# that run later. #bmbm attempts to minimize this effect by running
[212] Fix | Delete
# the tests twice, the first time as a rehearsal in order to get the
[213] Fix | Delete
# runtime environment stable, the second time for
[214] Fix | Delete
# real. <tt>GC.start</tt> is executed before the start of each of
[215] Fix | Delete
# the real timings; the cost of this is not included in the
[216] Fix | Delete
# timings. In reality, though, there's only so much that #bmbm can
[217] Fix | Delete
# do, and the results are not guaranteed to be isolated from garbage
[218] Fix | Delete
# collection and other effects.
[219] Fix | Delete
#
[220] Fix | Delete
# Because #bmbm takes two passes through the tests, it can
[221] Fix | Delete
# calculate the required label width.
[222] Fix | Delete
#
[223] Fix | Delete
# require 'benchmark'
[224] Fix | Delete
#
[225] Fix | Delete
# array = (1..1000000).map { rand }
[226] Fix | Delete
#
[227] Fix | Delete
# Benchmark.bmbm do |x|
[228] Fix | Delete
# x.report("sort!") { array.dup.sort! }
[229] Fix | Delete
# x.report("sort") { array.dup.sort }
[230] Fix | Delete
# end
[231] Fix | Delete
#
[232] Fix | Delete
# <i>Generates:</i>
[233] Fix | Delete
#
[234] Fix | Delete
# Rehearsal -----------------------------------------
[235] Fix | Delete
# sort! 11.928000 0.010000 11.938000 ( 12.756000)
[236] Fix | Delete
# sort 13.048000 0.020000 13.068000 ( 13.857000)
[237] Fix | Delete
# ------------------------------- total: 25.006000sec
[238] Fix | Delete
#
[239] Fix | Delete
# user system total real
[240] Fix | Delete
# sort! 12.959000 0.010000 12.969000 ( 13.793000)
[241] Fix | Delete
# sort 12.007000 0.000000 12.007000 ( 12.791000)
[242] Fix | Delete
#
[243] Fix | Delete
# #bmbm yields a Benchmark::Job object and returns an array of
[244] Fix | Delete
# Benchmark::Tms objects.
[245] Fix | Delete
#
[246] Fix | Delete
def bmbm(width = 0, &blk) # :yield: job
[247] Fix | Delete
job = Job.new(width)
[248] Fix | Delete
yield(job)
[249] Fix | Delete
width = job.width
[250] Fix | Delete
sync = STDOUT.sync
[251] Fix | Delete
STDOUT.sync = true
[252] Fix | Delete
[253] Fix | Delete
# rehearsal
[254] Fix | Delete
print "Rehearsal "
[255] Fix | Delete
puts '-'*(width+CAPTION.length - "Rehearsal ".length)
[256] Fix | Delete
list = []
[257] Fix | Delete
job.list.each{|label,item|
[258] Fix | Delete
print(label.ljust(width))
[259] Fix | Delete
res = Benchmark::measure(&item)
[260] Fix | Delete
print res.format()
[261] Fix | Delete
list.push res
[262] Fix | Delete
}
[263] Fix | Delete
sum = Tms.new; list.each{|i| sum += i}
[264] Fix | Delete
ets = sum.format("total: %tsec")
[265] Fix | Delete
printf("%s %s\n\n",
[266] Fix | Delete
"-"*(width+CAPTION.length-ets.length-1), ets)
[267] Fix | Delete
[268] Fix | Delete
# take
[269] Fix | Delete
print ' '*width, CAPTION
[270] Fix | Delete
list = []
[271] Fix | Delete
ary = []
[272] Fix | Delete
job.list.each{|label,item|
[273] Fix | Delete
GC::start
[274] Fix | Delete
print label.ljust(width)
[275] Fix | Delete
res = Benchmark::measure(&item)
[276] Fix | Delete
print res.format()
[277] Fix | Delete
ary.push res
[278] Fix | Delete
list.push [label, res]
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
STDOUT.sync = sync
[282] Fix | Delete
ary
[283] Fix | Delete
end
[284] Fix | Delete
[285] Fix | Delete
#
[286] Fix | Delete
# Returns the time used to execute the given block as a
[287] Fix | Delete
# Benchmark::Tms object.
[288] Fix | Delete
#
[289] Fix | Delete
def measure(label = "") # :yield:
[290] Fix | Delete
t0, r0 = Benchmark.times, Time.now
[291] Fix | Delete
yield
[292] Fix | Delete
t1, r1 = Benchmark.times, Time.now
[293] Fix | Delete
Benchmark::Tms.new(t1.utime - t0.utime,
[294] Fix | Delete
t1.stime - t0.stime,
[295] Fix | Delete
t1.cutime - t0.cutime,
[296] Fix | Delete
t1.cstime - t0.cstime,
[297] Fix | Delete
r1.to_f - r0.to_f,
[298] Fix | Delete
label)
[299] Fix | Delete
end
[300] Fix | Delete
[301] Fix | Delete
#
[302] Fix | Delete
# Returns the elapsed real time used to execute the given block.
[303] Fix | Delete
#
[304] Fix | Delete
def realtime(&blk) # :yield:
[305] Fix | Delete
r0 = Time.now
[306] Fix | Delete
yield
[307] Fix | Delete
r1 = Time.now
[308] Fix | Delete
r1.to_f - r0.to_f
[309] Fix | Delete
end
[310] Fix | Delete
[311] Fix | Delete
[312] Fix | Delete
[313] Fix | Delete
#
[314] Fix | Delete
# A Job is a sequence of labelled blocks to be processed by the
[315] Fix | Delete
# Benchmark.bmbm method. It is of little direct interest to the user.
[316] Fix | Delete
#
[317] Fix | Delete
class Job # :nodoc:
[318] Fix | Delete
#
[319] Fix | Delete
# Returns an initialized Job instance.
[320] Fix | Delete
# Usually, one doesn't call this method directly, as new
[321] Fix | Delete
# Job objects are created by the #bmbm method.
[322] Fix | Delete
# _width_ is a initial value for the label offset used in formatting;
[323] Fix | Delete
# the #bmbm method passes its _width_ argument to this constructor.
[324] Fix | Delete
#
[325] Fix | Delete
def initialize(width)
[326] Fix | Delete
@width = width
[327] Fix | Delete
@list = []
[328] Fix | Delete
end
[329] Fix | Delete
[330] Fix | Delete
#
[331] Fix | Delete
# Registers the given label and block pair in the job list.
[332] Fix | Delete
#
[333] Fix | Delete
def item(label = "", &blk) # :yield:
[334] Fix | Delete
raise ArgumentError, "no block" unless block_given?
[335] Fix | Delete
label += ' '
[336] Fix | Delete
w = label.length
[337] Fix | Delete
@width = w if @width < w
[338] Fix | Delete
@list.push [label, blk]
[339] Fix | Delete
self
[340] Fix | Delete
end
[341] Fix | Delete
[342] Fix | Delete
alias report item
[343] Fix | Delete
[344] Fix | Delete
# An array of 2-element arrays, consisting of label and block pairs.
[345] Fix | Delete
attr_reader :list
[346] Fix | Delete
[347] Fix | Delete
# Length of the widest label in the #list, plus one.
[348] Fix | Delete
attr_reader :width
[349] Fix | Delete
end
[350] Fix | Delete
[351] Fix | Delete
module_function :benchmark, :measure, :realtime, :bm, :bmbm
[352] Fix | Delete
[353] Fix | Delete
[354] Fix | Delete
[355] Fix | Delete
#
[356] Fix | Delete
# This class is used by the Benchmark.benchmark and Benchmark.bm methods.
[357] Fix | Delete
# It is of little direct interest to the user.
[358] Fix | Delete
#
[359] Fix | Delete
class Report # :nodoc:
[360] Fix | Delete
#
[361] Fix | Delete
# Returns an initialized Report instance.
[362] Fix | Delete
# Usually, one doesn't call this method directly, as new
[363] Fix | Delete
# Report objects are created by the #benchmark and #bm methods.
[364] Fix | Delete
# _width_ and _fmtstr_ are the label offset and
[365] Fix | Delete
# format string used by Tms#format.
[366] Fix | Delete
#
[367] Fix | Delete
def initialize(width = 0, fmtstr = nil)
[368] Fix | Delete
@width, @fmtstr = width, fmtstr
[369] Fix | Delete
end
[370] Fix | Delete
[371] Fix | Delete
#
[372] Fix | Delete
# Prints the _label_ and measured time for the block,
[373] Fix | Delete
# formatted by _fmt_. See Tms#format for the
[374] Fix | Delete
# formatting rules.
[375] Fix | Delete
#
[376] Fix | Delete
def item(label = "", *fmt, &blk) # :yield:
[377] Fix | Delete
print label.ljust(@width)
[378] Fix | Delete
res = Benchmark::measure(&blk)
[379] Fix | Delete
print res.format(@fmtstr, *fmt)
[380] Fix | Delete
res
[381] Fix | Delete
end
[382] Fix | Delete
[383] Fix | Delete
alias report item
[384] Fix | Delete
end
[385] Fix | Delete
[386] Fix | Delete
[387] Fix | Delete
[388] Fix | Delete
#
[389] Fix | Delete
# A data object, representing the times associated with a benchmark
[390] Fix | Delete
# measurement.
[391] Fix | Delete
#
[392] Fix | Delete
class Tms
[393] Fix | Delete
CAPTION = " user system total real\n"
[394] Fix | Delete
FMTSTR = "%10.6u %10.6y %10.6t %10.6r\n"
[395] Fix | Delete
[396] Fix | Delete
# User CPU time
[397] Fix | Delete
attr_reader :utime
[398] Fix | Delete
[399] Fix | Delete
# System CPU time
[400] Fix | Delete
attr_reader :stime
[401] Fix | Delete
[402] Fix | Delete
# User CPU time of children
[403] Fix | Delete
attr_reader :cutime
[404] Fix | Delete
[405] Fix | Delete
# System CPU time of children
[406] Fix | Delete
attr_reader :cstime
[407] Fix | Delete
[408] Fix | Delete
# Elapsed real time
[409] Fix | Delete
attr_reader :real
[410] Fix | Delete
[411] Fix | Delete
# Total time, that is _utime_ + _stime_ + _cutime_ + _cstime_
[412] Fix | Delete
attr_reader :total
[413] Fix | Delete
[414] Fix | Delete
# Label
[415] Fix | Delete
attr_reader :label
[416] Fix | Delete
[417] Fix | Delete
#
[418] Fix | Delete
# Returns an initialized Tms object which has
[419] Fix | Delete
# _u_ as the user CPU time, _s_ as the system CPU time,
[420] Fix | Delete
# _cu_ as the children's user CPU time, _cs_ as the children's
[421] Fix | Delete
# system CPU time, _real_ as the elapsed real time and _l_
[422] Fix | Delete
# as the label.
[423] Fix | Delete
#
[424] Fix | Delete
def initialize(u = 0.0, s = 0.0, cu = 0.0, cs = 0.0, real = 0.0, l = nil)
[425] Fix | Delete
@utime, @stime, @cutime, @cstime, @real, @label = u, s, cu, cs, real, l
[426] Fix | Delete
@total = @utime + @stime + @cutime + @cstime
[427] Fix | Delete
end
[428] Fix | Delete
[429] Fix | Delete
#
[430] Fix | Delete
# Returns a new Tms object whose times are the sum of the times for this
[431] Fix | Delete
# Tms object, plus the time required to execute the code block (_blk_).
[432] Fix | Delete
#
[433] Fix | Delete
def add(&blk) # :yield:
[434] Fix | Delete
self + Benchmark::measure(&blk)
[435] Fix | Delete
end
[436] Fix | Delete
[437] Fix | Delete
#
[438] Fix | Delete
# An in-place version of #add.
[439] Fix | Delete
#
[440] Fix | Delete
def add!
[441] Fix | Delete
t = Benchmark::measure(&blk)
[442] Fix | Delete
@utime = utime + t.utime
[443] Fix | Delete
@stime = stime + t.stime
[444] Fix | Delete
@cutime = cutime + t.cutime
[445] Fix | Delete
@cstime = cstime + t.cstime
[446] Fix | Delete
@real = real + t.real
[447] Fix | Delete
self
[448] Fix | Delete
end
[449] Fix | Delete
[450] Fix | Delete
#
[451] Fix | Delete
# Returns a new Tms object obtained by memberwise summation
[452] Fix | Delete
# of the individual times for this Tms object with those of the other
[453] Fix | Delete
# Tms object.
[454] Fix | Delete
# This method and #/() are useful for taking statistics.
[455] Fix | Delete
#
[456] Fix | Delete
def +(other); memberwise(:+, other) end
[457] Fix | Delete
[458] Fix | Delete
#
[459] Fix | Delete
# Returns a new Tms object obtained by memberwise subtraction
[460] Fix | Delete
# of the individual times for the other Tms object from those of this
[461] Fix | Delete
# Tms object.
[462] Fix | Delete
#
[463] Fix | Delete
def -(other); memberwise(:-, other) end
[464] Fix | Delete
[465] Fix | Delete
#
[466] Fix | Delete
# Returns a new Tms object obtained by memberwise multiplication
[467] Fix | Delete
# of the individual times for this Tms object by _x_.
[468] Fix | Delete
#
[469] Fix | Delete
def *(x); memberwise(:*, x) end
[470] Fix | Delete
[471] Fix | Delete
#
[472] Fix | Delete
# Returns a new Tms object obtained by memberwise division
[473] Fix | Delete
# of the individual times for this Tms object by _x_.
[474] Fix | Delete
# This method and #+() are useful for taking statistics.
[475] Fix | Delete
#
[476] Fix | Delete
def /(x); memberwise(:/, x) end
[477] Fix | Delete
[478] Fix | Delete
#
[479] Fix | Delete
# Returns the contents of this Tms object as
[480] Fix | Delete
# a formatted string, according to a format string
[481] Fix | Delete
# like that passed to Kernel.format. In addition, #format
[482] Fix | Delete
# accepts the following extensions:
[483] Fix | Delete
#
[484] Fix | Delete
# <tt>%u</tt>:: Replaced by the user CPU time, as reported by Tms#utime.
[485] Fix | Delete
# <tt>%y</tt>:: Replaced by the system CPU time, as reported by #stime (Mnemonic: y of "s*y*stem")
[486] Fix | Delete
# <tt>%U</tt>:: Replaced by the children's user CPU time, as reported by Tms#cutime
[487] Fix | Delete
# <tt>%Y</tt>:: Replaced by the children's system CPU time, as reported by Tms#cstime
[488] Fix | Delete
# <tt>%t</tt>:: Replaced by the total CPU time, as reported by Tms#total
[489] Fix | Delete
# <tt>%r</tt>:: Replaced by the elapsed real time, as reported by Tms#real
[490] Fix | Delete
# <tt>%n</tt>:: Replaced by the label string, as reported by Tms#label (Mnemonic: n of "*n*ame")
[491] Fix | Delete
#
[492] Fix | Delete
# If _fmtstr_ is not given, FMTSTR is used as default value, detailing the
[493] Fix | Delete
# user, system and real elapsed time.
[494] Fix | Delete
#
[495] Fix | Delete
def format(arg0 = nil, *args)
[496] Fix | Delete
fmtstr = (arg0 || FMTSTR).dup
[497] Fix | Delete
fmtstr.gsub!(/(%[-+\.\d]*)n/){"#{$1}s" % label}
[498] Fix | Delete
fmtstr.gsub!(/(%[-+\.\d]*)u/){"#{$1}f" % utime}
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function