Last active
March 16, 2016 08:41
-
-
Save zeroflag/abc23ace07a85df04b91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: factorial ( n -- n! | err:1024 ) | |
dup 0< if drop 1024 throw then | |
dup 0= if | |
drop 1 | |
else | |
dup 1= if | |
drop 1 | |
else | |
dup 1- factorial * | |
then | |
then ; | |
: bench ( ntimes -- sec ) | |
time swap | |
0 do 10 factorial drop loop | |
time swap - ; | |
10000000 bench . \\ takes 2-3 seconds on Intel(R) Xeon(R) CPU X5650 @ 2.67GHz | |
###################################################################### | |
def factorial(n): | |
if n < 0: raise RuntimeException('neg') | |
if n == 0: | |
return 1 | |
else: | |
if n == 1: | |
return 1 | |
else: | |
return n * factorial(n-1) | |
import time | |
def bench(n): | |
s = time.time() | |
for i in range(n): factorial(10) | |
print time.time() - s | |
bench(10000000) # takes about 21 seconds on Intel(R) Xeon(R) CPU X5650 @ 2.67GHz using Python 2.7.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment