| 1 | n/a | # |
|---|
| 2 | n/a | # Copyright (C) 2001-2012 Python Software Foundation. All Rights Reserved. |
|---|
| 3 | n/a | # Modified and extended by Stefan Krah. |
|---|
| 4 | n/a | # |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | # Usage: ../../../python bench.py |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | import time |
|---|
| 10 | n/a | from math import log, ceil |
|---|
| 11 | n/a | try: |
|---|
| 12 | n/a | from test.support import import_fresh_module |
|---|
| 13 | n/a | except ImportError: |
|---|
| 14 | n/a | from test.test_support import import_fresh_module |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | C = import_fresh_module('decimal', fresh=['_decimal']) |
|---|
| 17 | n/a | P = import_fresh_module('decimal', blocked=['_decimal']) |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | # |
|---|
| 20 | n/a | # NOTE: This is the pi function from the decimal documentation, modified |
|---|
| 21 | n/a | # for benchmarking purposes. Since floats do not have a context, the higher |
|---|
| 22 | n/a | # intermediate precision from the original is NOT used, so the modified |
|---|
| 23 | n/a | # algorithm only gives an approximation to the correctly rounded result. |
|---|
| 24 | n/a | # For serious use, refer to the documentation or the appropriate literature. |
|---|
| 25 | n/a | # |
|---|
| 26 | n/a | def pi_float(): |
|---|
| 27 | n/a | """native float""" |
|---|
| 28 | n/a | lasts, t, s, n, na, d, da = 0, 3.0, 3, 1, 0, 0, 24 |
|---|
| 29 | n/a | while s != lasts: |
|---|
| 30 | n/a | lasts = s |
|---|
| 31 | n/a | n, na = n+na, na+8 |
|---|
| 32 | n/a | d, da = d+da, da+32 |
|---|
| 33 | n/a | t = (t * n) / d |
|---|
| 34 | n/a | s += t |
|---|
| 35 | n/a | return s |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | def pi_cdecimal(): |
|---|
| 38 | n/a | """cdecimal""" |
|---|
| 39 | n/a | D = C.Decimal |
|---|
| 40 | n/a | lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24) |
|---|
| 41 | n/a | while s != lasts: |
|---|
| 42 | n/a | lasts = s |
|---|
| 43 | n/a | n, na = n+na, na+8 |
|---|
| 44 | n/a | d, da = d+da, da+32 |
|---|
| 45 | n/a | t = (t * n) / d |
|---|
| 46 | n/a | s += t |
|---|
| 47 | n/a | return s |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | def pi_decimal(): |
|---|
| 50 | n/a | """decimal""" |
|---|
| 51 | n/a | D = P.Decimal |
|---|
| 52 | n/a | lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24) |
|---|
| 53 | n/a | while s != lasts: |
|---|
| 54 | n/a | lasts = s |
|---|
| 55 | n/a | n, na = n+na, na+8 |
|---|
| 56 | n/a | d, da = d+da, da+32 |
|---|
| 57 | n/a | t = (t * n) / d |
|---|
| 58 | n/a | s += t |
|---|
| 59 | n/a | return s |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | def factorial(n, m): |
|---|
| 62 | n/a | if (n > m): |
|---|
| 63 | n/a | return factorial(m, n) |
|---|
| 64 | n/a | elif m == 0: |
|---|
| 65 | n/a | return 1 |
|---|
| 66 | n/a | elif n == m: |
|---|
| 67 | n/a | return n |
|---|
| 68 | n/a | else: |
|---|
| 69 | n/a | return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | print("\n# ======================================================================") |
|---|
| 73 | n/a | print("# Calculating pi, 10000 iterations") |
|---|
| 74 | n/a | print("# ======================================================================\n") |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | to_benchmark = [pi_float, pi_decimal] |
|---|
| 77 | n/a | if C is not None: |
|---|
| 78 | n/a | to_benchmark.insert(1, pi_cdecimal) |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | for prec in [9, 19]: |
|---|
| 81 | n/a | print("\nPrecision: %d decimal digits\n" % prec) |
|---|
| 82 | n/a | for func in to_benchmark: |
|---|
| 83 | n/a | start = time.time() |
|---|
| 84 | n/a | if C is not None: |
|---|
| 85 | n/a | C.getcontext().prec = prec |
|---|
| 86 | n/a | P.getcontext().prec = prec |
|---|
| 87 | n/a | for i in range(10000): |
|---|
| 88 | n/a | x = func() |
|---|
| 89 | n/a | print("%s:" % func.__name__.replace("pi_", "")) |
|---|
| 90 | n/a | print("result: %s" % str(x)) |
|---|
| 91 | n/a | print("time: %fs\n" % (time.time()-start)) |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | print("\n# ======================================================================") |
|---|
| 95 | n/a | print("# Factorial") |
|---|
| 96 | n/a | print("# ======================================================================\n") |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | if C is not None: |
|---|
| 99 | n/a | c = C.getcontext() |
|---|
| 100 | n/a | c.prec = C.MAX_PREC |
|---|
| 101 | n/a | c.Emax = C.MAX_EMAX |
|---|
| 102 | n/a | c.Emin = C.MIN_EMIN |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | for n in [100000, 1000000]: |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | print("n = %d\n" % n) |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | if C is not None: |
|---|
| 109 | n/a | # C version of decimal |
|---|
| 110 | n/a | start_calc = time.time() |
|---|
| 111 | n/a | x = factorial(C.Decimal(n), 0) |
|---|
| 112 | n/a | end_calc = time.time() |
|---|
| 113 | n/a | start_conv = time.time() |
|---|
| 114 | n/a | sx = str(x) |
|---|
| 115 | n/a | end_conv = time.time() |
|---|
| 116 | n/a | print("cdecimal:") |
|---|
| 117 | n/a | print("calculation time: %fs" % (end_calc-start_calc)) |
|---|
| 118 | n/a | print("conversion time: %fs\n" % (end_conv-start_conv)) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | # Python integers |
|---|
| 121 | n/a | start_calc = time.time() |
|---|
| 122 | n/a | y = factorial(n, 0) |
|---|
| 123 | n/a | end_calc = time.time() |
|---|
| 124 | n/a | start_conv = time.time() |
|---|
| 125 | n/a | sy = str(y) |
|---|
| 126 | n/a | end_conv = time.time() |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | print("int:") |
|---|
| 129 | n/a | print("calculation time: %fs" % (end_calc-start_calc)) |
|---|
| 130 | n/a | print("conversion time: %fs\n\n" % (end_conv-start_conv)) |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | if C is not None: |
|---|
| 133 | n/a | assert(sx == sy) |
|---|