#!/usr/bin/env python

#
# Copyright (C) 2001-2012 Python Software Foundation. All Rights Reserved.
# Modified and extended by Stefan Krah.
#

# Usage: ../../../python bench.py


import time
from math import log, ceil
try:
  from test.support import import_fresh_module
except ImportError:
  from test.test_support import import_fresh_module

C = import_fresh_module('decimal', fresh=['_decimal'])
P = import_fresh_module('decimal', blocked=['_decimal'])


# Pi function from the decimal.py documentation
def pi_float():
    """native float"""
    lasts, t, s, n, na, d, da = 0, 3.0, 3, 1, 0, 0, 24
    while s != lasts:
        lasts = s
        n, na = n+na, na+8
        d, da = d+da, da+32
        t = (t * n) / d
        s += t
    return s

def pi_cdecimal():
    """cdecimal"""
    D = C.Decimal
    lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
    while s != lasts:
        lasts = s
        n, na = n+na, na+8
        d, da = d+da, da+32
        t = (t * n) / d
        s += t
    return s

def pi_decimal():
    """decimal"""
    D = P.Decimal
    lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
    while s != lasts:
        lasts = s
        n, na = n+na, na+8
        d, da = d+da, da+32
        t = (t * n) / d
        s += t
    return s

def factorial(n, m):
    if (n > m):
        return factorial(m, n)
    elif m == 0:
        return 1
    elif n == m:
        return n
    else:
        return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m)


print("\n# ======================================================================")
print("#                   Calculating pi, 10000 iterations")
print("# ======================================================================\n")

for prec in [9, 19]:
    print("\nPrecision: %d decimal digits\n" % prec)
    to_benchmark = [pi_float, pi_decimal]
    if C is not None:
      to_benchmark.append(pi_cdecimal)
    for func in to_benchmark:
        start = time.time()
        if C is not None:
          C.getcontext().prec = prec
        P.getcontext().prec = prec
        for i in range(10000):
            x = func()
        print("%s:" % func.__name__.replace("pi_", ""))
        print("result: %s" % str(x))
        print("time: %fs\n" % (time.time()-start))


print("\n# ======================================================================")
print("#                               Factorial")
print("# ======================================================================\n")

if C is not None:
  c = C.getcontext()
  c.prec = C.MAX_PREC
  c.Emax = C.MAX_EMAX
  c.Emin = C.MIN_EMIN

for n in [100000, 1000000]:

    print("n = %d\n" % n)

    if C is not None:
      # C version of decimal
      start_calc = time.time()
      x = factorial(C.Decimal(n), 0)
      end_calc = time.time()
      start_conv = time.time()
      sx = str(x)
      end_conv = time.time()
      print("cdecimal:")
      print("calculation time: %fs" % (end_calc-start_calc))
      print("conversion time: %fs\n" % (end_conv-start_conv))

    # Python integers
    start_calc = time.time()
    y = factorial(n, 0)
    end_calc = time.time()
    start_conv = time.time()
    sy = str(y)
    end_conv =  time.time()

    print("int:")
    print("calculation time: %fs" % (end_calc-start_calc))
    print("conversion time: %fs\n\n" % (end_conv-start_conv))

    if C is not None:
      assert(sx == sy)

@@ -216,16 +216,18 @@
 il%0Atry:%0A
+  
   from t
@@ -285,16 +285,18 @@
 tError:%0A
+  
   from t
@@ -1884,24 +1884,26 @@
 None:%0A      
+  
 to_benchmark
@@ -2517,16 +2517,18 @@
 t None:%0A
+  
   c = C.
@@ -2540,16 +2540,18 @@
 ntext()%0A
+  
   c.prec
@@ -2564,16 +2564,18 @@
 AX_PREC%0A
+  
   c.Emax
@@ -2590,16 +2590,18 @@
 _EMAX%0A  
+ 
+ 
 c.Emin =
@@ -2697,16 +2697,18 @@
 :%0A      
+  
 # C vers
@@ -2718,24 +2718,26 @@
  of decimal%0A
+  
       start_
@@ -2751,24 +2751,26 @@
 time.time()%0A
+  
       x = fa
@@ -2796,24 +2796,26 @@
 ), 0)%0A      
+  
 end_calc = t
@@ -2821,24 +2821,26 @@
 time.time()%0A
+  
       start_
@@ -2864,16 +2864,18 @@
 )%0A      
+  
 sx = str
@@ -2880,24 +2880,26 @@
 tr(x)%0A      
+ 
+ 
 end_conv = t
@@ -2907,32 +2907,34 @@
 me.time()%0A      
+  
 print(%22cdecimal:
@@ -2932,24 +2932,26 @@
 cdecimal:%22)%0A
+  
       print(
@@ -2995,24 +2995,26 @@
 tart_calc))%0A
+  
       print(
@@ -3398,24 +3398,26 @@
 s not None:%0A
+  
       assert

Fix whitespace.