Last active
August 29, 2015 14:03
-
-
Save Vannevelj/2bf4a27d8b8777b34b82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var invocations = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
var random = new Random(); | |
var iterations = 100000000; | |
var data = new object[] { 5, 5L, (sbyte) 5, (short) 5, (byte) 5, (ushort) 5, (uint) 5, 5ul, "lala", SomeEnum.SomeValue }; | |
var times = new long[] { 0L, 0L }; | |
var sw = new Stopwatch(); | |
sw.Start(); | |
for(int i = 0; i < iterations; i++){ | |
var index = random.Next(10); | |
invocations[index]++; | |
SuperFastSolver.NumericalEquals(data[index], 5ul); | |
} | |
times[0] = sw.ElapsedMilliseconds; | |
sw.Restart(); | |
for(int i = 0; i < iterations; i++){ | |
VladimirFastSolver.NumericalEquals(data[random.Next(10)], 5ul); | |
} | |
times[1] = sw.ElapsedMilliseconds; | |
sw.Stop(); | |
Console.WriteLine ("With overloads: {0}ms", times[0]); | |
Console.WriteLine ("With ifs: {0}ms", times[1]); | |
Console.WriteLine ("Invocations: {0}", string.Join(" - ", invocations)); | |
} | |
enum SomeEnum { | |
SomeValue | |
} | |
class SuperFastSolver | |
{ | |
public static bool NumericalEquals(int x, ulong y) | |
{ | |
return x >= 0 && y == (ulong)x; | |
} | |
public static bool NumericalEquals(long x, ulong y) | |
{ | |
return x >= 0 && y == (ulong)x; | |
} | |
public static bool NumericalEquals(sbyte x, ulong y) | |
{ | |
return x >= 0 && y == (ulong)x; | |
} | |
public static bool NumericalEquals(short x, ulong y) | |
{ | |
return x >= 0 && y == (ulong)x; | |
} | |
public static bool NumericalEquals(byte x, ulong y) | |
{ | |
return y == x; | |
} | |
public static bool NumericalEquals(ushort x, ulong y) | |
{ | |
return y == x; | |
} | |
public static bool NumericalEquals(uint x, ulong y) | |
{ | |
return y == x; | |
} | |
public static bool NumericalEquals(ulong x, ulong y) | |
{ | |
return y == x; | |
} | |
public static bool NumericalEquals(object x, ulong y) | |
{ | |
return false; | |
} | |
} | |
class VladimirFastSolver | |
{ | |
public static bool NumericalEquals(object x, ulong y) | |
{ | |
if (x is sbyte) | |
{ | |
sbyte z = (sbyte)x; | |
return z >= 0 && y == (ulong)z; | |
} | |
if (x is short) | |
{ | |
short z = (short)x; | |
return z >= 0 && y == (ulong)z; | |
} | |
if (x is int) | |
{ | |
int z = (int)x; | |
return z >= 0 && y == (ulong)z; | |
} | |
if (x is long) | |
{ | |
long z = (long)x; | |
return z >= 0 && y == (ulong)z; | |
} | |
if (x is byte) | |
{ | |
return y == (byte)x; | |
} | |
if (x is ushort) | |
{ | |
return y == (ushort)x; | |
} | |
if (x is uint) | |
{ | |
return y == (uint)x; | |
} | |
if (x is ulong) | |
{ | |
return y == (ulong)x; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment