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

后端 未结 11 1199
醉话见心
醉话见心 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:21

    They are almost identical. Both solutions involve a temporary variable, an increment of a word on your system (int or ptr), and a logical check which should take one assembly instruction.

    The only difference I see is the array lookup

    arr[idx]

    might require pointer arithmetic then a fetch while the dereference:

    *ptr

    just requires a fetch

    My advice is that if it really matters, implement both and see if there's any savings.

提交回复
热议问题