sizehint!
sizehint!(s, n)
Suggest that collection s
reserve capacity for at least n
elements. This can improve performance.
Examples
-
Reserve capacity for an array:
julia> arr = [1, 2, 3]; julia> sizehint!(arr, 10) 3-element Array{Int64,1}: 1 2 3
This example suggests that the array
arr
should reserve capacity for at least 10 elements. It does not modify the existing elements but ensures that the array can efficiently accommodate additional elements. - Optimize capacity for a vector of strings:
julia> words = ["apple", "banana", "orange"]; julia> sizehint!(words, 5) 3-element Array{String,1}: "apple" "banana" "orange"
It suggests that the vector
words
should reserve capacity for at least 5 elements. This can be useful when you expect to add more elements to the vector in the future.
Common mistake example:
julia> sizehint!(3, 5)
ERROR: MethodError: no method matching sizehint!(::Int64, ::Int64)
In this example, the function is called on an incorrect argument type. sizehint!
expects a collection, such as an array or a vector, as the first argument. Make sure to provide a valid collection to sizehint!
for it to work correctly.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.