Last active
October 24, 2018 02:36
-
-
Save cako/2c42402d96cedbcf9b848704d79adc3a to your computer and use it in GitHub Desktop.
Benchmark pull request introduced to LinearMaps
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
using BenchmarkTools | |
using Statistics | |
using Plots | |
_flags(A::Array) = unsafe_load(convert(Ptr{UInt16}, pointer_from_objref(A)), 9) | |
_isshared(A::Array) = !(_flags(A) & 0x4000 == 0x0000) | |
function create_and_resize_flag(n) | |
a = Array{Float64}(undef, 1000) | |
if !_isshared(a) | |
resize!(a, n) | |
else | |
@warn("shared") | |
a = Array{Float64}(undef, n) | |
end | |
return | |
end | |
function create_and_resize_try(n) | |
a = Array{Float64}(undef, 1000) | |
try | |
resize!(a, n) | |
catch err | |
@warn(err) | |
end | |
return | |
end | |
function create_and_resize(n) | |
a = Array{Float64}(undef, 1000) | |
resize!(a, n) | |
return | |
end | |
function create(n) | |
a = Array{Float64}(undef, n) | |
return | |
end | |
create_resize_flag_t = Float64[] | |
create_resize_try_t = Float64[] | |
create_resize_t = Float64[] | |
create_t = Float64[] | |
t1 = minimum(@benchmark create(1000)).time | |
lengths = round.(Int, 10 .^ (1:0.5:5.5)) | |
for i in lengths | |
println(i) | |
t = @benchmark create_and_resize_flag($i) | |
push!(create_resize_flag_t, median(t).time - t1) | |
t = @benchmark create_and_resize_try($i) | |
push!(create_resize_try_t, median(t).time - t1) | |
t = @benchmark create_and_resize($i) | |
push!(create_resize_t, median(t).time - t1) | |
t = @benchmark create($i) | |
push!(create_t, median(t).time) | |
end | |
plot(lengths, create_resize_try_t, label="Resize and try", lw=3, xscale = :log10, yscale = :log10) | |
plot!(lengths, create_resize_t, label="Resize", lw=3, xscale = :log10, yscale = :log10) | |
plot!(lengths, create_t, label="Create", lw=3, xscale = :log10, yscale = :log10) | |
plot!(lengths, create_resize_flag_t, style=:dot, label="Resize by flag", lw=3, xscale = :log10, yscale = :log10) | |
plot!([1_000, 1_000], [10, 10^5], style=:dash, color="black", lw=1, label="Resize from here", xscale = :log10, yscale = :log10, xlabel="Length", ylabel="Time [ns]", legend=:bottomright) | |
savefig("benchmark.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment