Skip to content

Instantly share code, notes, and snippets.

@binarybana
Created November 5, 2012 17:43
Show Gist options
  • Select an option

  • Save binarybana/4018913 to your computer and use it in GitHub Desktop.

Select an option

Save binarybana/4018913 to your computer and use it in GitHub Desktop.
Julia Python microbenchmarks take 2
from timeit import timeit
A = timeit(setup = 'import test,random; n = range(10**6)', stmt = 'test.stress1(n)',
number=5) / 5.0
B = timeit(setup = 'import test,random; n = range(10**6); random.shuffle(n)',
stmt = 'test.stress1(n)',
number=5) / 5.0
C = timeit(setup='import test,random; n = [random.randint(0,100) for i in range(10**6)]',
stmt = 'test.stress1(n)',
number=5) / 5.0
D = timeit(setup='import test,random; n = [random.randint(0,100000000) for i in range(10**6)]',
stmt = 'test.stress1(n)',
number=5) / 5.0
E = timeit(setup='import test',
stmt = 'test.stress_strings(10**6)',
number=5) / 5.0
print("%15s %6.2fs %5.2fs %5.2fs %5.2fs %5.2fs" % ("Python", A, B, C, D, E))
function stress1(keys::Array{Int,1})
x = Dict{Int,Float64}()
for i in keys
x[i] = 0.0
end
return x
end
function stress_strings(n::Int)
d = Dict{String,Int}()
for i = 1:n
d[string(i)] = i
end
return d
end
N = 10^6;
n = [1:N];
A = mean([@elapsed stress1(n) for i=1:5])
n = shuffle([1:N]);
B = mean([@elapsed stress1(n) for i=1:5])
n = randi((0,100),N);
C = mean([@elapsed stress1(n) for i=1:5])
n = randi((0,100000000),N);
D = mean([@elapsed stress1(n) for i=1:5])
E = mean([@elapsed stress_strings(N) for i in 1:5])
@printf("%15s %6s %6s %6s %6s %6s\n", "","A","B","C","D","E")
println("----------------------------------------------------")
#@printf("%15s %6.2fs %5.2fs %5.2fs %5.2fs %5.2fs", "julia@3711a14090", A, B, C, D, E)
@printf("%15s %6.2fs %5.2fs %5.2fs %5.2fs %5.2fs", "^^ with IIT", A, B, C, D, E)
def stress_strings(n):
d = {}
for i in xrange(1,n):
d[str(i)] = i
return d
def stress1(keys):
x = {}
for i in keys:
x[i] = 0.0
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment