Skip to content

Instantly share code, notes, and snippets.

@danielmatz
Created October 18, 2016 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmatz/0c4fefb4f0fd53e9cfc35331b9783f09 to your computer and use it in GitHub Desktop.
Save danielmatz/0c4fefb4f0fd53e9cfc35331b9783f09 to your computer and use it in GitHub Desktop.
Comparison of implementations of zeros
using BenchmarkTools
function zeros_calloc{S, N}(T::Type, dims::Vararg{S, N})
data = ccall((:calloc, "libc"),
Ptr{Void},
(Csize_t, Csize_t),
prod(dims), sizeof(T))
data == C_NULL && error("Failed to allocate memory")
ccall(:jl_ptr_to_array,
Array{T, N},
(Any, Ptr{Void}, Any, Cint),
Array{T, N}, data, dims, 1)
end
zeros_mmap{S, N}(T::Type, dims::Vararg{S, N}) = Mmap.mmap(Array{T, N}, dims)
@show @benchmark zeros(Float64, 10)
@show @benchmark zeros_calloc(Float64, 10)
@show @benchmark zeros_mmap(Float64, 10)
@show @benchmark zeros(Float64, 1000, 1000)
@show @benchmark zeros_calloc(Float64, 1000, 1000)
@show @benchmark zeros_mmap(Float64, 1000, 1000)
function zeros_then_fill(f, T, dims...)
A = f(T, dims...)
fill!(A, 1.0)
A
end
@show @benchmark zeros_then_fill(zeros, Float64, 1000, 1000)
@show @benchmark zeros_then_fill(zeros_calloc, Float64, 1000, 1000)
@show @benchmark zeros_then_fill(zeros_mmap, Float64, 1000, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment