Math.Pow(double, double) is a relatively expensive operation (since it can take an arbitrary exponent). For powers of two, byteshift operators are much faster.
Benchmark.NET can't actually measure the time it takes to do 1 << exp, but Math.Pow(2, exp) takes about 40 nanoseconds. It was actually slow enough to show up in some of the performance traces we took.
Math.Pow(double, double)
is a relatively expensive operation (since it can take an arbitrary exponent). For powers of two, byteshift operators are much faster.Benchmark.NET can't actually measure the time it takes to do
1 << exp
, butMath.Pow(2, exp)
takes about 40 nanoseconds. It was actually slow enough to show up in some of the performance traces we took.