Global Variables performance effect (c, c++)

后端 未结 4 642
半阙折子戏
半阙折子戏 2021-02-05 08:24

I\'m currently developing a very fast algorithm, with one part of it being an extremely fast scanner and statistics function. In this quest, i\'m after any performance benefit.

4条回答
  •  孤独总比滥情好
    2021-02-05 08:36

    If you have something like

    int stats[256]; while (p

    you are not really comparing the same thing because for the first instance you are not doing an initialization of your array. Written explicitly the second line is equivalent to

    static int stats[256] = { 0 }; while (p

    So to be a fair comparison you should have the first read

     int stats[256] = { 0 }; while (p

    Your compiler might deduce much more things if he has the variables in a known state.

    Now then, there could be runtime advantage of the static case, since the initialization is done at compile time (or program startup).

    To test if this makes up for your difference you should run the same function with the static declaration and the loop several times, to see if the difference vanishes if your number of invocations grows.

    But as other said already, best is to inspect the assembler that your compiler produces to see what effective difference there are in the code that is produced.

提交回复
热议问题