Last active
August 29, 2015 14:02
-
-
Save ArcticEcho/bc701d47ddfc03cfb401 to your computer and use it in GitHub Desktop.
Run in release config, without optimizations, without debugger & ignore results from first build. (And obviously don't have anything running in the background).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
// WARNING: For the code below to exacute properly (without getting a SOE) this program must be | |
// a full-trust app; see, project properties > security > enable "ClickOnce security setting" | |
// and check the "This is a full trust application" radio button. | |
namespace C_Sharp_Benchmarker | |
{ | |
class Program | |
{ | |
private const long testIterations = 100; | |
private const long repeatIterations = 20; | |
public static void Main(string[] args) | |
{ | |
new Thread(() => | |
{ | |
var r1 = 0M; | |
var r2 = 0M; | |
var r3 = 0M; | |
for (long i = 0; i < repeatIterations; i++) | |
{ | |
r1 += Test1(); | |
r2 += Test2(); | |
r3 += Test3(); | |
Console.WriteLine(); | |
} | |
Console.Write("\n\nTest 1 total duration: " + r1.ToString() + "\nTest 2 total duration: " + r2.ToString() + "\nTest 3 total duration: " + r3.ToString() + "\n\nFastest: "); | |
if (r1 < r2 && r1 < r3) Console.WriteLine("Stack"); | |
if (r2 < r1 && r2 < r3) Console.WriteLine("Global"); | |
if (r3 < r1 && r3 < r2) Console.WriteLine("Heap"); | |
Console.ReadLine(); | |
}, 50000000).Start(); | |
} | |
public static long Test1() | |
{ | |
Console.Write("Running test 1....."); | |
var s = new Stopwatch(); | |
s.Start(); | |
for (long i = 0; i < testIterations; i++) | |
{ | |
TestMethod1(); | |
} | |
s.Stop(); | |
Console.WriteLine("Duration: " + s.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture) + " milliseconds"); | |
return s.ElapsedTicks; | |
} | |
public static long Test2() | |
{ | |
Console.Write("Running test 2....."); | |
var s = new Stopwatch(); | |
s.Start(); | |
for (long i = 0; i < testIterations; i++) | |
{ | |
TestMethod2(); | |
} | |
s.Stop(); | |
Console.WriteLine("Duration: " + s.ElapsedMilliseconds.ToString() + " milliseconds"); | |
return s.ElapsedTicks; | |
} | |
public static long Test3() | |
{ | |
Console.Write("Running test 3....."); | |
var s = new Stopwatch(); | |
s.Start(); | |
for (long i = 0; i < testIterations; i++) | |
{ | |
TestMethod3(); | |
} | |
s.Stop(); | |
Console.WriteLine("Duration: " + s.ElapsedMilliseconds.ToString() + " milliseconds"); | |
return s.ElapsedTicks; | |
} | |
public static unsafe void TestMethod1() | |
{ | |
float* samples = stackalloc float[1250000]; | |
for (var ii = 0; ii < 1250000; ii++) | |
{ | |
samples[ii] = 3.39060927590325f; | |
} | |
} | |
public unsafe static void TestMethod2() | |
{ | |
var ptr = Marshal.AllocHGlobal(50000000); | |
try | |
{ | |
float* samples = (float*)ptr; | |
for (var ii = 0; ii < 1250000; ii++) | |
{ | |
samples[ii] = 3.39060927590325f; | |
} | |
} | |
finally | |
{ | |
Marshal.FreeHGlobal(ptr); | |
} | |
} | |
public static unsafe void TestMethod3() | |
{ | |
float[] samples = new float[1250000]; | |
for (var ii = 0; ii < 1250000; ii++) | |
{ | |
samples[ii] = 3.39060927590325f; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: when
testIterations
= 10 & whentestIterations
= 20.