Skip to content

Instantly share code, notes, and snippets.

@airbreather
Last active February 28, 2016 20:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save airbreather/0b183895cdbfa7c99777 to your computer and use it in GitHub Desktop.
Measurements for SO answer with ID 32025538
// comment this out to see how comparatively expensive it is if all you have are properties.
#define FIELDS
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
struct Point
{
#if FIELDS
internal double X;
internal double Y;
#else
internal double X { get; set; }
internal double Y { get; set; }
#endif
}
class Program
{
static void Main(string[] args)
{
Point[] points = new Point[1000000];
Initialize(points);
// warm up
TransformAllPoints(points);
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 5000; i++)
{
TransformAllPoints(points);
}
sw.Stop();
Console.WriteLine("Took {0} seconds.", sw.ElapsedTicks / (double)Stopwatch.Frequency);
}
static void TransformAllPoints(Point[] points)
{
for (int i = 0; i < points.Length; i++)
{
#if FIELDS
TransformPoint(ref points[i].X, ref points[i].Y);
#else
double x = points[i].X;
double y = points[i].Y;
TransformPoint(ref x, ref y);
points[i].X = x;
points[i].Y = y;
#endif
}
}
static void TransformPoint(ref double x, ref double y)
{
x++;
y++;
}
static void Initialize(Point[] points)
{
Random rand = new Random(32025538);
for (int i = 0; i < points.Length; i++)
{
points[i].X = rand.NextDouble() * 13;
points[i].Y = rand.NextDouble() * 13;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment