Skip to content

Instantly share code, notes, and snippets.

@ggggggggg
Last active December 31, 2015 04:09
Show Gist options
  • Save ggggggggg/7931979 to your computer and use it in GitHub Desktop.
Save ggggggggg/7931979 to your computer and use it in GitHub Desktop.
v = dot(a[j:j+length(filter)-1], filter)
elapsed time: 1.305102964 seconds (640420192 bytes allocated)
v = Base.dot(sub(a,j:j+length(filter)-1), filter)
elapsed time: 10.830825758 seconds (2561056544 bytes allocated)
v = Base.dot(unsafe_view(a,j:j+length(filter)-1), filter)
elapsed time: 0.171855427 seconds (420640 bytes allocated)
v = NumericExtensions.dot(unsafe_view(a,j:j+length(filter)-1), filter)
elapsed time: 0.165605407 seconds (420640 bytes allocated)
v = -a[j]-a[j+1]+a[j+2]+a[j+3]
elapsed time: 0.050317084 seconds (420208 bytes allocated)
v = sum([a[j+i-1]*filter[i] for i=1:length(filter)])
elapsed time: 1.407556779 seconds (800420080 bytes allocated)
do_filter(a::Vector{Uint16}, j::Int) = -a[j]-a[j+1]+a[j+2]+a[j+3]
v = do_filter(a,j)
elapsed time: 1.345634364 seconds (320093216 bytes allocated)
using NumericExtensions
function producetriggers(a::Vector{Uint16}, filter::Vector{Int}, threshold::(Int,Int), too_close::Int)
t_up = maximum(threshold)
t_dn = minimum(threshold)
last_trigger = -2*too_close
for j = 4:length(a)-4
v = dot(a[j:j+length(filter)-1], filter)
if (v >= t_up || v <= t_dn)
j-last_trigger > too_close && produce(j)
last_trigger = j
end
end
end
function producetriggers_sub(a::Vector{Uint16}, filter::Vector{Int}, threshold::(Int,Int), too_close::Int)
t_up = maximum(threshold)
t_dn = minimum(threshold)
last_trigger = -2*too_close
for j = 4:length(a)-4
v = Base.dot(sub(a,j:j+length(filter)-1), filter)
if (v >= t_up || v <= t_dn)
j-last_trigger > too_close && produce(j)
last_trigger = j
end
end
end
function producetriggers_unsafe_base(a::Vector{Uint16}, filter::Vector{Int}, threshold::(Int,Int), too_close::Int)
t_up = maximum(threshold)
t_dn = minimum(threshold)
last_trigger = -2*too_close
for j = 4:length(a)-4
v = Base.dot(unsafe_view(a,j:j+length(filter)-1), filter)
if (v >= t_up || v <= t_dn)
j-last_trigger > too_close && produce(j)
last_trigger = j
end
end
end
function producetriggers_unsafe(a::Vector{Uint16}, filter::Vector{Int}, threshold::(Int,Int), too_close::Int)
t_up = maximum(threshold)
t_dn = minimum(threshold)
last_trigger = -2*too_close
for j = 4:length(a)-4
v = NumericExtensions.dot(unsafe_view(a,j:j+length(filter)-1), filter)
if (v >= t_up || v <= t_dn)
j-last_trigger > too_close && produce(j)
last_trigger = j
end
end
end
function producetriggers_hard(a::Vector{Uint16}, filter::Vector{Int}, threshold::(Int,Int), too_close::Int)
t_up = maximum(threshold)
t_dn = minimum(threshold)
last_trigger = -2*too_close
for j = 4:length(a)-4
v = -a[j]-a[j+1]+a[j+2]+a[j+3]
if (v >= t_up || v <= t_dn)
j-last_trigger > too_close && produce(j)
last_trigger = j
end
end
end
function producetriggers_comprehension(a::Vector{Uint16}, filter::Vector{Int}, threshold::(Int,Int), too_close::Int)
t_up = maximum(threshold)
t_dn = minimum(threshold)
last_trigger = -2*too_close
for j = 4:length(a)-4
v = sum([a[j+i-1]*filter[i] for i=1:length(filter)])
if (v >= t_up || v <= t_dn)
j-last_trigger > too_close && produce(j)
last_trigger = j
end
end
end
do_filter(a::Vector{Uint16}, j::Int) = -a[j]-a[j+1]+a[j+2]+a[j+3]
function producetriggers_func(a::Vector{Uint16}, do_filter, threshold::(Int,Int), too_close::Int)
t_up = maximum(threshold)
t_dn = minimum(threshold)
last_trigger = -2*too_close
for j = 4:length(a)-4
v = do_filter(a,j)
if (v >= t_up || v <= t_dn)
j-last_trigger > too_close && produce(j)
last_trigger = j
end
end
end
data = zeros(Uint16,10000000)
data[rand(1:end,10000)] = uint16(1000)
filter = [-1,-1,1,1]
for j = 1:2
print("v = dot(a[j:j+length(filter)-1], filter)\n\t")
@time collect(@task producetriggers(data, filter, (-1000000, 100), 20) );
print("v = Base.dot(sub(a,j:j+length(filter)-1), filter)\n\t")
@time collect(@task producetriggers_sub(data, filter, (-1000000, 100), 20) );
print("v = Base.dot(unsafe_view(a,j:j+length(filter)-1), filter)\n\t")
@time collect(@task producetriggers_unsafe_base(data, filter, (-1000000, 100), 20) );
print("v = NumericExtensions.dot(unsafe_view(a,j:j+length(filter)-1), filter)\n\t")
@time collect(@task producetriggers_unsafe(data, filter, (-1000000, 100), 20) );
print("v = -a[j]-a[j+1]+a[j+2]+a[j+3]\n\t")
@time collect(@task producetriggers_hard(data, filter, (-1000000, 100), 20) );
print("v = sum([a[j+i-1]*filter[i] for i=1:length(filter)])\n\t")
@time collect(@task producetriggers_comprehension(data, filter, (-1000000, 100), 20) );
print("do_filter(a::Vector{Uint16}, j::Int) = -a[j]-a[j+1]+a[j+2]+a[j+3]\nv = do_filter(a,j)\n\t")
@time collect(@task producetriggers_func(data, do_filter, (-1000000, 100), 20) );
println("\n\n")
end
#Profile.clear()
#@profile collect(@task producetriggers(data, filter, (-1000000, 100), 20) )
#Profile.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment