1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Diagnostics;
7
using System.Runtime.CompilerServices;
8
using System.Threading;
14
static bool automated;
15
static StreamWriter automationOutput;
17
public static void Main(string[] args)
19
automated = args.Length == 1 && args[0] == "/automated";
21
automationOutput = new StreamWriter("benchmark.out");
24
Stopwatch w = new Stopwatch();
27
Test(SwapAndVirtualCalls);
30
Test(FloatCalculation);
33
WriteLine("Total tests time: " + FormatTime(totalTime.TotalMilliseconds));
34
WriteLine("Total time: " + FormatTime(w.Elapsed.TotalMilliseconds));
37
automationOutput.Close();
39
Console.Write("Press any key to continue . . . ");
40
Console.ReadKey(true);
44
static void WriteLine(string text)
46
Console.WriteLine(text);
48
automationOutput.WriteLine(text);
51
static TimeSpan totalTime = TimeSpan.Zero;
53
static void Test(Action action)
55
Stopwatch w = new Stopwatch();
59
WriteLine(action.Method.Name + ": " + FormatTime(w.Elapsed.TotalMilliseconds));
60
totalTime += w.Elapsed;
63
static string FormatTime(double milliseconds)
65
return milliseconds.ToString("f6", System.Globalization.NumberFormatInfo.InvariantInfo) + "ms";
68
#region SwapAndVirtualCalls
69
static void SwapAndVirtualCalls()
71
BaseClass a = new DerivedClass1();
72
BaseClass b = new DerivedClass2();
73
for (int i = 0; i < 50000; i++) {
79
[MethodImpl(MethodImplOptions.NoInlining)]
80
static void Swap<T>(ref T a, ref T b)
87
public abstract class BaseClass
89
public abstract void Test();
92
public class DerivedClass1 : BaseClass
94
public override void Test()
99
public class DerivedClass2 : BaseClass
101
public override void Test()
108
static void BadRecursion()
113
[MethodImpl(MethodImplOptions.NoInlining)]
114
static void BadRecursion_A(int level)
116
if (level == 0) return;
117
BadRecursion_A(level - 1);
118
BadRecursion_B(level - 1);
121
[MethodImpl(MethodImplOptions.NoInlining)]
122
static void BadRecursion_B(int level)
124
if (level == 0) return;
125
BadRecursion_A(level - 1);
126
BadRecursion_B(level - 1);
131
static void QuickSort()
133
int[] data = new int[100000];
134
for (int i = 0; i < data.Length; i++) {
135
data[i] = unchecked( i * 165185 );
137
new QuickSortHelper(data).Sort();
140
public class QuickSortHelper
144
public QuickSortHelper(int[] array)
151
Sort(0, this.array.Length - 1);
154
int Partition(int left, int right)
156
int value = this.array[right];
157
int i = left, j = right - 1;
161
while (i < right && this.array[i] <= value)
164
while (j > left && this.array[j] >= value)
167
int help = this.array[i];
168
this.array[i] = this.array[j];
169
this.array[j] = help;
173
if (this.array[i] > value) {
174
int help = this.array[right];
175
this.array[right] = this.array[i];
176
this.array[i] = help;
182
[MethodImpl(MethodImplOptions.NoInlining)]
183
void Sort(int left, int right)
186
int i = Partition(left, right);
194
#region FloatCalculation
195
[MethodImpl(MethodImplOptions.NoInlining)]
196
static void FloatCalculation()
200
for (int i = 0; i < 10000; i++) {
201
sum += GetNextDouble(ref c);
204
throw new Exception("Calculation failed!");
208
[MethodImpl(MethodImplOptions.NoInlining)]
209
static double GetNextDouble(ref double counter)