Skip to content

Commit 1d8e0e2

Browse files
committed
improve benchmarks
1 parent dec2de2 commit 1d8e0e2

4 files changed

Lines changed: 87 additions & 41 deletions

File tree

benchmark/baseline_v6.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

benchmark/benchmarks.jl

Lines changed: 87 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,101 +19,122 @@ using QuestBase:
1919
power_of,
2020
substitute_all,
2121
is_harmonic,
22+
is_function,
23+
count_derivatives,
24+
add_div,
2225
DifferentialEquation,
2326
HarmonicVariable,
24-
d
25-
using Symbolics: Symbolics, @variables, unwrap, expand, Num, Equation
27+
HarmonicEquation,
28+
add_harmonic!,
29+
rearrange_standard!,
30+
d,
31+
var_name
32+
using Symbolics: Symbolics, @variables, unwrap, expand, Num, Equation, Differential
2633
using SymbolicUtils: BasicSymbolic
2734

2835
const SUITE = BenchmarkGroup()
2936

3037
# --- Setup variables ---
3138
@variables t x(t) y(t) ω0 ω F k a b c f θ
39+
D = Differential(t)
3240

33-
# --- Symbolic utilities ---
41+
# ==========================================================================
42+
# Symbolic utilities
43+
# ==========================================================================
3444
SUITE["symbolic_utils"] = BenchmarkGroup()
3545

36-
expr_add = a + b + c
37-
expr_mul = a * b * c
38-
expr_div = a / b
3946
expr_nested = (a + b * c)^2 + (a - c) / b
4047
expr_complex = a + b * cos(f * t) + c * sin(f * t)
4148

42-
SUITE["symbolic_utils"]["expand_all_simple"] = @benchmarkable expand_all($expr_add)
4349
SUITE["symbolic_utils"]["expand_all_nested"] = @benchmarkable expand_all($expr_nested)
4450
SUITE["symbolic_utils"]["expand_all_trig"] = @benchmarkable expand_all($expr_complex)
4551

4652
SUITE["symbolic_utils"]["expand_fraction"] = @benchmarkable expand_fraction(
47-
$(unwrap((a + b) / c))
53+
$(unwrap((a^2 + b * c + a * b^2 + c^3) / (a + b)))
4854
)
55+
4956
SUITE["symbolic_utils"]["_apply_termwise_add"] = @benchmarkable _apply_termwise(
50-
identity, $(unwrap(expr_add))
57+
simplify_complex, $(unwrap(a^2 + b * c + a * b^2 + c^3 + a * b * c))
5158
)
5259
SUITE["symbolic_utils"]["_apply_termwise_mul"] = @benchmarkable _apply_termwise(
53-
identity, $(unwrap(expr_mul))
60+
simplify_complex, $(unwrap(a * b * c * (a + b) * (b + c)))
5461
)
5562
SUITE["symbolic_utils"]["_apply_termwise_div"] = @benchmarkable _apply_termwise(
56-
identity, $(unwrap(expr_div))
63+
simplify_complex, $(unwrap((a^2 + b * c + c^3) / (a * b + c^2)))
5764
)
5865

5966
SUITE["symbolic_utils"]["simplify_complex_add"] = @benchmarkable simplify_complex(
60-
$(unwrap(Complex{Num}(a + b, 0 * a)))
67+
$(unwrap(Complex{Num}(a^2 + b * c, 0 * a)))
6168
)
6269
SUITE["symbolic_utils"]["simplify_complex_real"] = @benchmarkable simplify_complex(
63-
$(Complex{Num}(a, 0 * a))
70+
$(Complex{Num}(a^2 + b * c, 0 * a))
6471
)
6572

6673
SUITE["symbolic_utils"]["get_independent_simple"] = @benchmarkable get_independent(
67-
$a + $b + $c, $t
74+
$(a^2 + b * c + a * b), $t
6875
)
6976
SUITE["symbolic_utils"]["get_independent_trig"] = @benchmarkable get_independent(
70-
$(cos(f * t)^2 + a + b), $t
77+
$(cos(f * t)^2 + a * sin(f * t) + b), $t
7178
)
7279

73-
SUITE["symbolic_utils"]["get_all_terms_add"] = @benchmarkable get_all_terms($expr_add)
7480
SUITE["symbolic_utils"]["get_all_terms_nested"] = @benchmarkable get_all_terms($expr_nested)
7581

76-
# --- Exponentials ---
82+
SUITE["symbolic_utils"]["is_function_true"] = @benchmarkable is_function(
83+
$(cos(f * t)^2 + a), $t
84+
)
85+
SUITE["symbolic_utils"]["is_function_false"] = @benchmarkable is_function(
86+
$(a^2 + b * c), $t
87+
)
88+
89+
SUITE["symbolic_utils"]["count_derivatives_0"] = @benchmarkable count_derivatives($x)
90+
SUITE["symbolic_utils"]["count_derivatives_2"] = @benchmarkable count_derivatives(
91+
$(d(d(x, t), t))
92+
)
93+
94+
SUITE["symbolic_utils"]["var_name"] = @benchmarkable var_name($x)
95+
96+
# ==========================================================================
97+
# Exponentials
98+
# ==========================================================================
7799
SUITE["exponentials"] = BenchmarkGroup()
78100

79101
SUITE["exponentials"]["expand_exp_power"] = @benchmarkable expand_exp_power(
80-
$(unwrap(exp(a)^3))
102+
$(unwrap(exp(a)^3 + exp(b)^2 + a * exp(c)^4))
81103
)
82-
SUITE["exponentials"]["expand_exp_power_nested"] = @benchmarkable expand_exp_power(
83-
$(unwrap(exp(a)^3 + exp(b)^2))
84-
)
85-
86104
SUITE["exponentials"]["simplify_exp_products"] = @benchmarkable simplify_exp_products(
87-
$(unwrap(exp(a) * exp(b)))
88-
)
89-
SUITE["exponentials"]["simplify_exp_products_multi"] = @benchmarkable simplify_exp_products(
90-
$(unwrap(exp(3a) * exp(4b)))
105+
$(unwrap(exp(3a) * exp(4b) + exp(a) * exp(c)))
91106
)
92107

93-
# --- Fourier ---
108+
# ==========================================================================
109+
# Fourier
110+
# ==========================================================================
94111
SUITE["fourier"] = BenchmarkGroup()
95112

96-
trig_expr_simple = cos(f * t)
97113
trig_expr_sq = cos(f * t)^2
98114
trig_expr_product =
99115
(a * sin(f * t) + b * cos(f * t)) *
100116
(a * sin(2 * f * t) + b * cos(2 * f * t)) *
101117
(a * sin(3 * f * t) + b * cos(3 * f * t))
102118
trig_expr_hard = (a + b * cos(f * t + θ)^2)^3 * sin(f * t)
103119

104-
SUITE["fourier"]["trig_to_exp_simple"] = @benchmarkable trig_to_exp($trig_expr_simple)
105120
SUITE["fourier"]["trig_to_exp_sq"] = @benchmarkable trig_to_exp($trig_expr_sq)
121+
SUITE["fourier"]["trig_to_exp_product"] = @benchmarkable trig_to_exp($trig_expr_product)
122+
123+
exp_sum = unwrap(exp(im * a) + exp(-im * a) + exp(im * (a + b)))
124+
SUITE["fourier"]["exp_to_trig_sum"] = @benchmarkable exp_to_trig($exp_sum)
106125

107-
SUITE["fourier"]["exp_to_trig"] = @benchmarkable exp_to_trig($(unwrap(exp(im * a))))
126+
SUITE["fourier"]["add_div"] = @benchmarkable add_div(
127+
$(a / (b + c) + b / (a + c))
128+
)
108129

109130
SUITE["fourier"]["trig_reduce_simple"] = @benchmarkable trig_reduce($trig_expr_sq)
110131
SUITE["fourier"]["trig_reduce_product"] = @benchmarkable trig_reduce($trig_expr_product)
111132

112133
SUITE["fourier"]["fourier_cos_term_simple"] = @benchmarkable fourier_cos_term(
113-
$trig_expr_simple, $f, $t
134+
$(cos(f * t)), $f, $t
114135
)
115136
SUITE["fourier"]["fourier_cos_term_sq"] = @benchmarkable fourier_cos_term(
116-
$(cos(f * t)^2), $(2f), $t
137+
$trig_expr_sq, $(2f), $t
117138
)
118139
SUITE["fourier"]["fourier_sin_term_simple"] = @benchmarkable fourier_sin_term(
119140
$(sin(f * t)), $f, $t
@@ -125,23 +146,28 @@ SUITE["fourier"]["fourier_cos_term_hard"] = @benchmarkable fourier_cos_term(
125146
$trig_expr_hard, $f, $t
126147
)
127148

128-
# --- Powers ---
149+
# ==========================================================================
150+
# Powers
151+
# ==========================================================================
129152
SUITE["powers"] = BenchmarkGroup()
130153

131154
SUITE["powers"]["max_power_simple"] = @benchmarkable max_power($(a^2 + b), $a)
132155
SUITE["powers"]["max_power_nested"] = @benchmarkable max_power($(a * ((a + b)^4)^2 + a), $a)
133156
SUITE["powers"]["power_of_pow"] = @benchmarkable power_of($(unwrap(a^3)), $(unwrap(a)))
134-
SUITE["powers"]["drop_powers_simple"] = @benchmarkable drop_powers($(a^2 + b), $a, 1)
157+
SUITE["powers"]["drop_powers_single_var"] = @benchmarkable drop_powers(
158+
$((a + b)^3 + a^2 * b + a * b^2), $a, 2
159+
)
135160
SUITE["powers"]["drop_powers_multi"] = @benchmarkable drop_powers($((a + b)^2), [$a, $b], 1)
136161
SUITE["powers"]["drop_powers_high"] = @benchmarkable drop_powers(
137162
$((a + b)^3 + (a + b)^5), [$a, $b], 4
138163
)
139164

140-
# --- Substitution ---
165+
# ==========================================================================
166+
# Substitution
167+
# ==========================================================================
141168
SUITE["substitution"] = BenchmarkGroup()
142169

143170
@variables d_var e_var f_var g_var h_var
144-
rules_small = Dict(a => b)
145171
rules_medium = Dict(a => b, c => d_var, e_var => f_var)
146172
expr_sub = a + b + c + d_var + e_var + f_var
147173

@@ -154,8 +180,16 @@ SUITE["substitution"]["substitute_all_3rules"] = @benchmarkable substitute_all(
154180
SUITE["substitution"]["substitute_all_vector"] = @benchmarkable substitute_all(
155181
[$a, $c, $e_var], $rules_medium
156182
)
183+
SUITE["substitution"]["substitute_all_no_deriv"] = @benchmarkable substitute_all(
184+
$(a^2 + b * c), $(Dict(a => b)); include_derivatives=false
185+
)
186+
SUITE["substitution"]["substitute_all_with_deriv"] = @benchmarkable substitute_all(
187+
$(D(x) + a * x), $(Dict(x => 2x, a => b))
188+
)
157189

158-
# --- Construction ---
190+
# ==========================================================================
191+
# Construction and rearrangement
192+
# ==========================================================================
159193
SUITE["construction"] = BenchmarkGroup()
160194

161195
SUITE["construction"]["DifferentialEquation_single"] = @benchmarkable DifferentialEquation(
@@ -167,8 +201,23 @@ SUITE["construction"]["DifferentialEquation_coupled"] = @benchmarkable Different
167201
[$x, $y],
168202
)
169203

170-
# --- Harmonic checks ---
204+
# Rearrangement benchmark
205+
diff_eq_for_rearrange = DifferentialEquation(
206+
[d(x, t, 2) + ω0^2 * x - k * y ~ F * cos* t),
207+
d(y, t, 2) + ω0^2 * y - k * x ~ 0],
208+
[x, y],
209+
)
210+
SUITE["construction"]["rearrange_standard"] = @benchmarkable rearrange_standard!(
211+
eom
212+
) setup = (eom = deepcopy($diff_eq_for_rearrange))
213+
214+
# ==========================================================================
215+
# Harmonic checks
216+
# ==========================================================================
171217
SUITE["harmonic"] = BenchmarkGroup()
172218

173219
SUITE["harmonic"]["is_harmonic_true"] = @benchmarkable is_harmonic($(cos(f * t)), $t)
174220
SUITE["harmonic"]["is_harmonic_false"] = @benchmarkable is_harmonic($(cos(f * t^2 + a)), $t)
221+
SUITE["harmonic"]["is_harmonic_complex"] = @benchmarkable is_harmonic(
222+
$(a * cos(f * t) + b * sin(2f * t) + c), $t
223+
)

benchmark/results_v7.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

benchmark/results_v7_improved.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)