Global Variables performance effect (c, c++)

后端 未结 4 636
半阙折子戏
半阙折子戏 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

    It's hard to beat static allocation for speed, and while the 10% is a pretty small difference, it could be due to address calculation.

    But if you're looking for speed, your example in a comment while(p is an obvious candidate for unrolling, such as:

    static int stats[M];
    static int index_array[N];
    int *p = index_array, *pend = p+N;
    // ... initialize the arrays ...
    while (p < pend-8){
      stats[p[0]]++;
      stats[p[1]]++;
      stats[p[2]]++;
      stats[p[3]]++;
      stats[p[4]]++;
      stats[p[5]]++;
      stats[p[6]]++;
      stats[p[7]]++;
      p += 8;
    }
    while(p

    Don't count on the compiler to do it for you. It might or might not be able to figure it out.

    Other possible optimizations come to mind, but they depend on what you're actually trying to do.

提交回复
热议问题