Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 6 commits
  • 15 files changed
  • 0 comments
  • 1 contributor
2  .gitignore
... ...
@@ -0,0 +1,2 @@
  1
+*.pyc
  2
+*.pyo
1  README
... ...
@@ -1 +0,0 @@
1  
-Just an attempt to benchmark the algorithms from here: https://gist.github.com/289467 out of morbid curiosity.
2  README.markdown
Source Rendered
... ...
@@ -0,0 +1,2 @@
  1
+Just an attempt to benchmark the algorithms from
  2
+[here](https://gist.github.com/289467) out of morbid curiosity.
4  algos/enterprise_programmer.py
@@ -43,5 +43,5 @@ def calculateFactorial(self, target):
43 43
         return result
44 44
 
45 45
 
46  
-def f(x):
47  
-    return StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, x))
  46
+def factorial(x):
  47
+    return StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, x))
3  algos/expert_programmer.py
... ...
@@ -1,2 +1,5 @@
1 1
 import math
2 2
 fact = math.factorial
  3
+
  4
+
  5
+factorial = fact
3  algos/firstyear_c.py
@@ -6,3 +6,6 @@ def fact(x): #{
6 6
     #}
7 7
     return result;
8 8
 #}
  9
+
  10
+
  11
+factorial = fact
3  algos/firstyear_python.py
@@ -3,3 +3,6 @@ def Factorial(x):
3 3
     for i in xrange(2, x + 1):
4 4
         res *= i
5 5
     return res
  6
+
  7
+
  8
+factorial = Factorial
3  algos/firstyear_sicp.py
@@ -2,3 +2,6 @@
2 2
 def fact(x, acc=1):
3 3
     if (x > 1): return (fact((x - 1), (acc * x)))
4 4
     else:       return acc
  5
+
  6
+
  7
+factorial = fact
18  algos/gandaro.py
... ...
@@ -0,0 +1,18 @@
  1
+#!/usr/bin/env python3
  2
+# compatible from Python 2.6 to Python 3.2
  3
+from operator import mul
  4
+from functools import reduce
  5
+
  6
+# range can only handle ranges up to sys.maxint
  7
+def iterrange(a, b):
  8
+    while a < b:
  9
+        yield a
  10
+        a += 1
  11
+
  12
+def factorial(x):
  13
+    assert x > 0, 'x has to be greater than or equal to 0'
  14
+    return reduce(mul, iterrange(2, x+1), 1)
  15
+
  16
+
  17
+if __name__ == '__main__':
  18
+    print(factorial(6))
3  algos/lazier_python.py
... ...
@@ -1 +1,4 @@
1 1
 f = lambda x: x and x * f(x - 1) or 1
  2
+
  3
+
  4
+factorial = f
3  algos/lazy_python.py
... ...
@@ -1,2 +1,5 @@
1 1
 def fact(x):
2 2
     return x > 1 and x * fact(x - 1) or 1
  3
+
  4
+
  5
+factorial = fact
5  algos/python_hacker.py
... ...
@@ -1,4 +1,7 @@
1 1
 #@tailcall
2 2
 def fact(x, acc=1):
3 3
     if x: return fact(x.__sub__(1), acc.__mul__(x))
4  
-    return acc
  4
+    return acc
  5
+
  6
+
  7
+factorial = fact
3  algos/unix_programmer.py
... ...
@@ -1,3 +1,6 @@
1 1
 import os
2 2
 def fact(x):
3 3
     os.system('factorial ' + str(x))
  4
+
  5
+
  6
+factorial = fact
4  algos/windows_programmer.py
@@ -15,6 +15,8 @@ def CalculateAndPrintFactorialEx(dwNumber,
15 15
     return dwResult
16 16
     return 1
17 17
 import sys
18  
-
19 18
 def fact(x):
20 19
     CalculateAndPrintFactorialEx(x, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  20
+
  21
+
  22
+factorial = fact
104  bench.py 100644 → 100755
... ...
@@ -1,86 +1,48 @@
1  
-import sys
2  
-import math
3  
-from time import time
4  
-
5  
-
6  
-def start():
7  
-    return time()
  1
+#!/usr/bin/env python
8 2
 
  3
+import sys
9 4
 
10  
-def stop(start):
11  
-    return time() - start
  5
+from glob import glob
  6
+from math import factorial
  7
+from timeit import Timer
  8
+from argparse import ArgumentParser
12 9
 
  10
+MODULES = ['enterprise_programmer', 'expert_programmer', 'firstyear_c',
  11
+           'firstyear_pascal', 'firstyear_python', 'firstyear_sicp',
  12
+           'gandaro', 'lazier_python', 'lazy_python', 'newbie',
  13
+           'python_expert', 'python_hacker', 'unix_programmer',
  14
+           'windows_programmer']
13 15
 
14  
-def bench(name, num, callback, correct):
15  
-    print "========================================================"
16  
-    print "Starting algorithm %s for %s!" % (name, num)
17  
-    print "========================================================"
  16
+global function, args
18 17
 
19  
-    def round():
20  
-        s = start()
21  
-        newbie(num)
22  
-        return stop(s)
  18
+def bench(name, result):
  19
+    if function(*args) != result:
  20
+        print >>sys.stderr, 'Fail!  "%s" failed!' % name
  21
+        return 0
23 22
 
24  
-    rounds = 20
25  
-    time = 0
26  
-    for i in range(rounds):
27  
-        time += round()
  23
+    return Timer('function(*args)',
  24
+                 'from __main__ import function, args').timeit(20)/20
28 25
 
29  
-    avg = time / rounds
30  
-    print "Did it in %s ms averaged over %s rounds." % (avg * 1000, rounds)
31  
-    print "========================================================\n"
32  
-    return avg
33 26
 
34 27
 if __name__ == '__main__':
35  
-    try:
36  
-        r = sys.argv[1]
37  
-    except IndexError:
38  
-        r = 5000
  28
+    p = ArgumentParser(description='Benchmark factorial algorithms.')
  29
+    p.add_argument('--number', '-n', type=int, default=5000,
  30
+                   help='Number to calculate its factorial.', required=False)
  31
+    r = p.parse_args().number
39 32
 
40 33
     sys.setrecursionlimit(r * 2)
  34
+    correct = factorial(r)
  35
+    args = [r]
41 36
 
42  
-    print "Recursion limit:", sys.getrecursionlimit()
43  
-
44  
-    correct = math.factorial(r)
45  
-
46  
-    from algos.newbie import factorial as newbie
47  
-    bench("Newbie", r, newbie, correct)
48  
-
49  
-    from algos.firstyear_pascal import factorial as firstyear_pascal
50  
-    bench("First Year Pascal", r, firstyear_pascal, correct)
51  
-
52  
-    from algos.firstyear_c import fact as firstyear_c
53  
-    bench("First Year C", r, firstyear_c, correct)
54  
-
55  
-    from algos.firstyear_sicp import fact as firstyear_sicp
56  
-    bench("First Year SICP", r, firstyear_sicp, correct)
57  
-
58  
-    from algos.firstyear_python import Factorial as firstyear_python
59  
-    bench("First Year Python", r, firstyear_python, correct)
60  
-
61  
-    from algos.lazy_python import fact as lazy_python
62  
-    bench("Lazy Python", r, lazy_python, correct)
63  
-
64  
-    from algos.lazier_python import f as lazier_python
65  
-    bench("Lazier Python", r, lazier_python, correct)
66  
-
67  
-    from algos.python_expert import fact as python_expert
68  
-    bench("Python Expert", r, python_expert, correct)
69  
-
70  
-    from algos.python_hacker import fact as python_hacker
71  
-    bench("Python Hacker", r, python_hacker, correct)
72  
-
73  
-    from algos.expert_programmer import fact as expert_programmer
74  
-    bench("Expert Programmer", r, expert_programmer, correct)
75  
-
76  
-    from algos.web_designer import factorial as web_designer
77  
-    bench("Web Designer", r, web_designer, correct)
  37
+    for x in MODULES:
  38
+        print '#', x
78 39
 
79  
-    from algos.unix_programmer import fact as unix_programmer
80  
-    bench("Unix Programmer", r, unix_programmer, correct)
  40
+        try:
  41
+            m = __import__('algos.%s' % x, fromlist=['factorial'])
  42
+            function = m.factorial
  43
+            print bench(m.__name__, correct), 'seconds.'
81 44
 
82  
-    from algos.windows_programmer import fact as windows_programmer
83  
-    bench("Windows Programmer", r, windows_programmer, correct)
  45
+        except Exception as e:
  46
+            print >>sys.stderr, 'ERROR:', e.message
84 47
 
85  
-    from algos.enterprise_programmer import f as enterprise_programmer
86  
-    bench("Enterprise Programmer", r, enterprise_programmer, correct)
  48
+        print

No commit comments for this range

Something went wrong with that request. Please try again.