Created
December 8, 2010 17:04
-
-
Save zed/733555 to your computer and use it in GitHub Desktop.
test function call overhead in Python
This file contains 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
# http://stackoverflow.com/questions/4305518/why-is-equivalent-python-code-so-much-slower/4381575#4381575 | |
# iter_gcd -- Python 2.x version, with gcd function call | |
# Python 3.x version uses range instead of xrange | |
from sys import argv,stderr | |
def gcd(m, n): | |
if n > m: | |
m, n = n, m | |
while n != 0: | |
rem = m % n | |
m = n | |
n = rem | |
return m | |
def main(a1, a2): | |
comp = 0 | |
for j in xrange(a1, 1, -1): | |
for i in xrange(1, a2): | |
comp += gcd(i,j) | |
print(comp) | |
if __name__ == '__main__': | |
if len(argv) != 3: | |
stderr.write('usage: {0:s} num1 num2\n'.format(argv[0])) | |
exit(1) | |
else: | |
main(int(argv[1]), int(argv[2])) | |
# /usr/bin/time /usr/bin/python iter_gcd.py 2000 2000 | |
# 19446929 | |
# 4.82user 0.01system 0:04.83elapsed 99%CPU (0avgtext+0avgdata 17840maxresident)k | |
# 0inputs+0outputs (0major+1236minor)pagefaults 0swaps |
This file contains 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
# http://stackoverflow.com/questions/4305518/why-is-equivalent-python-code-so-much-slower/4381575#4381575 | |
# iter_gcd -- Python 2.x version, inline calculation, dummy function call | |
# Python 3.x version uses range instead of xrange | |
from sys import argv,stderr | |
def dummyfunc(n, m): | |
return n + m | |
def main(a1, a2): | |
comp = 0 | |
for j in xrange(a1, 1, -1): | |
for i in xrange(1, a2): | |
comp += dummyfunc(i, j) | |
print(comp) | |
if __name__ == '__main__': | |
if len(argv) != 3: | |
stderr.write('usage: {0:s} num1 num2\n'.format(argv[0])) | |
exit(1) | |
else: | |
main(int(argv[1]), int(argv[2])) | |
# /usr/bin/time /usr/bin/python iter_gcd_dummy.py 2000 2000 | |
# 7995998001 | |
# 0.79user 0.01system 0:00.80elapsed 99%CPU (0avgtext+0avgdata 17808maxresident)k | |
# 0inputs+0outputs (0major+1234minor)pagefaults 0swaps | |
This file contains 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
# http://stackoverflow.com/questions/4305518/why-is-equivalent-python-code-so-much-slower/4381575#4381575 | |
# iter_gcd -- Python 2.x version, inline calculation | |
# Python 3.x version uses range instead of xrange | |
from sys import argv,stderr | |
def main(a1, a2): | |
comp = 0 | |
for j in xrange(a1, 1, -1): | |
for i in xrange(1, a2): | |
if i < j: | |
m, n = j, i | |
else: | |
m, n = i, j | |
while n != 0: | |
rem = m % n | |
m = n | |
n = rem | |
comp += m | |
print(comp) | |
if __name__ == '__main__': | |
if len(argv) != 3: | |
stderr.write('usage: {0:s} num1 num2\n'.format(argv[0])) | |
exit(1) | |
else: | |
main(int(argv[1]), int(argv[2])) | |
# /usr/bin/time /usr/bin/python iter_gcd_inline.py 2000 2000 | |
# 19446929 | |
# 4.45user 0.00system 0:04.46elapsed 99%CPU (0avgtext+0avgdata 17856maxresident)k | |
# 0inputs+0outputs (0major+1237minor)pagefaults 0swaps |
This file contains 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
# http://stackoverflow.com/questions/4305518/why-is-equivalent-python-code-so-much-slower/4381575#4381575 | |
# iter_gcd -- Python 2.x version, inline calculation, dummy function call | |
# Python 3.x version uses range instead of xrange | |
from sys import argv,stderr | |
def dummyfunc(n, m): | |
a = n + m | |
def main(a1, a2): | |
comp = 0 | |
for j in xrange(a1, 1, -1): | |
for i in xrange(1, a2): | |
if i < j: | |
m, n = j, i | |
else: | |
m, n = i, j | |
while n != 0: | |
rem = m % n | |
m = n | |
n = rem | |
comp += m | |
dummyfunc(i, j) | |
print(comp) | |
if __name__ == '__main__': | |
if len(argv) != 3: | |
stderr.write('usage: {0:s} num1 num2\n'.format(argv[0])) | |
exit(1) | |
else: | |
main(int(argv[1]), int(argv[2])) | |
# /usr/bin/time /usr/bin/python iter_gcd_inline_dummy.py 2000 2000 | |
# 19446929 | |
# 5.14user 0.00system 0:05.14elapsed 99%CPU (0avgtext+0avgdata 17888maxresident)k | |
# 0inputs+0outputs (0major+1239minor)pagefaults 0swaps |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment