Print average when benchmarking (#3762)

This commit is contained in:
Hubert Plociniczak 2022-10-06 01:21:26 +02:00 committed by GitHub
parent d201959a36
commit 67526ea8eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from Standard.Base import all
import Standard.Base.System
import Standard.Base.Runtime.Ref
## Measure the amount of time it takes to execute a given computation.
@ -20,6 +21,7 @@ import Standard.Base.System
Bench.measure Examples.get_boolean "foo" iter_size=2 num_iters=1
measure : Any -> Text -> Integer -> Integer -> Nothing
measure = ~act -> label -> iter_size -> num_iters ->
result = Ref.new 0
single_call = _ ->
x1 = System.nano_time
Runtime.no_inline act
@ -30,8 +32,12 @@ measure = ~act -> label -> iter_size -> num_iters ->
res = iter_size.times single_call
avg = avg_list res
fmt = (avg / 1000000).format "%.2f"
result.put (result.get + avg)
IO.println (label + "/iteration:" + act_it_num.to_text + ": " + fmt + "ms")
num_iters.times iteration
fmt_avg = (result.get / (1000000*num_iters)).format "%.2f"
IO.println (label + " average: " + fmt_avg + "ms")
## PRIVATE

View File

@ -32,6 +32,7 @@ main =
Bench.measure ((random_vec.drop (Last 20)).sum) "Drop Last 20 and Sum" iter_size num_iterations
Bench.measure (random_vec.filter (x -> x % 3 == 1)) "Filter" iter_size num_iterations
Bench.measure (random_vec.filter_with_index (i->x -> (i+x) % 3 == 1)) "Filter With Index" iter_size num_iterations
Bench.measure (random_vec . map (x -> x + random_gen.nextLong) . filter (x -> x % 3 == 1)) "Map & Filter" iter_size num_iterations
Bench.measure (random_vec.partition (x -> x % 3 == 1)) "Partition" iter_size num_iterations
Bench.measure (random_vec.partition_with_index (i->x -> (i+x) % 3 == 1)) "Partition With Index" iter_size num_iterations