Class: Benchmark::Tms
- Inherits:
-
Object
- Object
- Benchmark::Tms
- Defined in:
- lib/benchmark.rb,
lib/railsbench/benchmark.rb
Overview
A data object, representing the times associated with a benchmark measurement.
Constant Summary collapse
- CAPTION =
" user system total real\n"
- FMTSTR =
"%10.6u %10.6y %10.6t %10.6r\n"
Instance Attribute Summary collapse
-
#cstime ⇒ Object
readonly
System CPU time of children.
-
#cutime ⇒ Object
readonly
User CPU time of children.
-
#label ⇒ Object
readonly
Label.
-
#real ⇒ Object
readonly
Elapsed real time.
-
#stime ⇒ Object
readonly
System CPU time.
-
#total ⇒ Object
readonly
Total time, that is utime + stime + cutime + cstime.
-
#utime ⇒ Object
readonly
User CPU time.
Instance Method Summary collapse
-
#*(x) ⇒ Object
Returns a new Tms object obtained by memberwise multiplication of the individual times for this Tms object by x.
-
#+(other) ⇒ Object
Returns a new Tms object obtained by memberwise summation of the individual times for this Tms object with those of the other Tms object.
-
#-(other) ⇒ Object
Returns a new Tms object obtained by memberwise subtraction of the individual times for the other Tms object from those of this Tms object.
-
#/(x) ⇒ Object
Returns a new Tms object obtained by memberwise division of the individual times for this Tms object by x.
-
#add(&blk) ⇒ Object
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! ⇒ Object
An in-place version of #add.
-
#format(arg0 = nil, *args) ⇒ Object
Returns the contents of this Tms object as a formatted string, according to a format string like that passed to Kernel.format.
-
#initialize(u = 0.0, s = 0.0, cu = 0.0, cs = 0.0, real = 0.0, l = nil) ⇒ Tms
constructor
Returns an initialized Tms object which has u as the user CPU time, s as the system CPU time, cu as the children’s user CPU time, cs as the children’s system CPU time, real as the elapsed real time and l as the label.
-
#to_a ⇒ Object
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 ⇒ Object
Same as #format.
Constructor Details
#initialize(u = 0.0, s = 0.0, cu = 0.0, cs = 0.0, real = 0.0, l = nil) ⇒ Tms
Returns an initialized Tms object which has u as the user CPU time, s as the system CPU time, cu as the children’s user CPU time, cs as the children’s system CPU time, real as the elapsed real time and l as the label.
430 431 432 433 |
# File 'lib/benchmark.rb', line 430 def initialize(u = 0.0, s = 0.0, cu = 0.0, cs = 0.0, real = 0.0, l = nil) @utime, @stime, @cutime, @cstime, @real, @label = u, s, cu, cs, real, l @total = @utime + @stime + @cutime + @cstime end |
Instance Attribute Details
#cstime ⇒ Object (readonly)
System CPU time of children
412 413 414 |
# File 'lib/benchmark.rb', line 412 def cstime @cstime end |
#cutime ⇒ Object (readonly)
User CPU time of children
409 410 411 |
# File 'lib/benchmark.rb', line 409 def cutime @cutime end |
#label ⇒ Object (readonly)
Label
421 422 423 |
# File 'lib/benchmark.rb', line 421 def label @label end |
#real ⇒ Object (readonly)
Elapsed real time
415 416 417 |
# File 'lib/benchmark.rb', line 415 def real @real end |
#stime ⇒ Object (readonly)
System CPU time
406 407 408 |
# File 'lib/benchmark.rb', line 406 def stime @stime end |
#total ⇒ Object (readonly)
Total time, that is utime + stime + cutime + cstime
418 419 420 |
# File 'lib/benchmark.rb', line 418 def total @total end |
#utime ⇒ Object (readonly)
User CPU time
403 404 405 |
# File 'lib/benchmark.rb', line 403 def utime @utime end |
Instance Method Details
#*(x) ⇒ Object
Returns a new Tms object obtained by memberwise multiplication of the individual times for this Tms object by x.
475 |
# File 'lib/benchmark.rb', line 475 def *(x); memberwise(:*, x) end |
#+(other) ⇒ Object
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.
462 |
# File 'lib/benchmark.rb', line 462 def +(other); memberwise(:+, other) end |
#-(other) ⇒ Object
Returns a new Tms object obtained by memberwise subtraction of the individual times for the other Tms object from those of this Tms object.
469 |
# File 'lib/benchmark.rb', line 469 def -(other); memberwise(:-, other) end |
#/(x) ⇒ Object
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.
482 |
# File 'lib/benchmark.rb', line 482 def /(x); memberwise(:/, x) end |
#add(&blk) ⇒ Object
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).
439 440 441 |
# File 'lib/benchmark.rb', line 439 def add(&blk) # :yield: self + Benchmark::measure(&blk) end |
#add! ⇒ Object
An in-place version of #add.
446 447 448 449 450 451 452 453 454 |
# File 'lib/benchmark.rb', line 446 def add! 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 |
#format(arg0 = nil, *args) ⇒ Object
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 fmtstr is not given, FMTSTR is used as default value, detailing the user, system and real elapsed time.
501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/benchmark.rb', line 501 def format(arg0 = nil, *args) fmtstr = (arg0 || FMTSTR).dup fmtstr.gsub!(/(%[-+\.\d]*)n/){"#{$1}s" % label} fmtstr.gsub!(/(%[-+\.\d]*)u/){"#{$1}f" % utime} fmtstr.gsub!(/(%[-+\.\d]*)y/){"#{$1}f" % stime} fmtstr.gsub!(/(%[-+\.\d]*)U/){"#{$1}f" % cutime} fmtstr.gsub!(/(%[-+\.\d]*)Y/){"#{$1}f" % cstime} fmtstr.gsub!(/(%[-+\.\d]*)t/){"#{$1}f" % total} fmtstr.gsub!(/(%[-+\.\d]*)r/){"(#{$1}f)" % real} arg0 ? Kernel::format(fmtstr, *args) : fmtstr end |
#to_a ⇒ Object
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.
526 527 528 |
# File 'lib/benchmark.rb', line 526 def to_a [@label, @utime, @stime, @cutime, @cstime, @real] end |
#to_s ⇒ Object
Same as #format.
516 517 518 |
# File 'lib/benchmark.rb', line 516 def to_s format end |