Created
November 16, 2023 02:47
-
-
Save tyleransom/06d6e416959997e78d4542f0ac37eead to your computer and use it in GitHub Desktop.
Julia Benchmarking of Data Containers
This file contains hidden or 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 Statistics, BenchmarkTools, DataFrames, Random | |
Random.seed!(1234) | |
mutable struct allParmsM | |
x::Float64 | |
end | |
# fill in the mutable struct with the values | |
allpM = allParmsM( | |
5.784 # x | |
) | |
# create a data frame | |
allp_df = DataFrame(x=5.784) | |
# create a dict | |
allp_dict = Dict(:x=>5.784) | |
# create a named tuple | |
allp_nt = (x=5.784,) | |
# mutable struct | |
function readin_struct(st) | |
st.x = rand() | |
return sqrt(st.x) | |
end | |
# data frame | |
function readin_df(stdf) | |
stdf[!,:x] .= rand() | |
return sqrt(stdf[!,:x][1]) | |
end | |
# dict | |
function readin_dict(stdict) | |
stdict[:x] = rand() | |
return sqrt(stdict[:x]) | |
end | |
# named tuple | |
function readin_nt(stnt) | |
stnt = merge(stnt, (x=rand(),)) | |
return sqrt(stnt.x) | |
end | |
function looper_struct(J::Int64,st) | |
out = zeros(J) | |
for j=1:J | |
out[j] = readin_struct(st) | |
end | |
return mean(out) | |
end | |
function looper_df(J::Int64,st) | |
out = zeros(J) | |
for j=1:J | |
out[j] = readin_df(st) | |
end | |
return mean(out) | |
end | |
function looper_dict(J::Int64,st) | |
out = zeros(J) | |
for j=1:J | |
out[j] = readin_dict(st) | |
end | |
return mean(out) | |
end | |
function looper_nt(J::Int64,st) | |
out = zeros(J) | |
for j=1:J | |
out[j] = readin_nt(st) | |
end | |
return mean(out) | |
end | |
# force compilation and collect garbage | |
looper_struct(1,allpM); looper_df(1,allp_df); looper_dict(1,allp_dict); looper_nt(1,allp_nt); GC.gc(); | |
# Do the benchmark timing | |
println("\nmutable struct") | |
@btime looper_struct(1_000_000,allpM) | |
println("\ndict") | |
@btime looper_dict(1_000_000,allp_dict) | |
println("\ndata frame") | |
@btime looper_df(1_000_000,allp_df) | |
println("\nnamed tuple") | |
@btime looper_nt(1_000_000,allp_nt) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment