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