@@ -7,6 +7,17 @@ SUITE = BenchmarkGroup()
77
88@syms a b c d x y[1 : 3 ] z[1 : 2 , 1 : 2 ]; Random. seed! (123 );
99
10+ function random_term (len; atoms, funs, fallback_atom= 1 )
11+ xs = rand (atoms, len)
12+ while length (xs) > 1
13+ xs = map (Iterators. partition (xs, 2 )) do xy
14+ x = xy[1 ]; y = get (xy, 2 , fallback_atom)
15+ rand (funs)(x, y)
16+ end
17+ end
18+ xs[]
19+ end
20+
1021let r = @rule (~ x => ~ x), rs = RuleSet ([r]),
1122 acr = @rule (~ x:: is_literal_number + ~ y => ~ y)
1223
@@ -34,16 +45,6 @@ let r = @rule(~x => ~x), rs = RuleSet([r]),
3445 overhead[" simplify" ][" noop:Sym" ] = @benchmarkable simplify ($ a)
3546 overhead[" simplify" ][" noop:Term" ] = @benchmarkable simplify ($ (a+ 2 ))
3647
37- function random_term (len; atoms, funs, fallback_atom= 1 )
38- xs = rand (atoms, len)
39- while length (xs) > 1
40- xs = map (Iterators. partition (xs, 2 )) do xy
41- x = xy[1 ]; y = get (xy, 2 , fallback_atom)
42- rand (funs)(x, y)
43- end
44- end
45- xs[]
46- end
4748 ex1 = random_term (1000 , atoms= [a, b, c, d, a^ (- 1 ), b^ (- 1 ), 1 , 2.0 ], funs= [+ , * ])
4849 ex2 = random_term (1000 , atoms= [a, b, c, d, a^ (- 1 ), b^ (- 1 ), 1 , 2.0 ], funs= [/ , * ])
4950
@@ -55,15 +56,17 @@ let r = @rule(~x => ~x), rs = RuleSet([r]),
5556 overhead[" substitute" ] = BenchmarkGroup ()
5657
5758
58- overhead[" substitute" ][" a" ] = @benchmarkable substitute (subs_expr, $ (Dict (a=> 1 ))) setup= begin
59+ # we use `fold = false` since otherwise it dynamic dispatches to `sin`/`cos` whenever
60+ # both arguments in the contained addition are substituted.
61+ overhead[" substitute" ][" a" ] = @benchmarkable substitute (subs_expr, $ (Dict (a=> 1 )); fold = false ) setup= begin
5962 subs_expr = (sin (a+ b) + cos (b+ c)) * (sin (b+ c) + cos (c+ a)) * (sin (c+ a) + cos (a+ b))
6063 end
6164
62- overhead[" substitute" ][" a,b" ] = @benchmarkable substitute (subs_expr, $ (Dict (a=> 1 , b=> 2 ))) setup= begin
65+ overhead[" substitute" ][" a,b" ] = @benchmarkable substitute (subs_expr, $ (Dict (a=> 1 , b=> 2 )); fold = false ) setup= begin
6366 subs_expr = (sin (a+ b) + cos (b+ c)) * (sin (b+ c) + cos (c+ a)) * (sin (c+ a) + cos (a+ b))
6467 end
6568
66- overhead[" substitute" ][" a,b,c" ] = @benchmarkable substitute (subs_expr, $ (Dict (a=> 1 , b=> 2 , c=> 3 ))) setup= begin
69+ overhead[" substitute" ][" a,b,c" ] = @benchmarkable substitute (subs_expr, $ (Dict (a=> 1 , b=> 2 , c=> 3 )); fold = false ) setup= begin
6770 subs_expr = (sin (a+ b) + cos (b+ c)) * (sin (b+ c) + cos (c+ a)) * (sin (c+ a) + cos (a+ b))
6871 end
6972
9497 (- f* (g + (- d* g) / d)) / (i + (- c* (h + (- e* g) / d)) / b + (- f* g) / d)) / d
9598 pform[" simplify_fractions" ] = @benchmarkable simplify_fractions ($ ex)
9699 pform[" iszero" ] = @benchmarkable SymbolicUtils. fraction_iszero ($ ex)
97- pform[" isone" ] = @benchmarkable SymbolicUtils. fraction_isone ($ o)
100+ pform[" isone" ] = @benchmarkable SymbolicUtils. fraction_isone ($ ex)
101+ pform[" isone:noop" ] = @benchmarkable SymbolicUtils. fraction_isone ($ o)
102+ pform[" iszero:noop" ] = @benchmarkable SymbolicUtils. fraction_iszero ($ o)
98103 pform[" easy_iszero" ] = @benchmarkable SymbolicUtils. fraction_iszero ($ ((b* (h + (- e* g) / d)) / b + (e* g) / d - h))
99104end
105+
106+ let
107+ arith = SUITE[" arithmetic" ] = BenchmarkGroup ()
108+ atoms = [a, b, c, d, a^ 2 , b^ 2 , a^ 1.5 , (b + c), b^ c, 1 , 2.0 ]
109+ funs = [+ , * ]
110+ exs = [random_term (5 ; atoms, funs) for _ in 1 : 50 ]
111+ @static if isdefined (SymbolicUtils, :SymReal )
112+ arith[" addition" ] = @benchmarkable SymbolicUtils. add_worker (SymReal, $ exs)
113+ elseif isdefined (SymbolicUtils, :add_worker )
114+ arith[" addition" ] = @benchmarkable SymbolicUtils. add_worker ($ exs)
115+ else
116+ exs = Tuple (exs)
117+ arith[" addition" ] = @benchmarkable + ($ (exs). .. )
118+ end
119+
120+ funs = [* , / ]
121+ exs = [random_term (5 ; atoms, funs) for _ in 1 : 50 ]
122+ @static if isdefined (SymbolicUtils, :SymReal )
123+ arith[" multiplication" ] = @benchmarkable SymbolicUtils. mul_worker (SymReal, $ exs)
124+ elseif isdefined (SymbolicUtils, :mul_worker )
125+ arith[" multiplication" ] = @benchmarkable SymbolicUtils. mul_worker ($ exs)
126+ else
127+ exs = Tuple (exs)
128+ arith[" multiplication" ] = @benchmarkable * ($ (exs). .. )
129+ end
130+
131+ ex1 = random_term (50 ; atoms, funs)
132+ ex2 = random_term (50 ; atoms, funs)
133+ arith[" division" ] = @benchmarkable $ ex1 / $ ex2
134+ end
0 commit comments