sashomaga

Test System.Diagnostics

May 5th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. /* Write a program to compare the performance of add, subtract, increment, multiply, divide for int, long, float, double and decimal values. */
  2.  
  3. using System;
  4. using System.Diagnostics;
  5.  
  6. public class MathOperationsTest
  7. {
  8.     public static void Main()
  9.     {
  10.         Stopwatch sw = new Stopwatch();
  11.  
  12.         sw.Start();
  13.         TestAdd<int>(10, 4, 10000000);
  14.         sw.Stop();        
  15.         Console.WriteLine("Add operation elased ticks: {0}", sw.ElapsedTicks);
  16.         Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
  17.         sw.Reset();
  18.  
  19.         sw.Start();
  20.         TestSubstract<double>(2d, 1d, 10000000);
  21.         sw.Stop();
  22.         Console.WriteLine("Substract operation elased ticks: {0}", sw.ElapsedTicks);
  23.         Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
  24.  
  25.         sw.Reset();
  26.         sw.Start();
  27.         TestIncrement<decimal>(1m, 10000000);
  28.         sw.Stop();
  29.         Console.WriteLine("Increment operation elased ticks: {0}", sw.ElapsedTicks);
  30.         Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
  31.  
  32.         sw.Reset();
  33.         sw.Start();
  34.         TestMultiply<long>(1L, 2L, 10000000);
  35.         sw.Stop();
  36.         Console.WriteLine("Multiply operation elased ticks: {0}", sw.ElapsedTicks);
  37.         Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
  38.  
  39.         sw.Reset();
  40.         sw.Start();
  41.         TestMultiply<float>(2f, 2f, 10000000);
  42.         sw.Stop();
  43.         Console.WriteLine("Divide operation elased ticks: {0}", sw.ElapsedTicks);
  44.         Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
  45.     }
  46.  
  47.     public static void TestAdd<T>(T number, T increment, int numberOfTimes)
  48.     {
  49.         dynamic result = number;
  50.  
  51.         for (int i = 0; i < numberOfTimes; i++)
  52.         {
  53.             result += increment;
  54.         }
  55.     }
  56.  
  57.     public static void TestSubstract<T>(T number, T increment, int numberOfTimes)
  58.     {
  59.         dynamic result = number;
  60.  
  61.         for (int i = 0; i < numberOfTimes; i++)
  62.         {
  63.             result -= increment;
  64.         }
  65.     }
  66.  
  67.     public static void TestIncrement<T>(T number, int numberOfTimes)
  68.     {
  69.         dynamic result = number;
  70.  
  71.         for (int i = 0; i < numberOfTimes; i++)
  72.         {
  73.             result++;
  74.         }
  75.     }
  76.  
  77.     public static void TestMultiply<T>(T number, T multiplier, int numberOfTimes)
  78.     {
  79.         dynamic result = number;
  80.  
  81.         for (int i = 0; i < numberOfTimes; i++)
  82.         {
  83.             result *= multiplier;
  84.         }
  85.     }
  86.  
  87.     public static void TestDivide<T>(T dividend, T factor, int numberOfTimes)
  88.     {
  89.         dynamic result = dividend;
  90.  
  91.         for (int i = 0; i < numberOfTimes; i++)
  92.         {
  93.             result /= factor;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment