Why is std::vector::operator[] 5 to 10 times faster than std::vector::at()?

前端 未结 3 1595
难免孤独
难免孤独 2020-12-02 09:05

During program optimization, trying to optimize a loop that iterates through a vector, I found the following fact: ::std::vector::at() is EXTREMELY slower than operator[] !<

相关标签:
3条回答
  • 2020-12-02 09:42

    The reason is that an unchecked access can probably be done with a single processor instruction. A checked access will also have to load the size from memory, compare it with the index, and (assuming it's in range) skip over a conditional branch to the error handler. There may be more faffing around to handle the possibility of throwing an exception. This will be many times slower, and this is precisely why you have both options.

    If you can prove that the index is within range without a runtime check then use operator[]. Otherwise, use at(), or add your own check before access. operator[] should be more or less as fast as possible, but will explode messily if the index is invalid.

    0 讨论(0)
  • 2020-12-02 09:50

    You don't do anything with the return value, so if the compiler inlines these functions it can optimize them away completely. Or perhaps it can optimize away the subscript ([]) version completely. Running without optimizations is useless from a performance measurement perspective, what you need is some simple but useful program to exercise the functions so they don't just get optimized away. For example you could shuffle the vector (randomly swap 50000 pairs of elements).

    0 讨论(0)
  • 2020-12-02 09:59

    I ran your test code on my machine:

    In an unoptimized debug build, the difference between the two loops is insignificant.

    In an optimized release build, the second for loop is optimized out entirely (the call to operator[] is likely inlined and the optimizer can see that the loop does nothing and can remove the whole loop).

    If I change the body of the loops to do some actual work, e.g., vec.at(i)++; and vec[i]++;, respectively, the difference between the two loops is insignificant.

    I don't see this five to tenfold performance difference that you see.

    0 讨论(0)
提交回复
热议问题