sympybench.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. Downloaded from https://code.google.com/p/benchrun/
  3. Test code for benchrun: sympy performance benchmark.
  4. """
  5. from benchrun import Benchmark, clock
  6. import sympycore
  7. import sympy
  8. class SympyBenchmark(Benchmark):
  9. version = ['sympy', 'sympycore']
  10. reference = ('version', 'sympy')
  11. class DirectSymbolicAddition(SympyBenchmark):
  12. """Add small polynomials with rational coefficients"""
  13. parameters = ['version']
  14. def run(self, version):
  15. module = __import__(version)
  16. x, y, z = map(module.Symbol, 'xyz')
  17. a = 3*x + 2*x*y - module.Rational(1,2)*z + 2
  18. b = 2*x + module.Rational(3,2)*x*y + 4*z - 2
  19. n = N = 100
  20. t1 = clock()
  21. while n:
  22. a + n*b
  23. n -= 1
  24. t2 = clock()
  25. return (t2-t1)/N
  26. class PowerExpansion(SympyBenchmark):
  27. """Expand (x+y+z)**n * (y+x)**(n-1)"""
  28. parameters = ['version', 'n']
  29. n = [5, 10, 20]
  30. def run(self, version, n):
  31. module = __import__(version)
  32. if version == 'sympy' and n > 10:
  33. return None
  34. x, y, z = map(module.Symbol, 'xyz')
  35. t1 = clock()
  36. e = ((x+y+z)**n * (y+x)**(n-1)).expand()
  37. t2 = clock()
  38. return t2-t1
  39. class LegendreRecurrence(SympyBenchmark):
  40. """Calculate the nth Legendre polynomial by recurrence."""
  41. parameters = ['version', 'n']
  42. n = [3, 10, 30, 100]
  43. def run(self, version, n):
  44. module = __import__(version)
  45. x = module.Symbol('x')
  46. if version == 'sympy' and n > 30:
  47. return None
  48. b, a = x, 1
  49. t1 = clock()
  50. for n in range(1, n):
  51. b, a = (((2*n+1)*x*b - n*a)/(n+1)).expand(), b
  52. t2 = clock()
  53. return t2-t1
  54. all_benchmarks = [
  55. DirectSymbolicAddition(),
  56. PowerExpansion(),
  57. LegendreRecurrence(),
  58. ]
  59. for bench in all_benchmarks:
  60. bench.print_result()