Skip to content

Instantly share code, notes, and snippets.

@brandonprry
Created August 29, 2013 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonprry/6373428 to your computer and use it in GitHub Desktop.
Save brandonprry/6373428 to your computer and use it in GitHub Desktop.
using System;
namespace floor
{
class MainClass
{
public static void Main (string[] args)
{
Random r = new Random ();
double floorMillis = 0;
double truncMillis = 0;
double floorMin = double.MaxValue;
double floorMax = 0;
double truncMin = double.MaxValue;
double truncMax = 0;
for (int k = 0; k < 1000; k++) {
Console.WriteLine(k);
DateTime start = DateTime.Now;
for (int i = 0; i < 50000; i++) {
int whole = r.Next (5000);
double dec = r.NextDouble ();
decimal d = whole + (decimal)dec;
Math.Truncate (d);
}
TimeSpan truncEnd = DateTime.Now - start;
truncMillis += truncEnd.TotalMilliseconds;
truncMax = (truncMax < truncEnd.TotalMilliseconds ? truncEnd.TotalMilliseconds : truncMax);
truncMin = (truncMin > truncEnd.TotalMilliseconds ? truncEnd.TotalMilliseconds : truncMin);
start = DateTime.Now;
for (int i = 0; i < 50000; i++) {
int whole = r.Next (5000);
double dec = r.NextDouble ();
decimal d = whole + (decimal)dec;
Math.Floor (d);
}
TimeSpan floorEnd = DateTime.Now - start;
floorMillis += floorEnd.TotalMilliseconds;
floorMax = (floorMax < floorEnd.TotalMilliseconds ? floorEnd.TotalMilliseconds : floorMax);
floorMin = (floorMin > floorEnd.TotalMilliseconds ? floorEnd.TotalMilliseconds : floorMin);
}
Console.WriteLine("Average for 50000 truncations in ms: " + truncMillis/1000);
Console.WriteLine("Trunc max: " + truncMax + "\tTrunc min: " + truncMin);
Console.WriteLine("\nAverage for 50000 floors in ms: " + floorMillis/1000);
Console.WriteLine("Floor max: " + floorMax + "\tFloor min: " + floorMin);
}
}
}
----- Time taken with decimal.Parse
Average for 50000 truncations in ms: 141.02773
Trunc max: 199.25 Trunc min: 77.809
Average for 50000 floors in ms: 143.033939
Floor max: 201.471 Floor min: 77.31
------ Time taken with cast
Average for 50000 truncations in ms: 154.93566
Trunc max: 222.129 Trunc min: 121.525
Average for 50000 floors in ms: 151.330833
Floor max: 227.499 Floor min: 120.121
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment