| 1 |
|
| 2 |
import time |
| 3 |
import pyperf |
| 4 |
|
| 5 |
from sympy import expand, symbols, integrate, tan, summation |
| 6 |
from sympy.core.cache import clear_cache |
| 7 |
|
| 8 |
|
| 9 |
def bench_expand(): |
| 10 |
x, y, z = symbols("x y z") |
| 11 |
expand((1 + x + y + z) ** 20) |
| 12 |
|
| 13 |
|
| 14 |
def bench_integrate(): |
| 15 |
x, y = symbols("x y") |
| 16 |
f = (1 / tan(x)) ** 10 |
| 17 |
return integrate(f, x) |
| 18 |
|
| 19 |
|
| 20 |
def bench_sum(): |
| 21 |
x, i = symbols("x i") |
| 22 |
summation(x**i / i, (i, 1, 400)) |
| 23 |
|
| 24 |
|
| 25 |
def bench_str(): |
| 26 |
x, y, z = symbols("x y z") |
| 27 |
str(expand((x + 2 * y + 3 * z) ** 30)) |
| 28 |
|
| 29 |
|
| 30 |
def bench_sympy(loops, func): |
| 31 |
timer = pyperf.perf_counter |
| 32 |
dt = 0 |
| 33 |
|
| 34 |
for _ in range(loops): |
| 35 |
|
| 36 |
clear_cache() |
| 37 |
|
| 38 |
t0 = timer() |
| 39 |
func() |
| 40 |
dt += timer() - t0 |
| 41 |
|
| 42 |
return dt |
| 43 |
|
| 44 |
|
| 45 |
BENCHMARKS = ("expand", "integrate", "sum", "str") |
| 46 |
|
| 47 |
|
| 48 |
def add_cmdline_args(cmd, args): |
| 49 |
if args.benchmark: |
| 50 |
cmd.append(args.benchmark) |
| 51 |
|
| 52 |
|
| 53 |
if __name__ == "__main__": |
| 54 |
|
| 55 |
|
| 56 |
import gc |
| 57 |
|
| 58 |
gc.disable() |
| 59 |
|
| 60 |
start_p = time.perf_counter() |
| 61 |
for _ in range(25): |
| 62 |
|
| 63 |
clear_cache() |
| 64 |
bench_expand() |
| 65 |
|
| 66 |
stop_p = time.perf_counter() |
| 67 |
print("Time elapsed: ", stop_p - start_p) |