Skip to content

Instantly share code, notes, and snippets.

@foxtran
Created February 5, 2020 11:30
Show Gist options
  • Save foxtran/214a69aeb94f9028f2b0a0e6e915115f to your computer and use it in GitHub Desktop.
Save foxtran/214a69aeb94f9028f2b0a0e6e915115f to your computer and use it in GitHub Desktop.
Comparison push! and direct access performance
import Pkg
Pkg.add("BenchmarkTools")
Pkg.add("StatsBase")
Pkg.add("DataFrames")
Pkg.add("VegaLite")
Pkg.add("InteractiveUtils")
using InteractiveUtils
using BenchmarkTools
using StatsBase: median
using DataFrames
using VegaLite
versioninfo()
# Julia Version 1.3.1
# Commit 2d5741174c (2019-12-30 21:36 UTC)
# Platform Info:
# OS: Linux (x86_64-pc-linux-gnu)
# CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
# WORD_SIZE: 64
# LIBM: libopenlibm
# LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
function f1(n)
x = Vector{Int}(undef, n)
for i in 1:n
x[i] = i
end
x
end
function f2(n)
x = Int[]
for i in 1:n
push!(x, i)
end
x
end
function f3(n)
x = Int[]
sizehint!(x, n)
for i in 1:n
push!(x, i)
end
x
end
function f4(n)
x = Int[]
sizehint!(x, n)
for i in 1:n
x[i] = i
end
x
end
function f5(n)
x = Vector{Int}(undef, n)
for i in 1:n
push!(x, i)
end
x
end
function f6(n)
x = Vector{Int}(undef, n)
for i in 1:n
@inbounds x[i] = i
end
x
end
n = 10_000
@btime f1($n) # 5.212 μs (2 allocations: 78.20 KiB)
@btime f2($n) # 114.357 μs (14 allocations: 256.64 KiB)
@btime f3($n) # 108.457 μs (2 allocations: 78.27 KiB)
#f4(n) # BoundsError
#@assert f5(n) == f1(n) # AssertionError
########
# Plots
df = DataFrame(n = Int[], val = Float64[], type = String[])
for n in 2 .^ collect(0:17)
d = @benchmark f1($n)
p = @benchmark f2($n)
h = @benchmark f3($n)
di = @benchmark f6($n)
push!(df, (n, median(d.times)/1000, "direct"))
push!(df, (n, median(p.times)/1000, "push"))
push!(df, (n, median(h.times)/1000, "push_hint"))
push!(df, (n, median(di.times)/1000, "direct_inbounds"))
end
df |> @vlplot(
:line,
background="white",
x={field=:n, scale={type="log"}, axis={gridWidth=0.5}},
y={field=:val, scale={type="log"}, axis={gridWidth=0.5, title="Median time, μs"}},
color=:type,
width=600,
height=600
) |> x -> save(joinpath(@__DIR__, "push_direct_comparison.png"), x)
#####################
df2 = by(df, :n) do d
DataFrame(val = d[:val], type = d[:type], val_max = maximum(d[:val]), val_min = minimum(d[:val]))
end
df2[:rel_max] = df2[:val] ./ df2[:val_max]
df2[:rel_min] = df2[:val] ./ df2[:val_min]
df2 |> @vlplot(
:line,
background="white",
x={field=:n, scale={type="log"}, axis={gridWidth=0.5}},
y={field=:rel_max, axis={gridWidth=0.5, title="Median time, μs"}},
color=:type,
width=600,
height=600
) |> x -> save(joinpath(@__DIR__, "push_direct_comparison_rel_max.png"), x)
df2 |> @vlplot(
:line,
background="white",
x={field=:n, scale={type="log"}, axis={gridWidth=0.5}},
y={field=:rel_min, axis={gridWidth=0.5, title="Median time, μs"}},
color=:type,
width=600,
height=600
) |> x -> save(joinpath(@__DIR__, "push_direct_comparison_rel_min.png"), x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment