Effective optimization strategies on modern C++ compilers

后端 未结 19 2087
梦如初夏
梦如初夏 2020-12-22 17:02

I\'m working on scientific code that is very performance-critical. An initial version of the code has been written and tested, and now, with profiler in hand, it\'s time to

19条回答
  •  情深已故
    2020-12-22 17:38

    Here is something that worked for me once. I can't say that it will work for you. I had code on the lines of

    switch(num) {
       case 1: result = f1(param); break;
       case 2: result = f2(param); break;
       //...
    }
    

    Then I got a serious performance boost when I changed it to

    // init:
    funcs[N] = {f1, f2 /*...*/};
    // later in the code:
    result = (funcs[num])(param);
    

    Perhaps someone here can explain the reason the latter version is better. I suppose it has something to do with the fact that there are no conditional branches there.

提交回复
热议问题