Skip to content

Instantly share code, notes, and snippets.

@bradphelan
Last active July 7, 2017 06:55
Show Gist options
  • Save bradphelan/9b383c8e99edc38068fcc0dccc8a7b48 to your computer and use it in GitHub Desktop.
Save bradphelan/9b383c8e99edc38068fcc0dccc8a7b48 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
namespace StructPerformance
{
internal class Program
{
private static void Main(string[] args)
{
Test();
}
static int N = 1223;
static uint jump = 113;
static uint index = 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int next()
{
index += jump;
return (int)(index % N);
}
static Point3Class[] cla = Enumerable.Range(0, N).Select(i => new Point3Class(i, i + 1, i + 2)).ToArray();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static Point3Class cl() => cla[next()];
static Point3Struct[] sta = Enumerable.Range(0, N).Select(i => new Point3Struct(i, i + 1, i + 2)).ToArray();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static Point3Struct st() => sta[next()];
public static void Test()
{
const double x = 11, y = 12, z = 13;
const int n = 100000000;
for (var j = 0; j < 5; j++)
{
double acc = 0;
var sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
acc += Point3Class.Dot(cl(), cl());
}
Console.WriteLine($"Dot(cl,cl):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
{
Point3Class acc3 = new Point3Class( 0,0,0 );
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
acc3 = Point3Class.Add(acc3,Point3Class.Add(cl(), cl()));
}
Console.WriteLine($"Add(cl,cl):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
}
acc = 0;
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
acc += Point3Struct.Dot(st(), st());
}
Console.WriteLine($"Dot(st, st):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
{
Point3Struct acc3 = new Point3Struct( 0,0,0 );
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
acc3 = Point3Struct.Add(acc3,Point3Struct.Add(st(), st()));
}
Console.WriteLine($"Add(st,st):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
}
acc = 0;
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
cl(); // Just to be sure to have the same overhead
acc += Point3Class.Dot(cl());
}
Console.WriteLine($"Dot(cl):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
acc = 0;
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
st(); // Just to be sure to have the same overhead
acc += Point3Struct.Dot(st());
}
Console.WriteLine($"Dot(st):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
acc = 0;
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
st();
st(); // Just to be sure to have the same overhead
acc += Dot(x, y, z, x, y, z);
}
Console.WriteLine($"Dot(xyz):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
acc = 0;
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
var t = cl();
cl(); // just to be sure to have the same overhead
acc += Dot(t.X, t.Y, t.Z, t.X, t.Y, t.Z );
}
Console.WriteLine($"Dot(cl.xyz):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
acc = 0;
sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
var t = st();
st(); // just to be sure to have the same overhead
acc += Dot(t.X, t.Y, t.Z, t.X, t.Y, t.Z);
}
Console.WriteLine($"Dot(st.xyz):\t\t{sw.ElapsedMilliseconds}ms,\tres: {acc}");
Console.WriteLine("____________________________________________________");
Console.WriteLine();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Dot(double ax, double ay, double az, double bx, double by, double bz)
{
return ax * bx + ay * by + az * bz;
}
}
public class Point3Class
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Point3Class(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point3Class Add
(Point3Class a, Point3Class b) => new Point3Class( a.X + b.X, a.Y + b.Y, a.Z + b.Z );
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Dot(Point3Class a, Point3Class b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Dot(Point3Class a)
{
return a.X * a.X + a.Y * a.Y + a.Z * a.Z;
}
}
public struct Point3Struct
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Point3Struct(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point3Struct Add
(Point3Struct a, Point3Struct b) => new Point3Struct( a.X + b.X, a.Y + b.Y, a.Z + b.Z );
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Dot(Point3Struct a, Point3Struct b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Dot(Point3Struct a)
{
return a.X * a.X + a.Y * a.Y + a.Z * a.Z;
}
}
}
Dot(cl,cl): 1909ms, res: 130942086576020
Add(cl,cl): 2777ms, res: 130942086576020
Dot(st, st): 2108ms, res: 130942091180147
Add(st,st): 2457ms, res: 130942091180147
Dot(cl): 1949ms, res: 149756581443233
Dot(st): 2050ms, res: 149756595813533
Dot(xyz): 1951ms, res: 43400000000
Dot(cl.xyz): 2004ms, res: 149756576701475
Dot(st.xyz): 2043ms, res: 149756587592393
____________________________________________________
Dot(cl,cl): 1919ms, res: 130942089687095
Add(cl,cl): 2818ms, res: 130942089687095
Dot(st, st): 2050ms, res: 130942081191395
Add(st,st): 2375ms, res: 130942081191395
Dot(cl): 1883ms, res: 149756585218877
Dot(st): 2023ms, res: 149756590028306
Dot(xyz): 1900ms, res: 43400000000
Dot(cl.xyz): 1945ms, res: 149756585516021
Dot(st.xyz): 1994ms, res: 149756582373833
____________________________________________________
Dot(cl,cl): 1878ms, res: 130942099529669
Add(cl,cl): 2727ms, res: 130942099529669
Dot(st, st): 2075ms, res: 130942087147769
Add(st,st): 2433ms, res: 130942087147769
Dot(cl): 1958ms, res: 149756581207352
Dot(st): 2069ms, res: 149756586706955
Dot(xyz): 1947ms, res: 43400000000
Dot(cl.xyz): 1990ms, res: 149756588404910
Dot(st.xyz): 2035ms, res: 149756599731773
____________________________________________________
Dot(cl,cl): 1898ms, res: 130942093553492
Add(cl,cl): 2729ms, res: 130942093553492
Dot(st, st): 2034ms, res: 130942089822422
Add(st,st): 2375ms, res: 130942089822422
Dot(cl): 1886ms, res: 149756589542480
Dot(st): 2011ms, res: 149756595526199
Dot(xyz): 1911ms, res: 43400000000
Dot(cl.xyz): 1953ms, res: 149756579041031
Dot(st.xyz): 2003ms, res: 149756586589724
____________________________________________________
Dot(cl,cl): 1888ms, res: 130942084365890
Add(cl,cl): 2727ms, res: 130942084365890
Dot(st, st): 2034ms, res: 130942087363757
Add(st,st): 2381ms, res: 130942087363757
Dot(cl): 1885ms, res: 149756584537721
Dot(st): 2017ms, res: 149756577190250
Dot(xyz): 1942ms, res: 43400000000
Dot(cl.xyz): 1967ms, res: 149756588671991
Dot(st.xyz): 2006ms, res: 149756594418443
____________________________________________________
Press any key to continue . . .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment