Julia combinations vs. Julia for-loop
The two pieces of code achieve the same objective. One uses combinations, the other uses a nested for-loops. The latter is about 100x faster. Does this mean there is huge room for improvement in Julia combinations function, or something else is happening here?
BTW Julia's performance of the for-loop version is really impressive. PyPy takes about 12 seconds for an equivalent for-loop version and C take 0.64 seconds with -O3 option.
Code using combinations (takes about 93 seconds)
========================================
function countcombos()
counter = 0
sumcounter = 0
combos = combinations(1:334, 4)
for c in combos
counter = counter + 1
if sum(c) <= 93
sumcounter = sumcounter + 1
end
#if counter % 1000000 == 0
# println(counter)
#end
end
println("counter: $counter")
println("sumcounter: $sumcounter")
end
start_time = time()
countcombos()
stop_time = time()
println("time taken: $(stop_time - start_time)")
Code using loop (takes about 0.95 seconds)
===================================
t0 = time()
function findcombos()
counter = 0
sumcounter = 0
for i = 1:331
for j = i+1:332
for k = j+1:333
for l = k+1:334
sum = i + j + k + l
counter = counter + 1
if sum <= 93
sumcounter = sumcounter + 1
end
end
end
end
println(i)
end
println("Counter: ", counter)
println("Sumcounter: ", sumcounter)
end
findcombos()
t1 = time()
println(t1-t0)