Skip to content

Instantly share code, notes, and snippets.

@milroc
Forked from johnmyleswhite/JuliaGlobals
Last active December 17, 2015 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milroc/5556298 to your computer and use it in GitHub Desktop.
Save milroc/5556298 to your computer and use it in GitHub Desktop.
Fork of @johnmyleswhite's Julia vs Python.
total = 0
N = 300
start_time = time()
for a in 0:(N - 1)
for b in 0:(N - 1)
for c in 0:(N - 1)
if a^2 + b^2 == c^2
total = total + 1
end
end
end
end
end_time = time()
end_time - start_time
# Repeat now that JIT has done its work
total = 0
N = 300
start_time = time()
for a in 0:(N - 1)
for b in 0:(N - 1)
for c in 0:(N - 1)
if a^2 + b^2 == c^2
total = total + 1
end
end
end
end
end_time = time()
println(end_time - start_time)
function loop(N::Integer)
total = 0
start_time = time()
for a in 0:(N - 1)
for b in 0:(N - 1)
for c in 0:(N - 1)
if a^2 + b^2 == c^2
total = total + 1
end
end
end
end
end_time = time()
return end_time - start_time
end
loop(300)
# Repeat now that JIT has done its work
println(loop(300))
from time import time
total = 0
N = 300
start = time()
for a in range(N):
for b in range(N):
for c in range(N):
if a**2 + b**2 == c**2:
total = total + 1
end = time()
print('global')
print(int(round((end - start)*1000)))
def foo():
total = 0
N = 300
start = time()
for a in range(N):
for b in range(N):
for c in range(N):
if a**2 + b**2 == c**2:
total = total + 1
end = time()
print('local')
print(int(round((end - start)*1000)))
foo()
# $ ./bin/pypy ~/work/gist/5556298/loops.py
# global
# 218
# local
# 57
total = 0
N = 300
from time import time
start = time()
for a in range(N):
for b in range(N):
for c in range(N):
if a**2 + b**2 == c**2:
total = total + 1
end = time()
print(end - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment