I’ll answer my own question shall I?

Last night I asked this question on Twitter :-

#xna #Csharp Question – Anyone know if bit shifting is still faster than multiply/divide in .NET? E.G. (y<<8)+(y<<6) is faster than (y*320)

But no one answered :-( . So I wrote this quick test.

using System;
 
namespace Multiply_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int c;
 
            Console.Write("Test 1 - ");
            DateTime StartTimeTest1 = DateTime.Now;
            for (int y = 0; y &lt; int.MaxValue; y++)
                c = (y / 2);
            DateTime EndTimeTest1 = DateTime.Now;
 
            TimeSpan taken1 = new TimeSpan((EndTimeTest1.Subtract(StartTimeTest1)).Ticks);
            Console.WriteLine(string.Format("{0} Seconds",taken1.TotalSeconds));
 
            Console.Write("Test 2 - ");
            DateTime StartTimeTest2 = DateTime.Now;
            for (int y = 0; y &lt; int.MaxValue; y++)
                c = (y &gt;&gt; 1);
            DateTime EndTimeTest2 = DateTime.Now;
 
            TimeSpan taken2 = new TimeSpan((EndTimeTest2.Subtract(StartTimeTest2)).Ticks);
            Console.WriteLine(string.Format("{0} Seconds",taken2.TotalSeconds));
 
            Console.WriteLine("\n\nPress Any Key...");
            Console.ReadKey(true);
        }
    }
}

And this was the result :-

Multiply Test Results

Multiply Test Results

Yep still faster.

Comment are closed.