Is any particular bitwise operation (AND, OR, XOR, etc.) faster than another in C#?
The reason I ask is because I know that in hardware, most things are built using NAND gates, because a few NAND gates can replicate any other gate. Does this have any ramifications on high level programming languages? Or do all the abstraction layers in between make it so they all run at the same speed.
I realize any performance boost gained from this would be minimal, I'm just asking out of curiosity.
EDIT: Please, stop trying to explain that there is not functional reason to know this. It's literally just curiosity.
However, I am generating a HashCode by performing some bitwise operation on two numbers. It makes sense to use the least expensive/fastest operation possible. Again, it won't make any difference, I'm just curious.
EDIT: My question boils down to this: Does the fact that hardware depends on NAND gates at the lower levels have any effect on higher level processes. Would this cause a NAND to be faster than an XOR?
I ask out of curiosity about how details of hardware can impact software. It's just interesting to me.
Is any particular bitwise operation (AND, OR, XOR, etc.) faster than another in C#?
I've created a benchmark:
Random r = new Random();
int a = r.Next();
int b = r.Next();
int c = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < int.MaxValue; i++)
{
c += a & b;
}
sw.Stop();
Console.WriteLine("AND operator: {0} ticks", sw.Elapsed.Ticks);
Console.WriteLine("Result: {0}", c);
c = 0;
// The above is just to make sure that the optimizer does not optimize the loop away,
// as pointed out by johnnycrash in the comments.
sw.Restart();
for (int i = 0; i < int.MaxValue; i++)
{
c += a | b;
}
sw.Stop();
Console.WriteLine("OR operator: {0} ticks", sw.Elapsed.Ticks);
Console.WriteLine("Result: {0}", c);
c = 0;
for (int i = 0; i < int.MaxValue; i++)
{
c += a ^ b;
}
sw.Stop();
Console.WriteLine("XOR operator: {0} ticks", sw.Elapsed.Ticks);
Console.WriteLine("Result: {0}", c);
c = 0;
for (int i = 0; i < int.MaxValue; i++)
{
c += ~a;
}
sw.Stop();
Console.WriteLine("NOT operator: {0} ticks", sw.Elapsed.Ticks);
Console.WriteLine("Result: {0}", c);
c = 0;
for (int i = 0; i < int.MaxValue; i++)
{
c += a << 1;
}
sw.Stop();
Console.WriteLine("Left shift operator: {0} ticks", sw.Elapsed.Ticks);
Console.WriteLine("Result: {0}", c);
c = 0;
for (int i = 0; i < int.MaxValue; i++)
{
c += a >> 1;
}
sw.Stop();
Console.WriteLine("Right shift operator: {0} ticks", sw.Elapsed.Ticks);
Console.WriteLine("Result: {0}", c);
It outputs this:
AND operator: 7979680 ticks
OR operator: 7826806 ticks
XOR operator: 7826806 ticks
NOT operator: 7826806 ticks
Left shift operator: 7826806 ticks
Right shift operator: 7826806 ticks
The AND operator takes longer because it is the first loop. If I switch the AND and the OR loop for example, then the OR loop takes more time.
CPU's are synchronous logics engine, driven by a clock. These low-level operations are all performed in a single instruction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With