Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to compare the performance of add, subtract, increment, multiply, divide for int, long, float, double and decimal values. */
- using System;
- using System.Diagnostics;
- public class MathOperationsTest
- {
- public static void Main()
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- TestAdd<int>(10, 4, 10000000);
- sw.Stop();
- Console.WriteLine("Add operation elased ticks: {0}", sw.ElapsedTicks);
- Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
- sw.Reset();
- sw.Start();
- TestSubstract<double>(2d, 1d, 10000000);
- sw.Stop();
- Console.WriteLine("Substract operation elased ticks: {0}", sw.ElapsedTicks);
- Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
- sw.Reset();
- sw.Start();
- TestIncrement<decimal>(1m, 10000000);
- sw.Stop();
- Console.WriteLine("Increment operation elased ticks: {0}", sw.ElapsedTicks);
- Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
- sw.Reset();
- sw.Start();
- TestMultiply<long>(1L, 2L, 10000000);
- sw.Stop();
- Console.WriteLine("Multiply operation elased ticks: {0}", sw.ElapsedTicks);
- Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
- sw.Reset();
- sw.Start();
- TestMultiply<float>(2f, 2f, 10000000);
- sw.Stop();
- Console.WriteLine("Divide operation elased ticks: {0}", sw.ElapsedTicks);
- Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
- }
- public static void TestAdd<T>(T number, T increment, int numberOfTimes)
- {
- dynamic result = number;
- for (int i = 0; i < numberOfTimes; i++)
- {
- result += increment;
- }
- }
- public static void TestSubstract<T>(T number, T increment, int numberOfTimes)
- {
- dynamic result = number;
- for (int i = 0; i < numberOfTimes; i++)
- {
- result -= increment;
- }
- }
- public static void TestIncrement<T>(T number, int numberOfTimes)
- {
- dynamic result = number;
- for (int i = 0; i < numberOfTimes; i++)
- {
- result++;
- }
- }
- public static void TestMultiply<T>(T number, T multiplier, int numberOfTimes)
- {
- dynamic result = number;
- for (int i = 0; i < numberOfTimes; i++)
- {
- result *= multiplier;
- }
- }
- public static void TestDivide<T>(T dividend, T factor, int numberOfTimes)
- {
- dynamic result = dividend;
- for (int i = 0; i < numberOfTimes; i++)
- {
- result /= factor;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment