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