module Benchmark::Timing
Perform caclulations on Timing
results.
Constants
- MICROSECONDS_PER_SECOND
Microseconds per second.
Public Class Methods
Add one second to the time represenetation
# File lib/benchmark/timing.rb, line 54 def self.add_second(t, s) t + (s * MICROSECONDS_PER_SECOND) end
Recycle used objects by starting Garbage Collector.
# File lib/benchmark/timing.rb, line 35 def self.clean_env # rbx if GC.respond_to? :run GC.run(true) else GC.start end end
Calculate (arithmetic) mean of given samples. @param [Array] samples Samples to calculate mean. @return [Float] Mean of given samples.
# File lib/benchmark/timing.rb, line 10 def self.mean(samples) sum = samples.inject(:+) sum / samples.size end
Get an object that represents now and can be converted to microseconds
# File lib/benchmark/timing.rb, line 49 def self.now Process.clock_gettime Process::CLOCK_MONOTONIC, :float_microsecond end
Calculate standard deviation of given samples. @param [Array] samples Samples to calculate standard deviation. @param [Float] m Optional mean (Expected value). @return [Float] standard deviation of given samples.
# File lib/benchmark/timing.rb, line 30 def self.stddev(samples, m=nil) Math.sqrt variance(samples, m) end
Return the number of microseconds between the 2 moments
# File lib/benchmark/timing.rb, line 59 def self.time_us(before, after) after - before end
Calculate variance of given samples. @param [Float] m Optional mean (Expected value). @return [Float] Variance of given samples.
# File lib/benchmark/timing.rb, line 18 def self.variance(samples, m=nil) m ||= mean(samples) total = samples.inject(0) { |acc, i| acc + ((i - m) ** 2) } total / samples.size end