Advertisement
starbeamrainbowlabs

C Sharp squaring numbers test code

Mar 5th, 2015
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Console.WriteLine("This program tests the performance of squaring numbers.\n");
  9.         Console.WriteLine("Iterations | a*a Time | Math.Pow(a, 2) Time");
  10.         Console.WriteLine("-----------|----------|--------------------");
  11.  
  12.         int iterations = 100000;
  13.         double number = 3092d;
  14.         double result;
  15.  
  16.         while(true)
  17.         {
  18.             Stopwatch timera = Stopwatch.StartNew();
  19.  
  20.             for(int i = 0; i < iterations; i++)
  21.             {
  22.                 result = number*number;
  23.             }
  24.             timera.Stop();
  25.  
  26.             Stopwatch timerb = Stopwatch.StartNew();
  27.             for(int i = 0; i < iterations; i++)
  28.             {
  29.                 result = Math.Pow(number, 2);
  30.             }
  31.             timerb.Stop();
  32.  
  33.             Console.WriteLine(" {0,9} | {1,8} | {2}", iterations, timera.ElapsedMilliseconds.ToString(),
  34. timerb.ElapsedMilliseconds.ToString());
  35.            
  36.             iterations += 100000;
  37.            
  38.             System.Threading.Thread.Sleep(500); // make sure that we don't crash the server
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement