Skip to content

Instantly share code, notes, and snippets.

@kmsquire
Last active December 27, 2015 23:09
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 kmsquire/7403647 to your computer and use it in GitHub Desktop.
Save kmsquire/7403647 to your computer and use it in GitHub Desktop.
Comparison of array generation
julia> func(0,.1,.1); @time func(0,.1,10000)
elapsed time: 1.43696256 seconds (800264048 bytes allocated)
julia> func3(0,.1,.1); @time func3(0,.1,10000)
elapsed time: 0.838369673 seconds (800264048 bytes allocated)
julia> func5(0,.1,.1); @time func5(0,.1,10000)
elapsed time: 0.409506126 seconds (1000312 bytes allocated)
In [2]: func()
(0.40718698501586914, 'elapsed')
# Array creation from range
function func(a, inc, b)
    for i=1:1000
        [a:inc:b]
    end
end
# Assignment with array creation from range
function func3(a, inc, b)
    for i=1:1000
        A = [a:inc:b]
    end
end
# Preallocation
function func5(a, inc, b)
A = Array(Float64, length(a:inc:b))
    for i=1:1000
        A[:] = a:inc:b
    end
end
func(0,.1,.1); @time func(0,.1,10000)
func3(0,.1,.1); @time func3(0,.1,10000)
func5(0,.1,.1); @time func5(0,.1,10000)
import numpy as np
from time import time
def func():
a = 0
b = 10000
inc = 0.1
t0 = time()
for i in range(1000):
np.arange(a, b, inc)
print(time()-t0, "elapsed")
@skariel
Copy link

skariel commented Nov 11, 2013

here is a c++ version:
https://gist.github.com/skariel/7410160

in my machine it is ~20% faster than func3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment