~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Analysis/Profiler/Tests/Benchmark/Benchmark.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Diagnostics;
 
6
using System.IO;
 
7
using System.Runtime.CompilerServices;
 
8
using System.Threading;
 
9
 
 
10
namespace Benchmark
 
11
{
 
12
        class Program
 
13
        {
 
14
                static bool automated;
 
15
                static StreamWriter automationOutput;
 
16
                
 
17
                public static void Main(string[] args)
 
18
                {
 
19
                        automated = args.Length == 1 && args[0] == "/automated";
 
20
                        if (automated) {
 
21
                                automationOutput = new StreamWriter("benchmark.out");
 
22
                        }
 
23
                        
 
24
                        Stopwatch w = new Stopwatch();
 
25
                        w.Start();
 
26
                        
 
27
                        Test(SwapAndVirtualCalls);
 
28
                        Test(BadRecursion);
 
29
                        Test(QuickSort);
 
30
                        Test(FloatCalculation);
 
31
                        
 
32
                        w.Stop();
 
33
                        WriteLine("Total tests time: " + FormatTime(totalTime.TotalMilliseconds));
 
34
                        WriteLine("Total time: " + FormatTime(w.Elapsed.TotalMilliseconds));
 
35
                        
 
36
                        if (automated) {
 
37
                                automationOutput.Close();
 
38
                        } else {
 
39
                                Console.Write("Press any key to continue . . . ");
 
40
                                Console.ReadKey(true);
 
41
                        }
 
42
                }
 
43
                
 
44
                static void WriteLine(string text)
 
45
                {
 
46
                        Console.WriteLine(text);
 
47
                        if (automated)
 
48
                                automationOutput.WriteLine(text);
 
49
                }
 
50
                
 
51
                static TimeSpan totalTime = TimeSpan.Zero;
 
52
                
 
53
                static void Test(Action action)
 
54
                {
 
55
                        Stopwatch w = new Stopwatch();
 
56
                        w.Start();
 
57
                        action();
 
58
                        w.Stop();
 
59
                        WriteLine(action.Method.Name + ": " + FormatTime(w.Elapsed.TotalMilliseconds));
 
60
                        totalTime += w.Elapsed;
 
61
                }
 
62
                
 
63
                static string FormatTime(double milliseconds)
 
64
                {
 
65
                        return milliseconds.ToString("f6", System.Globalization.NumberFormatInfo.InvariantInfo) + "ms";
 
66
                }
 
67
                
 
68
                #region SwapAndVirtualCalls
 
69
                static void SwapAndVirtualCalls()
 
70
                {
 
71
                        BaseClass a = new DerivedClass1();
 
72
                        BaseClass b = new DerivedClass2();
 
73
                        for (int i = 0; i < 50000; i++) {
 
74
                                a.Test();
 
75
                                Swap(ref a, ref b);
 
76
                        }
 
77
                }
 
78
                
 
79
                [MethodImpl(MethodImplOptions.NoInlining)]
 
80
                static void Swap<T>(ref T a, ref T b)
 
81
                {
 
82
                        T t = a;
 
83
                        a = b;
 
84
                        b = t;
 
85
                }
 
86
                
 
87
                public abstract class BaseClass
 
88
                {
 
89
                        public abstract void Test();
 
90
                }
 
91
                
 
92
                public class DerivedClass1 : BaseClass
 
93
                {
 
94
                        public override void Test()
 
95
                        {
 
96
                        }
 
97
                }
 
98
                
 
99
                public class DerivedClass2 : BaseClass
 
100
                {
 
101
                        public override void Test()
 
102
                        {
 
103
                        }
 
104
                }
 
105
                #endregion
 
106
                
 
107
                #region BadRecursion
 
108
                static void BadRecursion()
 
109
                {
 
110
                        BadRecursion_A(17);
 
111
                }
 
112
                
 
113
                [MethodImpl(MethodImplOptions.NoInlining)]
 
114
                static void BadRecursion_A(int level)
 
115
                {
 
116
                        if (level == 0) return;
 
117
                        BadRecursion_A(level - 1);
 
118
                        BadRecursion_B(level - 1);
 
119
                }
 
120
                
 
121
                [MethodImpl(MethodImplOptions.NoInlining)]
 
122
                static void BadRecursion_B(int level)
 
123
                {
 
124
                        if (level == 0) return;
 
125
                        BadRecursion_A(level - 1);
 
126
                        BadRecursion_B(level - 1);
 
127
                }
 
128
                #endregion
 
129
                
 
130
                #region QuickSort
 
131
                static void QuickSort()
 
132
                {
 
133
                        int[] data = new int[100000];
 
134
                        for (int i = 0; i < data.Length; i++) {
 
135
                                data[i] = unchecked( i * 165185 );
 
136
                        }
 
137
                        new QuickSortHelper(data).Sort();
 
138
                }
 
139
                
 
140
                public class QuickSortHelper
 
141
                {
 
142
                        int[] array;
 
143
                        
 
144
                        public QuickSortHelper(int[] array)
 
145
                        {
 
146
                                this.array = array;
 
147
                        }
 
148
                        
 
149
                        public void Sort()
 
150
                        {
 
151
                                Sort(0, this.array.Length - 1);
 
152
                        }
 
153
                        
 
154
                        int Partition(int left, int right)
 
155
                        {
 
156
                                int value = this.array[right];
 
157
                                int i = left, j = right - 1;
 
158
                                
 
159
                                do
 
160
                                {
 
161
                                        while (i < right && this.array[i] <= value)
 
162
                                                i++;
 
163
                                        
 
164
                                        while (j > left && this.array[j] >= value)
 
165
                                                j--;
 
166
                                        if (i < j) {
 
167
                                                int help = this.array[i];
 
168
                                                this.array[i] = this.array[j];
 
169
                                                this.array[j] = help;
 
170
                                        }
 
171
                                } while (i < j);
 
172
                                
 
173
                                if (this.array[i] > value) {
 
174
                                        int help = this.array[right];
 
175
                                        this.array[right] = this.array[i];
 
176
                                        this.array[i] = help;
 
177
                                }
 
178
                                
 
179
                                return i;
 
180
                        }
 
181
                        
 
182
                        [MethodImpl(MethodImplOptions.NoInlining)]
 
183
                        void Sort(int left, int right)
 
184
                        {
 
185
                                if (left < right) {
 
186
                                        int i = Partition(left, right);
 
187
                                        Sort(left, i - 1);
 
188
                                        Sort(i + 1, right);
 
189
                                }
 
190
                        }
 
191
                }
 
192
                #endregion
 
193
                
 
194
                #region FloatCalculation
 
195
                [MethodImpl(MethodImplOptions.NoInlining)]
 
196
                static void FloatCalculation()
 
197
                {
 
198
                        double c = 0;
 
199
                        double sum = 0;
 
200
                        for (int i = 0; i < 10000; i++) {
 
201
                                sum += GetNextDouble(ref c);
 
202
                        }
 
203
                        if (sum != 50005000)
 
204
                                throw new Exception("Calculation failed!");
 
205
                }
 
206
                
 
207
                
 
208
                [MethodImpl(MethodImplOptions.NoInlining)]
 
209
                static double GetNextDouble(ref double counter)
 
210
                {
 
211
                        return ++counter;
 
212
                }
 
213
                #endregion
 
214
        }
 
215
}