首页 > 解决方案 > C# Get Label Address For Lookup Table?

问题描述

I'm trying to optimize an algorithm I've been working on in C#. I have a while loop that will continue as long as a couple of variables are not 0. I was hoping to potentially replace this with some binary arithmetic & lookup table to avoid conditional branching.

while(a != 0 && b != 0 ) {
   // DO WORK
}

I was hoping I might be able to do something like this:

var lookup_table = [next_iteration, done];
next_iteration:
    // DO WORK
    goto lookup_table[<< a&b? 0 : 1 >>];
done:

Replacing the << a&b? 0 : 1 >> with binary arithmetic. Probably folding all the bits to the 1s place in a & b, and then masking out other bits in the final AND operation. Perhaps there's a better technique.

Of course, this level of optimization may just be beyond the capabilities of C#/Managed Languages without dropping into IL (which could be a valid solution). In which case I can drop down to C/C++. I'm curious to see if I can't get it to work in C# though.

标签: c#.netoptimization

解决方案


推荐阅读