Faster alternative to nested loops?

后端 未结 12 1472
栀梦
栀梦 2020-12-12 18:35

I have a need to create a list of combinations of numbers. The numbers are quite small so I can use byte rather than int. However it requires many

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 19:24

    Some of your numbers fit entirely on an integer nuimber of bits, so you can "pack" them with the upper level number :

    for (byte lm = 0; lm < 12; lm++)
    {
        ...
        t[z].l = (lm&12)>>2;
        t[z].m = lm&3;
        ...
    }
    

    Of course, this makes the code less readable, but you saved one loop. This can be done each time one of the numbers is a power of two, which is seven time in your case.

提交回复
热议问题