fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. var values = new object[] { 42, 1.42f, 1.42d, 1.0m };
  8. var converters = new Func<object, bool>[] { CheckNegative1, CheckNegative2 };
  9. var dummy = false;
  10. var watch = new System.Diagnostics.Stopwatch();
  11.  
  12. foreach (var converter in converters) {
  13. foreach (var value in values) {
  14. watch.Reset();
  15. watch.Start();
  16. for (var i = 0; i < 3000000; ++i) {
  17. dummy = converter(value);
  18. }
  19. watch.Stop();
  20. Console.WriteLine("Testing with type {0} and converter #{1} took {2}ms",
  21. value.GetType().Name,
  22. Array.IndexOf(converters, converter) + 1,
  23. watch.ElapsedMilliseconds);
  24. }
  25.  
  26. }
  27.  
  28. Console.Write(Convert.ToInt32(dummy));
  29. }
  30.  
  31. static bool CheckNegative1(object number) {
  32. return Convert.ToDouble(number) < 0;
  33. }
  34.  
  35. static bool CheckNegative2(object number)
  36. {
  37. switch(Type.GetTypeCode(number.GetType())) {
  38. case TypeCode.Int32:
  39. return (int)number < 0;
  40. case TypeCode.Single:
  41. return (float)number < 0;
  42. case TypeCode.Double:
  43. return (double)number < 0;
  44. case TypeCode.Decimal:
  45. return (decimal)number < 0;
  46.  
  47. // etc etc
  48. default:
  49. throw new ArgumentException();
  50. }
  51. }
  52. }
Success #stdin #stdout 3.27s 37096KB
stdin
Standard input is empty
stdout
Testing with type Int32 and converter #1 took 130ms
Testing with type Single and converter #1 took 118ms
Testing with type Double and converter #1 took 127ms
Testing with type Decimal and converter #1 took 1983ms
Testing with type Int32 and converter #2 took 156ms
Testing with type Single and converter #2 took 147ms
Testing with type Double and converter #2 took 157ms
Testing with type Decimal and converter #2 took 416ms
0