For example
let a = vec![1,2,3,4,5,6,7,8,9,10];
a.get(5).unwrap();
a.get(0).unwrap();
Does getting the element of index-5 take more time than getting that of index 0?
Also, Is it the same when indexing tuple?
Thanks in advance.
For example
let a = vec![1,2,3,4,5,6,7,8,9,10];
a.get(5).unwrap();
a.get(0).unwrap();
Does getting the element of index-5 take more time than getting that of index 0?
Also, Is it the same when indexing tuple?
Thanks in advance.
Arrays are random-access data structures, so asymptotically, all elements take approximately the same time to access. (Caching can change physical access times quite a bit, but it's nevertheless usually described as O(1).)
Tuples cannot be indexed; they are simply data structures of which the fields have integer names rather than words. Accessing a tuple's fields is exactly the same operation as accessing the named fields of a struct
: they are at a compile-time known constant offset, and thus also accessible in O(1).
You said 'arrays', is it the same in vector?
In Rust Vec
s and arrays have the same performance characteristics for accessing elements. A Vec
is kind of just an array that can be resized dynamically.
Vec is an implementation of the general concept of a dynamic array. Vec, Rust fixed-size arrays and slices are all contiguous-buffer arrays with a constant element size and have exactly the same performance characteristics when it comes to looking up elements.
Thanks for your responses.