Is there a performance difference between i++ and ++i in C?

前端 未结 14 1115
遥遥无期
遥遥无期 2020-11-22 10:08

Is there a performance difference between i++ and ++i if the resulting value is not used?

14条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:59

    My C is a little rusty, so I apologize in advance. Speedwise, I can understand the results. But, I am confused as to how both files came out to the same MD5 hash. Maybe a for loop runs the same, but wouldn't the following 2 lines of code generate different assembly?

    myArray[i++] = "hello";
    

    vs

    myArray[++i] = "hello";
    

    The first one writes the value to the array, then increments i. The second increments i then writes to the array. I'm no assembly expert, but I just don't see how the same executable would be generated by these 2 different lines of code.

    Just my two cents.

提交回复
热议问题