Created
October 18, 2016 01:57
Comparison of implementations of zeros
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 | |
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