Skip to content

Instantly share code, notes, and snippets.

@quinnj
Last active August 29, 2015 14:05
Show Gist options
  • Save quinnj/8c8765963eb302344bc2 to your computer and use it in GitHub Desktop.
Save quinnj/8c8765963eb302344bc2 to your computer and use it in GitHub Desktop.
Julia: Create Array from Range: Issue #4785
macro NO_GC(ex)
quote
gc_disable()
ret = $ex;
gc_enable()
gc()
end
end
# func1: vcat(range)
function func1(a, b, inc, iter)
tic()
for i=1:iter
[a:inc:b]
end
toc()
end
# FloatRange, no gc; Results: 1.35-1.4 seconds, 0% gc
@NO_GC func1(0,10000,0.1,1000)
# StepRange, no gc; Results: 0.2-0.25 seconds, 0% gc
@NO_GC func1(0,200000,2,1000)
# FloatRange, gc; Results: 1.8-2.0 seconds, 75% gc
func1(0,10000,0.1,1000)
# StepRange, gc; Results: 0.75-0.76 seconds, 75% gc
func1(0,200000,2,1000)
# func2: pre-allocate A, assign with A[:] = range
function func2(a, b, inc, iter)
range = a:inc:b
A = Array(eltype(range), length(range))
tic()
for i=1:iter
A[:] = range
end
toc()
end
# FloatRange, no gc; Results: 0.82-0.89 seconds, 0% gc
@NO_GC func2(0,10000,0.1,1000)
# StepRange, no gc; Results: 1.94-2.00 seconds, 0% gc
@NO_GC func2(0,200000,2,1000)
# FloatRange, gc; Results: 0.76-0.78 seconds, no gc?
func2(0,10000,0.1,1000)
# StepRange, gc; Results: 1.87-1.90 seconds, no gc?
func2(0,200000,2,1000)
# func3: Pre-allocate A, devectorize assigning
function func3(a, b, inc, iter)
temp = a:inc:b
A = Array(eltype(temp), length(temp))
tic()
for i=1:iter
ix = 1
for j in temp
@inbounds A[ix] = j
ix += 1
end
end
toc()
end
# FloatRange, no gc; Results: 1.19-1.22 seconds, 0% gc
@NO_GC func3(0,10000,0.1,1000)
# StepRange, no gc; Results: 0.03-0.05 seconds, 0% gc
@NO_GC func3(0,200000,2,1000)
# FloatRange, gc; Results: 1.22-1.23 seconds, no gc
@time func3(0,10000,0.1,1000)
# StepRange, gc; Results: 0.04-0.05 seconds, no gc
@time func3(0,200000,2,1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment