# Performance of resize! vs pre-allocating with zeros(...)

**URL:** <https://discourse.julialang.org/t/performance-of-resize-vs-pre-allocating-with-zeros/80258>\
**Category:** Performance\
**Created:** [April 29, 2022, 3:00pm UTC](https://discourse.julialang.org/t/performance-of-resize-vs-pre-allocating-with-zeros/80258 "2022-04-29T15:00:53Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 29, 2022, 6:05pm UTC](https://discourse.julialang.org/t/performance-of-resize-vs-pre-allocating-with-zeros/80258/7 "2022-04-29T18:05:34Z")

</div>

> [@fcdimitr](#):
>
> One strategy is lazy allocation, the other is eager. In the lazy case, the pages are not allocated by the OS until needed, hence the penalty when visiting them for the first time.

Please refer to the following thread for an in-depth discussion about the (dis-)advantages of using `calloc`, as well as links to further discussions.

> [@Faster zeros with calloc](https://discourse.julialang.org/t/faster-zeros-with-calloc/69860):
>
> Abstract TL; DR zeros\_via\_calloc is a potentially faster version of zeros that is comparable to Array{T}(undef, ...) and numpy.zeros. function zeros\_via\_calloc(::Type{T}, dims::Integer...) where T ptr = Ptr{T}(Libc.calloc(prod(dims), sizeof(T))) return unsafe\_wrap(Array{T}, ptr, dims; own=true) end # Windows benchmark julia\> @btime zeros\_via\_calloc(Float64, 1024, 1024); 12.400 μs (2 allocations: 8.00 MiB) julia\> @btime zeros(Float64, 1024, 1024); 1.652 ms (2 allocations: 8.00 MiB)…

---

_[View the full topic](https://discourse.julialang.org/t/performance-of-resize-vs-pre-allocating-with-zeros/80258)._
