Time a function: Difference between revisions
Content added Content deleted
No edit summary |
(added ruby) |
||
Line 217: | Line 217: | ||
['2.7', '2.8', '31.4', '38.1', '58.0', '76.2', '100.5', '130.0', '149.3', '180.0'] |
['2.7', '2.8', '31.4', '38.1', '58.0', '76.2', '100.5', '130.0', '149.3', '180.0'] |
||
using ''qsort()'' from [[Quicksort]]. Timings show that the implementation of ''qsort()'' has quadratic dependence on sequence length ''N'' for already sorted sequences (instead of ''O(N*log(N))'' in average). |
using ''qsort()'' from [[Quicksort]]. Timings show that the implementation of ''qsort()'' has quadratic dependence on sequence length ''N'' for already sorted sequences (instead of ''O(N*log(N))'' in average). |
||
=={{header|Ruby}}== |
|||
Ruby's Benchmark module provides a way to generate nice reports (numbers are in seconds): |
|||
<ruby>require 'benchmark' |
|||
Benchmark.bm(8) do |x| |
|||
x.report("nothing:") { } |
|||
x.report("sum:") { (1..1_000_000).inject(4) {|sum, x| sum + x} } |
|||
end</ruby> |
|||
Output: |
|||
<pre> |
|||
user system total real |
|||
nothing: 0.000000 0.000000 0.000000 ( 0.000014) |
|||
sum: 2.700000 0.400000 3.100000 ( 3.258348) |
|||
</pre> |
|||
You can get the total time as a number for later processing like this: |
|||
<ruby>Benchmark.measure { whatever }.total</ruby> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |