r/cpp_questions • u/MillenniumDev • 6d ago
SOLVED Performance of std::vector
In the university we had to implement quicksort, the unsigned ints are stored in std::vector. I have accidentally discovered, that all algorithms, despite how students implemented them, are much faster when instead of directly accessing data from the vector like that:
arr[n]
we write:
unsigned int *a = arr.data()
and access that data by writing:
a[n]
But I can't understand why is that, in both cases it translates to one and the same: *(a + n)
The code is compiled on Clion IDE if that changes anything, no custom settings for compilation are used.
I can't directly share the algorithms for copyright reasons as they are not mine.
View all comments
Show parent comment
10
u/Selbstdenker 6d ago
You can certainly do that. Compiler explorer is just a very easy way to do that. You do not have to figure out how to call your compiler to get the assembly instead of binary output or which tiny part of the executable is the part which comes from your code and not the cruft the compiler adds to get you an executable program. It also helps you to understand the instructions and aligns your code with the resulting instructions.