Skip to content

Instantly share code, notes, and snippets.

@ivarne
Last active December 30, 2015 17:49
Show Gist options
  • Save ivarne/7863449 to your computer and use it in GitHub Desktop.
Save ivarne/7863449 to your computer and use it in GitHub Desktop.
function old_colon{T<:Integer}(start::T, step::T, stop::T)
Range(start, step, max(0, div(stop-start+step, step)))
end
function new_colon{T<:Integer}(start::T, step::T, stop::T)
len,rem = divrem(stop-start,step)
Range(start, step, max(0, len+div(rem+step,step)))
end
function range_test_old()
for start = -1000:1000, stop = start-1000:10:start+1000, step=-31:2:30
r=old_colon(start,step,stop)
end
end
function range_test_new()
for start = -1000:1000, stop = start-1000:10:start+1000, step=-31:2:30
r=new_colon(start,step,stop)
end
end
println("Old")
@time range_test_old();
@time range_test_old();
@time range_test_old();
@time range_test_old();
@time range_test_old();
println("New")
@time range_test_new();
@time range_test_new();
@time range_test_new();
@time range_test_new();
@time range_test_new();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment