I have this trial timer code to time euler solutions in Ruby.
$RUNS = 12
def run(solve)
times = []
$RUNS.times do
start_t = Time.now.usec
solve.call
end_t = Time.now.usec
times << (end_t - start_t)/1000.0
end
#times = times.delete_if {|i| i < 0}
puts times.inspect
times.sort
mean = times.inject{|a,c| a+c} / $RUNS
puts("Mean:\t#{mean}");
if (times.length % 2 == 0) then
median = (times[times.length / 2 - 1] + times[times.length / 2]) / 2.0
else
median = times[times.length / 2];
end
puts("Median: #{median}");
end
Unfortunately, I keep getting answers like this:
[409.805, 418.16, -582.23, 402.223, -581.94, 413.196, 426.816, -584.732, 519.457, -569.557, 558.918, -579.176]
What can I do to avoid these strange negative numbers?
usecreturns the microseconds from the time in the same was asmonthreturns the month. It is not the number of microseconds for the given time since the epoch.So if
start_twas 1049896564.259970 seconds andend_twas 1049896592.123130 seconds then you would get 123130 – 259970 if you subtracted the usecs. i.e. a negative number.Instead you could use
Time.now.to_fto convert to floating point number of seconds since epoch and subtract those from each other. You can also just subtract oneTimeobject from another directly e.g.