Class: Benchmark::Tms

Inherits:
Object show all
Defined in:
opal/stdlib/benchmark.rb

Overview

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

Constant Summary collapse

CAPTION =

Default caption, see also Benchmark::CAPTION

"      user     system      total        real\n"
FORMAT =

Default format string, see also Benchmark::FORMAT

"%10.6u %10.6y %10.6t %10.6r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(utime = 0.0, stime = 0.0, cutime = 0.0, cstime = 0.0, real = 0.0, label = nil) ⇒ Tms

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.



422
423
424
425
# File 'opal/stdlib/benchmark.rb', line 422

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

Instance Attribute Details

#cstimeObject (readonly)

System CPU time of children



405
406
407
# File 'opal/stdlib/benchmark.rb', line 405

def cstime
  @cstime
end

#cutimeObject (readonly)

User CPU time of children



402
403
404
# File 'opal/stdlib/benchmark.rb', line 402

def cutime
  @cutime
end

#labelObject (readonly)

Label



414
415
416
# File 'opal/stdlib/benchmark.rb', line 414

def label
  @label
end

#realObject (readonly)

Elapsed real time



408
409
410
# File 'opal/stdlib/benchmark.rb', line 408

def real
  @real
end

#stimeObject (readonly)

System CPU time



399
400
401
# File 'opal/stdlib/benchmark.rb', line 399

def stime
  @stime
end

#totalObject (readonly)

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



411
412
413
# File 'opal/stdlib/benchmark.rb', line 411

def total
  @total
end

#utimeObject (readonly)

User CPU time



396
397
398
# File 'opal/stdlib/benchmark.rb', line 396

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.



467
# File 'opal/stdlib/benchmark.rb', line 467

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.



454
# File 'opal/stdlib/benchmark.rb', line 454

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.



461
# File 'opal/stdlib/benchmark.rb', line 461

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.



474
# File 'opal/stdlib/benchmark.rb', line 474

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+).



431
432
433
# File 'opal/stdlib/benchmark.rb', line 431

def add(&blk) # :yield:
  self + Benchmark.measure(&blk)
end

#add!(&blk) ⇒ Object

An in-place version of #add.



438
439
440
441
442
443
444
445
446
# File 'opal/stdlib/benchmark.rb', line 438

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

#format(format = 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 format is not given, FORMAT is used as default value, detailing the user, system and real elapsed time.



493
494
495
496
497
498
499
500
501
502
503
# File 'opal/stdlib/benchmark.rb', line 493

def format(format = nil, *args)
  str = (format || FORMAT).dup
  .gsub(/(%[-+.\d]*)n/) { "#{$1}s" % label }
  .gsub(/(%[-+.\d]*)u/) { "#{$1}f" % utime }
  .gsub(/(%[-+.\d]*)y/) { "#{$1}f" % stime }
  .gsub(/(%[-+.\d]*)U/) { "#{$1}f" % cutime }
  .gsub(/(%[-+.\d]*)Y/) { "#{$1}f" % cstime }
  .gsub(/(%[-+.\d]*)t/) { "#{$1}f" % total }
  .gsub(/(%[-+.\d]*)r/) { "(#{$1}f)" % real }
  format ? str % args : str
end

#to_aObject

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.



518
519
520
# File 'opal/stdlib/benchmark.rb', line 518

def to_a
  [@label, @utime, @stime, @cutime, @cstime, @real]
end

#to_hObject

Returns a hash containing the same data as to_a.



525
526
527
528
529
530
531
532
533
534
# File 'opal/stdlib/benchmark.rb', line 525

def to_h
  {
    label:  @label,
    utime:  @utime,
    stime:  @stime,
    cutime: @cutime,
    cstime: @cstime,
    real:   @real
  }
end

#to_sObject

Same as #format.



508
509
510
# File 'opal/stdlib/benchmark.rb', line 508

def to_s
  format
end