bench_symbench.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. from sympy.core.random import random
  3. from sympy.core.numbers import (I, Integer, pi)
  4. from sympy.core.symbol import Symbol
  5. from sympy.core.sympify import sympify
  6. from sympy.functions.elementary.miscellaneous import sqrt
  7. from sympy.functions.elementary.trigonometric import sin
  8. from sympy.polys.polytools import factor
  9. from sympy.simplify.simplify import simplify
  10. from sympy.abc import x, y, z
  11. from timeit import default_timer as clock
  12. def bench_R1():
  13. "real(f(f(f(f(f(f(f(f(f(f(i/2)))))))))))"
  14. def f(z):
  15. return sqrt(Integer(1)/3)*z**2 + I/3
  16. f(f(f(f(f(f(f(f(f(f(I/2)))))))))).as_real_imag()[0]
  17. def bench_R2():
  18. "Hermite polynomial hermite(15, y)"
  19. def hermite(n, y):
  20. if n == 1:
  21. return 2*y
  22. if n == 0:
  23. return 1
  24. return (2*y*hermite(n - 1, y) - 2*(n - 1)*hermite(n - 2, y)).expand()
  25. hermite(15, y)
  26. def bench_R3():
  27. "a = [bool(f==f) for _ in range(10)]"
  28. f = x + y + z
  29. [bool(f == f) for _ in range(10)]
  30. def bench_R4():
  31. # we don't have Tuples
  32. pass
  33. def bench_R5():
  34. "blowup(L, 8); L=uniq(L)"
  35. def blowup(L, n):
  36. for i in range(n):
  37. L.append( (L[i] + L[i + 1]) * L[i + 2] )
  38. def uniq(x):
  39. v = set(x)
  40. return v
  41. L = [x, y, z]
  42. blowup(L, 8)
  43. L = uniq(L)
  44. def bench_R6():
  45. "sum(simplify((x+sin(i))/x+(x-sin(i))/x) for i in range(100))"
  46. sum(simplify((x + sin(i))/x + (x - sin(i))/x) for i in range(100))
  47. def bench_R7():
  48. "[f.subs(x, random()) for _ in range(10**4)]"
  49. f = x**24 + 34*x**12 + 45*x**3 + 9*x**18 + 34*x**10 + 32*x**21
  50. [f.subs(x, random()) for _ in range(10**4)]
  51. def bench_R8():
  52. "right(x^2,0,5,10^4)"
  53. def right(f, a, b, n):
  54. a = sympify(a)
  55. b = sympify(b)
  56. n = sympify(n)
  57. x = f.atoms(Symbol).pop()
  58. Deltax = (b - a)/n
  59. c = a
  60. est = 0
  61. for i in range(n):
  62. c += Deltax
  63. est += f.subs(x, c)
  64. return est*Deltax
  65. right(x**2, 0, 5, 10**4)
  66. def _bench_R9():
  67. "factor(x^20 - pi^5*y^20)"
  68. factor(x**20 - pi**5*y**20)
  69. def bench_R10():
  70. "v = [-pi,-pi+1/10..,pi]"
  71. def srange(min, max, step):
  72. v = [min]
  73. while (max - v[-1]).evalf() > 0:
  74. v.append(v[-1] + step)
  75. return v[:-1]
  76. srange(-pi, pi, sympify(1)/10)
  77. def bench_R11():
  78. "a = [random() + random()*I for w in [0..1000]]"
  79. [random() + random()*I for w in range(1000)]
  80. def bench_S1():
  81. "e=(x+y+z+1)**7;f=e*(e+1);f.expand()"
  82. e = (x + y + z + 1)**7
  83. f = e*(e + 1)
  84. f.expand()
  85. if __name__ == '__main__':
  86. benchmarks = [
  87. bench_R1,
  88. bench_R2,
  89. bench_R3,
  90. bench_R5,
  91. bench_R6,
  92. bench_R7,
  93. bench_R8,
  94. #_bench_R9,
  95. bench_R10,
  96. bench_R11,
  97. #bench_S1,
  98. ]
  99. report = []
  100. for b in benchmarks:
  101. t = clock()
  102. b()
  103. t = clock() - t
  104. print("%s%65s: %f" % (b.__name__, b.__doc__, t))