Time a function: Difference between revisions
Content added Content deleted
(Add Seed7 example) |
No edit summary |
||
Line 984: | Line 984: | ||
You can get the total time as a number for later processing like this: |
You can get the total time as a number for later processing like this: |
||
<lang ruby>Benchmark.measure { whatever }.total</lang> |
<lang ruby>Benchmark.measure { whatever }.total</lang> |
||
=={{header|Scala}}== |
|||
Define a <code>time</code> function that returns the elapsed time (in ms) to execute a block of code. |
|||
<lang scala> |
|||
def time(f: => Unit)={ |
|||
val s = System.currentTimeMillis |
|||
f |
|||
System.currentTimeMillis - s |
|||
} |
|||
</lang> |
|||
Can be called with a code block: |
|||
<lang scala> |
|||
println(time { |
|||
for(i <- 1 to 10000000) {} |
|||
}) |
|||
</lang> |
|||
Or with a function: |
|||
<lang scala> |
|||
def count(i:Int) = for(j <- 1 to i){} |
|||
println(time (count(10000000))) |
|||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |