From 00d6fbe4a317bbbe8fc1dffffa0761364e5d531a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Sun, 22 Oct 2023 17:12:04 +0000 Subject: [PATCH 1/5] Default to median time, when for allocations --- src/execution.jl | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/execution.jl b/src/execution.jl index a031e652..82add1a2 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -639,12 +639,14 @@ returning the value of the expression. Unlike `@time`, it uses the `@benchmark` macro, and accepts all of the same additional parameters as `@benchmark`. The printed time -is the *minimum* elapsed time measured during the benchmark. +is the *minimum* elapsed time measured during +the benchmark, unless there are allocations then +*median* as the main valuable timing. """ macro btime(args...) _, params = prunekwargs(args...) bench, trial, result = gensym(), gensym(), gensym() - trialmin, trialallocs = gensym(), gensym() + trialmin, trialmed, trialallocs = gensym(), gensym(), gensym() tune_phase = hasevals(params) ? :() : :($BenchmarkTools.tune!($bench)) return esc( quote @@ -653,18 +655,29 @@ macro btime(args...) $tune_phase local $trial, $result = $BenchmarkTools.run_result($bench) local $trialmin = $BenchmarkTools.minimum($trial) + local $trialmed = $BenchmarkTools.median($trial) local $trialallocs = $BenchmarkTools.allocs($trialmin) - println( - " ", - $BenchmarkTools.prettytime($BenchmarkTools.time($trialmin)), - " (", - $trialallocs, - " allocation", - $trialallocs == 1 ? "" : "s", - ": ", - $BenchmarkTools.prettymemory($BenchmarkTools.memory($trialmin)), - ")", - ) + if $trialallocs == 0 + println( + " ", + $BenchmarkTools.prettytime($BenchmarkTools.time($trialmin)), + " (minimum time; 0 allocations)", + ) + else + println( + " ", + $BenchmarkTools.prettytime($BenchmarkTools.time($trialmed)), + " (median time; miniumum is ", + $BenchmarkTools.prettytime($BenchmarkTools.time($trialmin)), + ", ", + $trialallocs, + " allocation", + $trialallocs == 1 ? "" : "s", + ": ", + $BenchmarkTools.prettymemory($BenchmarkTools.memory($trialmin)), + ")", + ) + end $result end, ) From 85d787e11f415a3fd80eaad64c5dfa40f5ca6ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Sun, 22 Oct 2023 18:26:23 +0000 Subject: [PATCH 2/5] Show mean for allocations --- src/execution.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/execution.jl b/src/execution.jl index 82add1a2..972187f1 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -641,12 +641,12 @@ macro, and accepts all of the same additional parameters as `@benchmark`. The printed time is the *minimum* elapsed time measured during the benchmark, unless there are allocations then -*median* as the main valuable timing. +*mean* as the main valuable timing. """ macro btime(args...) _, params = prunekwargs(args...) bench, trial, result = gensym(), gensym(), gensym() - trialmin, trialmed, trialallocs = gensym(), gensym(), gensym() + trialmin, trialmean, trialallocs = gensym(), gensym(), gensym() tune_phase = hasevals(params) ? :() : :($BenchmarkTools.tune!($bench)) return esc( quote @@ -655,7 +655,7 @@ macro btime(args...) $tune_phase local $trial, $result = $BenchmarkTools.run_result($bench) local $trialmin = $BenchmarkTools.minimum($trial) - local $trialmed = $BenchmarkTools.median($trial) + local $trialmed = $BenchmarkTools.mean($trial) local $trialallocs = $BenchmarkTools.allocs($trialmin) if $trialallocs == 0 println( @@ -667,7 +667,7 @@ macro btime(args...) println( " ", $BenchmarkTools.prettytime($BenchmarkTools.time($trialmed)), - " (median time; miniumum is ", + " (mean time; miniumum is ", $BenchmarkTools.prettytime($BenchmarkTools.time($trialmin)), ", ", $trialallocs, From 45438d266752eb45d69c891d46d865b8c8c41666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Sun, 22 Oct 2023 18:32:06 +0000 Subject: [PATCH 3/5] Fix to mean --- src/execution.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/execution.jl b/src/execution.jl index 972187f1..9c5e4850 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -655,7 +655,7 @@ macro btime(args...) $tune_phase local $trial, $result = $BenchmarkTools.run_result($bench) local $trialmin = $BenchmarkTools.minimum($trial) - local $trialmed = $BenchmarkTools.mean($trial) + local $trialmean = $BenchmarkTools.mean($trial) local $trialallocs = $BenchmarkTools.allocs($trialmin) if $trialallocs == 0 println( From 8b3cbb61530699935c93cd10e426459b2bf19d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Sun, 22 Oct 2023 18:41:20 +0000 Subject: [PATCH 4/5] Fix to mean --- src/execution.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/execution.jl b/src/execution.jl index 9c5e4850..6e29225a 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -666,7 +666,7 @@ macro btime(args...) else println( " ", - $BenchmarkTools.prettytime($BenchmarkTools.time($trialmed)), + $BenchmarkTools.prettytime($BenchmarkTools.time($trialmean)), " (mean time; miniumum is ", $BenchmarkTools.prettytime($BenchmarkTools.time($trialmin)), ", ", From c055dca931a5e9c45eefb478f2b984596d1dc00b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Mon, 23 Oct 2023 13:08:01 +0000 Subject: [PATCH 5/5] Update test --- test/ExecutionTests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ExecutionTests.jl b/test/ExecutionTests.jl index 235794ff..b296a64c 100644 --- a/test/ExecutionTests.jl +++ b/test/ExecutionTests.jl @@ -333,7 +333,7 @@ let fname = tempname() end s = read(fname, String) try - @test occursin(r"[0-9.]+ \w*s \([0-9]* allocations?: [0-9]+ bytes\)", s) + @test occursin(r"[0-9.]+ \w*s \(minimum time; [0-9]* allocations\)", s) catch println(stderr, "@btime output didn't match ", repr(s)) rethrow()