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

34 Upvotes

63 comments sorted by

View all comments

Show parent comment

1

u/ConstructionLost4861 9d ago

despite how students implemented them

Are you referring to yourself as "students"?

2

u/MillenniumDev 9d ago

I am refering to my group. My whole job was to manipulate algorithms of other students to find as many things as I can related to optimisation.

7

u/ConstructionLost4861 9d ago

Oh sorry for my confusion, I thought you mean "my students". Yeah look for the word "Debug" on your IDE's GUI and change it to "Release", or add -O2 to your build flags if you're compiling using make, or add --config Release to your cmake --build command.

2

u/I__Know__Stuff 9d ago

That's what I thought it meant, too.

1

u/AKostur 9d ago

Themselves plus their cohort.  They’re noting that it’s not just themselves, but other students saw the same results.