W3cubDocs

/Ruby 3

class Benchmark::Tms

Parent:
Object

A data object, representing the times associated with a benchmark measurement.

Constants

CAPTION

Default caption, see also Benchmark::CAPTION

FORMAT

Default format string, see also Benchmark::FORMAT

Attributes

cstime[R]

System CPU time of children

cutime[R]

User CPU time of children

label[R]

Label

real[R]

Elapsed real time

stime[R]

System CPU time

total[R]

Total time, that is utime + stime + cutime + cstime

utime[R]

User CPU time

Public Class Methods

new(utime = 0.0, stime = 0.0, cutime = 0.0, cstime = 0.0, real = 0.0, label = nil) Show source
# File lib/benchmark.rb, line 427
def initialize(utime = 0.0, stime = 0.0, cutime = 0.0, cstime = 0.0, real = 0.0, label = nil)
  @utime, @stime, @cutime, @cstime, @real, @label = utime, stime, cutime, cstime, real, label.to_s
  @total = @utime + @stime + @cutime + @cstime
end

Returns an initialized Tms object which has utime as the user CPU time, stime as the system CPU time, cutime as the children's user CPU time, cstime as the children's system CPU time, real as the elapsed real time and label as the label.

Public Instance Methods

# File lib/benchmark.rb, line 475
def *(x); memberwise(:*, x) end

Returns a new Tms object obtained by memberwise multiplication of the individual times for this Tms object by x.

+(other) Show source
# File lib/benchmark.rb, line 462
def +(other); memberwise(:+, other) end

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.

-(other) Show source
# File lib/benchmark.rb, line 469
def -(other); memberwise(:-, other) end

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 482
def /(x); memberwise(:/, x) end

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.

add() { || ... } Show source
# File lib/benchmark.rb, line 436
def add(&blk) # :yield:
  self + Benchmark.measure(&blk)
end

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) Show source
# File lib/benchmark.rb, line 446
def add!(&blk)
  t = Benchmark.measure(&blk)
  @utime  = utime + t.utime
  @stime  = stime + t.stime
  @cutime = cutime + t.cutime
  @cstime = cstime + t.cstime
  @real   = real + t.real
  self
end

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) Show source
# File lib/benchmark.rb, line 501
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

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 Tms#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 Tms#cutime

%Y

Replaced by the children's system CPU time, as reported by Tms#cstime

%t

Replaced by the total CPU time, as reported by Tms#total

%r

Replaced by the elapsed real time, as reported by Tms#real

%n

Replaced by the label string, as reported by Tms#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.

to_a() Show source
# File lib/benchmark.rb, line 526
def to_a
  [@label, @utime, @stime, @cutime, @cstime, @real]
end

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_s() Show source
# File lib/benchmark.rb, line 516
def to_s
  format
end

Same as format.

Protected Instance Methods

memberwise(op, x) Show source
# File lib/benchmark.rb, line 540
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

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 +, -, *, /

Ruby Core © 1993–2020 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.