What are the differences between using array offsets vs pointer incrementation?

后端 未结 11 1205
醉话见心
醉话见心 2021-02-06 13:39

Given 2 functions, which should be faster, if there is any difference at all? Assume that the input data is very large

void iterate1(const char* pIn, int Size)
{         


        
11条回答
  •  悲&欢浪女
    2021-02-06 14:06

    To be sure, you must profile in your intended target environment.

    That said, my guess is that any modern compiler is going to optimize them both down to very similar (if not identical) code.

    If you didn't have an optimizer, the second has a chance of being faster, because you aren't re-computing the pointer on every iteration. But unless Size is a VERY large number (or the routine is called quite often), the difference isn't going to matter to your program's overall execution speed.

提交回复
热议问题