force-net / Crc32.NET

Fast version of Crc32 algorithm for .NET
MIT License
199 stars 52 forks source link

Complement vs Xor #4

Closed uecasm closed 3 years ago

uecasm commented 7 years ago

https://github.com/force-net/Crc32.NET/blob/699351a2a3ac9ac1cdadb1e16d4285e35f44fedd/Crc32.NET/SafeProxy.cs#L40 https://github.com/force-net/Crc32.NET/blob/699351a2a3ac9ac1cdadb1e16d4285e35f44fedd/Crc32.NET/SafeProxy.cs#L73

These are mathematically equivalent to just taking the complement (~crc, ~crcLocal, respectively), so why would you write it this way? Taking the complement should always be faster than doing an XOR, although perhaps the compiler is smart enough to do it for you.

force-net commented 7 years ago

This code was written historically. I'll check, is ~ operator gives any performance (it seems, no reasonable difference, but I need to check it, sometimes .NET JIT Optimizer gives original results).

force-net commented 6 years ago

I've tested this variants in different ways and environments and did not find any reasonable difference. It seems, JIT peforms all required operations by itself. I have not decide, what code is better to read yet, but there is no sense to change it for preformance reasons.