r/cpp_questions • • 15d 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.

33 Upvotes

63 comments sorted by

View all comments

Show parent comment

15

u/No_Engineering_1155 14d ago

I think the main reason is, that you only want to see a short snippet of your code in assembly and the convenience is there, copy-pasta your code and see what comes out as assembly. You can easily swap between compilers without installing them. But maybe there are other folks with better explanation.