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

10

u/nickwebha 13d ago

Am I living in a world where someone can copyright an std::vector sort? For a student in a school doing school work?

0

u/MillenniumDev 13d ago

They are being salty after I pointed out that it is possible to make their algorithms 10-13 times faster. Barred me not only from sharing their code, but also from questioning it or directly mentioning that it is slow. While it is unlikely for them to find me on Reddit, I am still avoiding breaking their "kind request" to avoid drama. Legally they can't claim shit, it is quicksort, they are not inventors of it.

2

u/imradzi 10d ago

really? i thought this sorting algorithm people has researched, tested, theorized for decades, and you claim yours is 10 fold better than theirs?

1

u/Wild_Meeting1428 11d ago

have you compiled their code with -O3 / -O2 and most important -NDEBUG? calling the subscript operator via the vector or the data pointer should be exactly the same.