Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Runtime.CompilerServices;
- public static class CSharpTest
- {
- private const int ITERATIONS = 1000 * 1000 * 1000;
- public static void Main()
- {
- PrerequisiteCheck();
- Console.WriteLine("running...");
- Test1();
- Test2();
- Console.WriteLine();
- Test1();
- Test2();
- Console.WriteLine("all done");
- }
- // I am having the issue with .NET 4.5.2 x86
- private static void PrerequisiteCheck()
- {
- if (Environment.Version.Major < 4)
- throw new InvalidOperationException("it seems you need a higher .net version");
- if (Environment.Is64BitProcess)
- throw new InvalidOperationException("try setting the platform to x86 instead");
- #if DEBUG
- throw new InvalidOperationException("and btw this is not a release build");
- #endif
- }
- private static void Test1()
- {
- Point a = new Point(1, 1), b = new Point(1, 1);
- var sw = Stopwatch.StartNew();
- for (int i = 0; i < ITERATIONS; i++)
- a = AddByVal(a, b);
- sw.Stop();
- Console.WriteLine("Test1: x={0} y={1}, Time elapsed: {2} ms", a.X, a.Y, sw.ElapsedMilliseconds);
- }
- private static void Test2()
- {
- var magical_speed_booster = Stopwatch.StartNew();
- {
- Point a = new Point(1, 1), b = new Point(1, 1);
- var sw = Stopwatch.StartNew();
- for (int i = 0; i < ITERATIONS; i++)
- a = AddByVal(a, b);
- sw.Stop();
- Console.WriteLine("Test2: x={0} y={1}, Time elapsed: {2} ms", a.X, a.Y, sw.ElapsedMilliseconds);
- }
- magical_speed_booster.Stop();
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static Point AddByVal(Point a, Point b)
- {
- return new Point(a.X + b.Y, a.Y + b.X);
- }
- }
- public struct Point
- {
- public Point(double x, double y) { X = x; Y = y; }
- public readonly double X;
- public readonly double Y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement