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

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

    You're asking the wrong question. Should a developer aim for readability or performance first?

    The first version is idiomatic for processing array, and your intent will be clear to anyone who has worked with arrays before, whereas the second relies heavily on the equivalence between array names and pointers, forcing someone reading the code to switch metaphors several times.

    Cue the comments saying that the second version is crystal clear to any developer worth his keybaord.

    If you wrote your program, and it's running slow, and you have profiled to the point where you have identified this loop as the bottleneck, then it would make sense to pop the hood and look at which of these is faster. But get something clear up and running first using well-known idiomatic language constructs.

提交回复
热议问题