Skip to content

Commit 77a355c

Browse files
committed
Use @belapsed for performance measurements
Add BenchmarkTools as test dependency. Run performance tests for fast arithmetic for one second each to avoid spurious regressions.
1 parent 66f23a3 commit 77a355c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ UnicodePlots = "2, 3"
2828
julia = "1.6"
2929

3030
[extras]
31+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
32+
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
3133
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3234
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
3335
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
34-
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
3536
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
3637

3738
[targets]
38-
test = ["FillArrays", "Random", "StableRNGs", "Test", "UnicodePlots"]
39+
test = ["BenchmarkTools", "FillArrays", "Random", "StableRNGs", "Test", "UnicodePlots"]

test/arithmetic.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
module TestArithmetic
22

33
using Test
4+
import BenchmarkTools: @belapsed
45
using CategoricalDistributions
56
using StableRNGs
67
rng = StableRNG(123)
78

9+
macro belapsed1(ex)
10+
:(@belapsed $ex seconds=1 evals=1)
11+
end
12+
813
L = ["yes", "no"]
914
d1 = UnivariateFinite(L, rand(rng, 2), pool=missing)
1015
d2 = UnivariateFinite(L, rand(rng, 2), pool=missing)
@@ -46,27 +51,27 @@ fast = UnivariateFinite(L, P, pool=missing);
4651

4752
@testset "performant arithmetic for UnivariateFiniteArray" begin
4853
@test pdf(slow + slow, L) == pdf(fast + fast, L)
49-
t_slow = @elapsed @eval slow + slow
50-
t_fast = @elapsed @eval fast + fast
54+
t_slow = @belapsed1 $slow + $slow
55+
t_fast = @belapsed1 $fast + $fast
5156
@test t_slow/t_fast > 10
5257

5358
@test pdf(slow - slow, L) == pdf(fast - fast, L)
54-
t_slow = @elapsed @eval slow - slow
55-
t_fast = @elapsed @eval fast - fast
59+
t_slow = @belapsed1 $slow - $slow
60+
t_fast = @belapsed1 $fast - $fast
5661
@test t_slow/t_fast > 10
5762

5863
@test pdf(42*slow, L) == pdf(42*fast, L)
5964
@test pdf(slow*42, L) == pdf(fast*42, L)
60-
t_slow = @elapsed @eval 42*slow
61-
t_fast = @elapsed @eval 42*fast
65+
t_slow = @belapsed1 42*$slow
66+
t_fast = @belapsed1 42*$fast
6267
@test t_slow/t_fast > 10
63-
t_slow = @elapsed @eval slow*42
64-
t_fast = @elapsed @eval fast*42
68+
t_slow = @belapsed1 $slow*42
69+
t_fast = @belapsed1 $fast*42
6570
@test t_slow/t_fast > 10
6671

6772
@test pdf(slow/42, L) == pdf(fast/42, L)
68-
t_slow = @elapsed @eval slow/42
69-
t_fast = @elapsed @eval fast/42
73+
t_slow = @belapsed1 $slow/42
74+
t_fast = @belapsed1 $fast/42
7075
@test t_slow/t_fast > 10
7176
end
7277

0 commit comments

Comments
 (0)