Last active
December 27, 2015 23:09
-
-
Save kmsquire/7403647 to your computer and use it in GitHub Desktop.
Comparison of array generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [2]: func() | |
(0.40718698501586914, 'elapsed') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here is a c++ version:
https://gist.github.com/skariel/7410160
in my machine it is ~20% faster than func3