Class: Benchmark::Tms
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/benchmark.rb |
Overview
A data object, representing the times associated with a benchmark measurement.
Constant Summary
-
CAPTION =
Default caption, see also CAPTION
" user system total real\n"
-
FORMAT =
Default format string, see also FORMAT
"%10.6u %10.6y %10.6t %10.6r\n"
Class Method Summary
Instance Attribute Summary
Instance Method Summary
-
#*(x)
Returns a new
Tms
object obtained by memberwise multiplication of the individual times for thisTms
object byx
. -
#+(other)
Returns a new
Tms
object obtained by memberwise summation of the individual times for thisTms
object with those of theother
Tms
object. -
#-(other)
Returns a new
Tms
object obtained by memberwise subtraction of the individual times for theother
Tms
object from those of thisTms
object. -
#/(x)
Returns a new
Tms
object obtained by memberwise division of the individual times for thisTms
object byx
. -
#add(&blk)
Returns a new
Tms
object whose times are the sum of the times for thisTms
object, plus the time required to execute the code block (blk
). -
#add!(&blk)
An in-place version of #add.
-
#format(format = nil, *args)
Returns the contents of this
Tms
object as a formatted string, according to a #format string like that passed toKernel.format
. -
#to_a
Returns a new 6-element array, consisting of the label, user CPU time, system CPU time, children’s user CPU time, children’s system CPU time and elapsed real time.
-
#to_h
Returns a hash containing the same data as #to_a.
-
#to_s
Same as #format.
-
#memberwise(op, x)
protected
Returns a new
Tms
object obtained by memberwise operationop
of the individual times for thisTms
object with those of the otherTms
object (x
).
Constructor Details
.new(utime = 0.0, stime = 0.0, cutime = 0.0, cstime = 0.0, real = 0.0, label = nil) ⇒ Tms
Instance Attribute Details
#cstime (readonly)
System CPU time of children
# File 'lib/benchmark.rb', line 425
attr_reader :cstime
#cutime (readonly)
User CPU time of children
# File 'lib/benchmark.rb', line 422
attr_reader :cutime
#label (readonly)
Label
# File 'lib/benchmark.rb', line 434
attr_reader :label
#real (readonly)
Elapsed real time
# File 'lib/benchmark.rb', line 428
attr_reader :real
#stime (readonly)
System CPU time
# File 'lib/benchmark.rb', line 419
attr_reader :stime
#total (readonly)
[ GitHub ]# File 'lib/benchmark.rb', line 431
attr_reader :total
#utime (readonly)
User CPU time
# File 'lib/benchmark.rb', line 416
attr_reader :utime
Instance Method Details
#*(x)
Returns a new Tms
object obtained by memberwise multiplication of the individual times for this Tms
object by x
.
# File 'lib/benchmark.rb', line 490
def *(x); memberwise(:*, x) end
#+(other)
Returns a new Tms
object obtained by memberwise summation of the individual times for this Tms
object with those of the other
Tms
object. This method and #/() are useful for taking statistics.
# File 'lib/benchmark.rb', line 477
def +(other); memberwise(:+, other) end
#-(other)
Returns a new Tms
object obtained by memberwise subtraction of the individual times for the other
Tms
object from those of this Tms
object.
# File 'lib/benchmark.rb', line 484
def -(other); memberwise(:-, other) end
#/(x)
Returns a new Tms
object obtained by memberwise division of the individual times for this Tms
object by x
. This method and #+() are useful for taking statistics.
# File 'lib/benchmark.rb', line 497
def /(x); memberwise(:/, x) end
#add(&blk)
Returns a new Tms
object whose times are the sum of the times for this Tms
object, plus the time required to execute the code block (blk
).
#add!(&blk)
An in-place version of #add. Changes the times of this Tms
object by making it the sum of the times for this Tms
object, plus the time required to execute the code block (blk
).
#format(format = nil, *args)
Returns the contents of this Tms
object as a formatted string, according to a format
string like that passed to Kernel.format
. In addition, #format
accepts the following extensions:
%u
-
Replaced by the user CPU time, as reported by #utime.
%y
-
Replaced by the system CPU time, as reported by #stime (Mnemonic: y of “s*y*stem”)
%U
-
Replaced by the children’s user CPU time, as reported by #cutime
%Y
-
Replaced by the children’s system CPU time, as reported by #cstime
%t
-
Replaced by the total CPU time, as reported by #total
%r
-
Replaced by the elapsed real time, as reported by #real
%n
-
Replaced by the label string, as reported by #label (Mnemonic: n of “*n*ame”)
If format
is not given, FORMAT is used as default value, detailing the user, system and real elapsed time.
# File 'lib/benchmark.rb', line 516
def format(format = nil, *args) str = (format || FORMAT).dup str.gsub!(/(%[-+.\d]*)n/) { "#{$1}s" % label } str.gsub!(/(%[-+.\d]*)u/) { "#{$1}f" % utime } str.gsub!(/(%[-+.\d]*)y/) { "#{$1}f" % stime } str.gsub!(/(%[-+.\d]*)U/) { "#{$1}f" % cutime } str.gsub!(/(%[-+.\d]*)Y/) { "#{$1}f" % cstime } str.gsub!(/(%[-+.\d]*)t/) { "#{$1}f" % total } str.gsub!(/(%[-+.\d]*)r/) { "(#{$1}f)" % real } format ? str % args : str end
#memberwise(op, x) (protected)
Returns a new Tms
object obtained by memberwise operation op
of the individual times for this Tms
object with those of the other Tms
object (x
).
op
can be a mathematical operation such as +
, -
, *
, /
# File 'lib/benchmark.rb', line 569
def memberwise(op, x) case x when Benchmark::Tms Benchmark::Tms.new(utime.__send__(op, x.utime), stime.__send__(op, x.stime), cutime.__send__(op, x.cutime), cstime.__send__(op, x.cstime), real.__send__(op, x.real) ) else Benchmark::Tms.new(utime.__send__(op, x), stime.__send__(op, x), cutime.__send__(op, x), cstime.__send__(op, x), real.__send__(op, x) ) end end
#to_a
Returns a new 6-element array, consisting of the label, user CPU time, system CPU time, children’s user CPU time, children’s system CPU time and elapsed real time.
# File 'lib/benchmark.rb', line 541
def to_a [@label, @utime, @stime, @cutime, @cstime, @real] end
#to_h
Returns a hash containing the same data as #to_a.
# File 'lib/benchmark.rb', line 548
def to_h { label: @label, utime: @utime, stime: @stime, cutime: @cutime, cstime: @cstime, real: @real } end
#to_s
Same as #format.
# File 'lib/benchmark.rb', line 531
def to_s format end