-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeasureIt.cs
2035 lines (1852 loc) · 79.2 KB
/
MeasureIt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************/
/* MeasureIt.cs */
/***************************************************************************/
/* A harness that does a bunch of interesting performance experiments
* with the .NET Runtime
Copyright (c) Microsoft Corporation. All rights reserved.
AUTHOR: Vance Morrison Date : 10/20/2007 */
/* This program uses code hyperlinks available as part of the HyperAddin */
/* Visual Studio plug-in. It is available from http://www.codeplex.com/hyperAddin */
/***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using PerformanceMeasurement;
using System.Reflection;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Security;
using System.Diagnostics;
using System.Text.RegularExpressions;
/* files are best browsed in 'outline form'. You outline code with Ctrl-M Ctrl-O. */
/* See code:MeasureIt to get started */
/// <summary>
/// MeasureIt is a simple harness that does a bunch of microbenchmark
/// performance experiments on the .NET runtime. It leverages the
/// code:MultiSampleCodeTimer to do the measuments and code:StatsLogger to
/// collect the results and display them as a HTML page.
///
/// The structure of MeasureIt is simple: There is a set of static functions
/// called Measure* which are a set of related measurements. The main program
/// code:MeasureIt.Main is responible for parsing command line argumends and
/// calling the Measure* functions. Use 'MeasureTheRuntime /? for usage.
///
/// Adding more measurements is as easy as adding another 'Measure*' static
/// method. Once the method exists the Main program will automatically call it
/// (it uses reflection to do this).
///
/// #Tips for writing a good microbenchmark
///
/// The most important aspect of writing a good microbenchmark is insuring
/// the time interval that you are measuruing is reasonably large (eg at
/// least 100us or more) It is already the case that the code:CodeTimer
/// helps by executing the code to measure many time (1000 in the
/// benchmarks below), however since even looping has overheadUsec (codeTimer
/// do their best to correct for it, but it can only do so much). Thus it
/// is best that what you want to measure is large in comparison to the
/// overheadUsec looping.
///
/// You will see that in the code below often the thing being measured is
/// cloned 10 times. This has two good effects. The first is that it
/// increases the time interval being measured (as discussed above). But it
/// turns out that very small loops are prone to the exact layout of memory
/// and cache lines, leading to unreproducable results. By cloning the test
/// 10 times you reduce this effect considerably.
///
/// Even with all these mitgations, large run-to-run variations are
/// definately possible (although I have tried to control all the effects I
/// know about). Always take performance numbers as suspect until enough
/// data is collected and the results can be modeled (predicted), but some
/// reasonable rationale.
///
/// Understand the inherent limitation of microbencharks. They only test
/// one very specific aspect of performance, and often other factors are
/// much more important in real programs. In particular microbencharks are
/// almost never memory cache limited, but real programs tend to be. Also
/// microbenchmarks don't tend to give 'real' inputs to the APIs they test.
/// Often the exact inputs don't matter, but for some APIs (eg hash tables,
/// sorting), it can be a big issue.
///
/// It also pays to step through the code you are profiling in the debugger
/// to make certain that you are not being blindsided by something. The JIT
/// may have optimized what you wanted to measure away completely, or there
/// may be other unusual overheadUsec you are not aware of. This is all part of
/// the requirement to develop a model that explains the performance data
/// you get, and to not REALLY believe the numbers until you have created
/// such a model.
///
/// As a way of cutting down on variation, I don't suggest believing
/// numbers that were gathered by running he appliation under a debugger.
/// In theory it should not matter, but I have seen cases where it does,
/// and until I understand just exactly when it matters, I would do all
/// measurment by running the application normally.
///
/// Note that you will get sizable differences depending on exacty which
/// CPU you do the run. Newer CPUs from the same manufacturer can also be
/// different, so keep that in mind when looking at the numbers.
/// </summary>
public static class MeasureIt
{
// Helper method to be called from LinqPad
static public string FromLinqPad()
{
Main(new String[0]);
// returning a string instead of void so linqpad .Dump() is not required when in Expression mode.
return "Use LinqpadUX instead";
}
// Uncomment the following attribute to test Addomain shared code.
// [LoaderOptimization(LoaderOptimization.MultiDomain)]
static public int Main(string[] args)
{
bool skipMachineStats = false;
List<MethodInfo> areas = new List<MethodInfo>();
for (int i = 0; i < args.Length; i++)
{
if (!args[i].StartsWith("/"))
{
MethodInfo area = GetAreaMethod(args[i]);
if (area == null)
{
Console.WriteLine("Unrecognised area '" + args[i] + "'. Use /? for list.");
return 1;
}
areas.Add(area);
}
else if (string.Compare(args[i], "/skipMachineStats", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(args[i], "/s") == 0)
skipMachineStats = true;
else if (string.Compare(args[i], "/UsersGuide", StringComparison.OrdinalIgnoreCase) == 0)
UsersGuide.DisplayConsoleAppUsersGuide("UsersGuide.htm");
else if (string.Compare(args[i], "/SetHighPerf", StringComparison.OrdinalIgnoreCase) == 0)
return PowerManagment.Set(PowerManagment.HighPerformance) ? 0 : 1;
else if (string.Compare(args[i], "/SetBalenced", StringComparison.OrdinalIgnoreCase) == 0)
return PowerManagment.Set(PowerManagment.Balenced) ? 0 : 1;
else if (string.Compare(args[i], "/SetPowerSaver", StringComparison.OrdinalIgnoreCase) == 0)
return PowerManagment.Set(PowerManagment.PowerSaver) ? 0 : 1;
else if (string.Compare(args[i], "/Edit", StringComparison.OrdinalIgnoreCase) == 0)
{
string srcDir = UnpackAttachedSource(null);
string programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
string slnFile = null;
// To avoid the conversion dialog, use the V9 (VS2008)
string vs9 = Path.Combine(programFiles, "Microsoft Visual Studio 9.0");
if (Directory.Exists(vs9))
slnFile = Path.Combine(srcDir, "MeasureIt.V9.sln");
else
{
string vs8 = Path.Combine(programFiles, "Microsoft Visual Studio 8");
if (Directory.Exists(vs8))
slnFile = Path.Combine(srcDir, "MeasureIt.sln");
}
if (slnFile != null)
{
Console.WriteLine("Launching solution " + slnFile);
Process process = new Process();
process.StartInfo = new ProcessStartInfo(slnFile);
process.Start();
}
return 0;
}
else if (string.Compare(args[i], "/?", StringComparison.OrdinalIgnoreCase) == 0)
{
Console.WriteLine("Usage MeasureTheRuntime [/?] [/usersGuide] [/skipMachineStats] [areas]");
Console.WriteLine();
Console.WriteLine("Runs a set of microbenchmarks.");
Console.WriteLine();
Console.WriteLine("Qualifiers:");
Console.WriteLine(" /edit Unpack and edit the source code. ");
Console.WriteLine(" /userGuide Display the users guide. ");
Console.WriteLine(" /setHighPerf Set power policy to 'High Performance'. ");
Console.WriteLine(" /setBalenced Set power policy to 'Balenced'. ");
Console.WriteLine(" /setPowerSaver Set power policy to 'Power Saver'. ");
Console.WriteLine(" /skipMachineStats (/s) Speeds startup by skipping machine stats.");
Console.WriteLine();
Console.WriteLine("Areas: (no area means all these areas)");
foreach (MethodInfo areaMethod in GetAreaMethods(false))
Console.WriteLine(" " + areaMethod.Name.Substring(7));
Console.WriteLine();
Console.WriteLine("Private Areas: (Must be explicitly mentioned on command line.)");
foreach (MethodInfo areaMethod in GetAreaMethods(true))
Console.WriteLine(" " + areaMethod.Name.Substring(7));
return 1;
}
else
{
Console.WriteLine("Unrecognised qualifer " + args[i] + " use /? for usage.");
return 1;
}
}
if (areas.Count == 0)
areas = GetAreaMethods(false);
StatsCollection data = new StatsCollection();
logger = new StatsLogger(data);
Console.WriteLine("Collecting Stats on the computer.");
logger.CaptureCurrentMachineInfo(Assembly.GetExecutingAssembly(), skipMachineStats);
string reportFileName = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "MeasureIt.html");
RunAtHighPerfPolicy(delegate
{
Stats methodCallStats = RunTests(areas);
logger.Scale = methodCallStats.Median;
logger.UnitsDescription = "Scaled where EmptyStaticFunction = 1.0 (" +
(logger.Scale * 1000).ToString("f1") + " nsec = 1.0 units)";
Console.WriteLine("Writing report to " + reportFileName + ".");
logger.DisplayHtmlReport(reportFileName);
Console.WriteLine("Launching Internet Explorer on " + reportFileName + ".");
});
StatsLogger.LaunchIE(reportFileName); Console.WriteLine("Done.");
return 0;
}
private static Stats RunTests(List<MethodInfo> areas)
{
Console.WriteLine("Measuring interesting runtime operations. Use /? or /usersGuide for more help.");
timer1000 = new MultiSampleCodeTimer(10, 1000);
timer1000.OnMeasure += logger.AddWithCount;
timer1 = new MultiSampleCodeTimer(10, 1);
timer1.OnMeasure += logger.AddWithCount;
timer100 = new MultiSampleCodeTimer(10, 100);
timer100.OnMeasure += logger.AddWithCount;
timer10 = new MultiSampleCodeTimer(10, 10);
timer10.OnMeasure += logger.AddWithCount;
// Just to show you the amount of measurement error you get (since it should be 0)
timer1000.Measure("NOTHING", 1, delegate
{
});
// Do this one unconditionally, because it is used as the baseline for everything else.
Stats methodCallStats = timer1000.Measure("MethodCalls: EmptyStaticFunction()", 10, delegate
{
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
Class.EmptyStaticFunction();
});
// This also has the nice effect of kicking the CPU out of any low
// power state.
int dummy;
timer1000.Measure("Loop 1K times", 1, delegate
{
int k = 0;
while (k < 1000)
k++; // still in danger of being optimized.
dummy = k; // avoid optimization.
});
foreach (MethodInfo areaMethod in areas)
{
string area = areaMethod.Name.Substring(7);
Console.WriteLine("Running " + area);
logger.Category = area;
areaMethod.Invoke(null, null);
logger.Category = null;
}
return methodCallStats;
}
/* To add another suite of benchmarks, simply add a method that begins */
/* with 'Measure' to this class. If the method is public it will be */
/* run by default. Private methods must be explictly mentioned */
/* see file:UsersGuide.htm for more */
static public void MeasureMethodCalls()
{
Class aClass = new Class();
SealedClass aSealedClass = new SealedClass();
StructWithInterface aStructWithInterface = new StructWithInterface();
AnInterface aInterface = new Class();
SuperClass aSuperClass = new Class();
ValueType aValueType;
timer1000.Measure("EmptyStaticFunction(arg1,...arg5)", 10, delegate
{
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
Class.EmptyStaticFunction5Arg(1, 2, 3, 4, 5);
});
timer1000.Measure("aClass.EmptyInstanceFunction()", 10, delegate
{
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
aClass.EmptyInstanceFunction();
});
timer1000.Measure("aClass.Interface()", 10, delegate
{
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
aInterface.InterfaceMethod();
});
// note that these calls inline away completely, but that is what I wanted to show. Namely
// calling an interface method directly on a sealed class has no extra overheadUsec over a normal
// call
timer1000.Measure("aSealedClass.Interface() (inlined)", 10, delegate
{
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
aSealedClass.InterfaceMethod();
});
// note that these calls inline away completely, but that is what I wanted to show. Namely
// calling an interface method directly on a struct has no extra overheadUsec over a normal call
timer1000.Measure("aStructWithInterface.Interface() (inlined)", 10, delegate
{
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
aStructWithInterface.InterfaceMethod();
});
timer1000.Measure("aClass.VirtualMethod()", 10, delegate
{
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
aSuperClass.VirtualMethod();
});
timer1000.Measure("Class.ReturnsValueType()", 10, delegate
{
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
aValueType = Class.ReturnsValueType();
});
}
static public void MeasureFieldAccess()
{
Class aClass = new Class();
string aString = "aString";
timer1000.Measure("aStaticInt++", 10, delegate
{
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
Class.aStaticInt++;
});
timer1000.Measure("aClass.aInstanceInt++", 10, delegate
{
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
aClass.aInstanceInt++;
});
timer1000.Measure("aClass.aIntstanceString = \"hi\"", 10, delegate
{
string aLocalString = "hi";
Class local = aClass;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
local.aInstanceString = aLocalString;
});
timer1000.Measure("aStaticString = aString", 10, delegate
{
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
Class.aStaticString = aString;
});
}
static public void MeasureObjectOps()
{
Type classType = typeof(Class);
Class classInstance = new Class();
object aObjectString = "aString1";
bool aBool;
string aString;
timer1000.Measure("(aObjectString is String)", 10, delegate
{
bool b;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
b = aObjectString is String;
aBool = b;
});
timer1000.Measure("(aObjectString as String)", 10, delegate
{
string s;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
s = aObjectString as string;
aString = s;
});
timer1000.Measure("(string) aObjectString)", 10, delegate
{
string s;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
s = (string)aObjectString;
aString = s;
});
timer1000.Measure("new Class()", 10, delegate
{
new Class();
new Class();
new Class();
new Class();
new Class();
new Class();
new Class();
new Class();
new Class();
new Class();
});
timer1000.Measure("new FinalizableClass()", 10, delegate
{
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
new FinalizableClass();
});
timer1000.Measure("Activator.CreateInstance<Class>()", 10, delegate
{
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
Activator.CreateInstance<Class>();
});
timer1000.Measure("(Class) Activator.CreateInstance(classType)", 10, delegate
{
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
classInstance = (Class)Activator.CreateInstance(classType);
});
timer1000.Measure("(Class) classInstance.MemberWiseClone()", 10, delegate
{
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
classInstance = (Class)classInstance.Clone();
});
// TODO casts that fail.
}
static unsafe public void MeasureArrays()
{
int[] aIntArray = new int[20];
int aInt = 1;
AnInterface[] aInterfaceArray = new Class[10];
Class aClass = new Class();
string[] aStringArray = new string[10];
string aString = "foo";
timer1000.Measure("aIntArray[i] = 1", 10, delegate
{
aIntArray[0] = aInt;
aIntArray[1] = aInt;
aIntArray[2] = aInt;
aIntArray[3] = aInt;
aIntArray[4] = aInt;
aIntArray[5] = aInt;
aIntArray[6] = aInt;
aIntArray[7] = aInt;
aIntArray[8] = aInt;
aIntArray[9] = aInt;
});
timer1000.Measure("localIntPtr[i] = 1", 10, delegate
{
fixed (int* localIntPtr = aIntArray)
{
localIntPtr[0] = aInt;
localIntPtr[1] = aInt;
localIntPtr[2] = aInt;
localIntPtr[3] = aInt;
localIntPtr[4] = aInt;
localIntPtr[5] = aInt;
localIntPtr[6] = aInt;
localIntPtr[7] = aInt;
localIntPtr[8] = aInt;
localIntPtr[9] = aInt;
}
});
timer1000.Measure("string[i] = aString", 10, delegate
{
aStringArray[0] = aString;
aStringArray[1] = aString;
aStringArray[2] = aString;
aStringArray[3] = aString;
aStringArray[4] = aString;
aStringArray[5] = aString;
aStringArray[6] = aString;
aStringArray[7] = aString;
aStringArray[8] = aString;
aStringArray[9] = aString;
});
timer1000.Measure("aInterfaceArray[i] = aClass", 10, delegate
{
aInterfaceArray[0] = aClass;
aInterfaceArray[1] = aClass;
aInterfaceArray[2] = aClass;
aInterfaceArray[3] = aClass;
aInterfaceArray[4] = aClass;
aInterfaceArray[5] = aClass;
aInterfaceArray[6] = aClass;
aInterfaceArray[7] = aClass;
aInterfaceArray[8] = aClass;
aInterfaceArray[9] = aClass;
});
timer1000.Measure("1 for...Length aIntArray[i] = 1", 1, delegate
{
int[] localIntArray = aIntArray;
for (int i = 0; i < localIntArray.Length; i++)
localIntArray[i] = aInt;
});
timer1000.Measure("1 for...10 aIntArray[i] = 1", 1, delegate
{
int[] localIntArray = aIntArray;
for (int i = 0; i < 10; i++)
localIntArray[i] = aInt;
});
timer1000.Measure("1 for...Length aStringArray[i] = 1", 1, delegate
{
string[] localStringArray = aStringArray;
for (int i = 0; i < localStringArray.Length; i++)
localStringArray[i] = aString;
});
timer1000.Measure("1 for...Length aInterfaceArray[i] = 1", 1, delegate
{
AnInterface[] localInterfaceArray = aInterfaceArray;
for (int i = 0; i < localInterfaceArray.Length; i++)
localInterfaceArray[i] = aClass;
});
}
static public void MeasureDelegates()
{
Predicate<int> action = delegate(int i) { return i > 0; };
IAsyncResult result = action.BeginInvoke(3, null, null);
bool resultValue = action.EndInvoke(result);
Class aClass = new Class();
MyDelegate aInstanceDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
MyDelegate aStaticDelegate = new MyDelegate(Class.EmptyStaticFunction);
timer1000.Measure("new MyDelegate(aClass.EmptyInstanceFunction)", 10, delegate
{
MyDelegate aMyDelegate;
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
aMyDelegate = new MyDelegate(aClass.EmptyInstanceFunction);
});
timer1000.Measure("new MyDelegate(Class.StaticFunction)", 10, delegate
{
MyDelegate aMyDelegate;
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
aMyDelegate = new MyDelegate(Class.EmptyStaticFunction);
});
timer1000.Measure("aInstanceDelegate()", 10, delegate
{
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
aInstanceDelegate();
});
timer1000.Measure("aStaticDelegate()", 10, delegate
{
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
aStaticDelegate();
});
}
static public void MeasureEvents()
{
Class c = new Class();
// The target is an empty function.
c.AnEvent += new MyDelegate(c.EmptyInstanceFunction);
c.MeasureFire10(timer1000);
}
static public void MeasureGenericsOnValueType()
{
GenericClass<int> aGenericClassWithInt = new GenericClass<int>();
// GenericValueClass<int> aGenericValueClassWithInt = new GenericValueClass<int>();
object intObject = 3;
int aInt = 3;
timer1000.Measure("aGenericClassWithInt.aGenericInstanceFieldT = aInt", 10, delegate
{
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
aGenericClassWithInt.aGenericInstanceFieldT = aInt;
});
timer1000.Measure("GenericClass<int>.aGenericStaticFieldT = aInt", 10, delegate
{
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
GenericClass<int>.aGenericStaticFieldT = aInt;
});
timer1000.Measure("aGenericClassWithInt.ClassGenericInstanceMethod()", 10, delegate
{
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
aGenericClassWithInt.ClassGenericInstanceMethod();
});
timer1000.Measure("GenericClass<int>.ClassGenericStaticMethod()", 10, delegate
{
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
GenericClass<int>.ClassGenericStaticMethod();
});
timer1000.Measure("Class.GenericMethod<int>()", 10, delegate
{
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
Class.GenericMethod<int>();
});
}
static public void MeasureGenericsOnReferenceType()
{
GenericClass<string> aGenericClassWithString = new GenericClass<string>();
// GenericValueClass<string> aGenericValueClassWithString = new GenericValueClass<string>();
object stringObject = "foo";
string aString = "foo";
timer1000.Measure("aGenericClassWithString.aGenericInstanceFieldT = aString", 10, delegate
{
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
aGenericClassWithString.aGenericInstanceFieldT = aString;
});
timer1000.Measure("GenericClass<string>.aGenericStaticFieldT = aString", 10, delegate
{
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
GenericClass<string>.aGenericStaticFieldT = aString;
});
timer1000.Measure("aGenericClassWithString.ClassGenericInstanceMethod()", 10, delegate
{
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
aGenericClassWithString.ClassGenericInstanceMethod();
});
timer1000.Measure("GenericClass<string>.ClassGenericStaticMethod()", 10, delegate
{
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
GenericClass<string>.ClassGenericStaticMethod();
});
timer1000.Measure("Class.GenericMethod<string>()", 10, delegate
{
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
Class.GenericMethod<string>();
});
}
static public void MeasureIteration()
{
List<string> sList = new List<string>();
List<int> iList = new List<int>();
int[] aList = new int[100];
int iResult = 0;
string sResult = null;
for (int i = 0; i < 100; i++)
{
aList[i] = i;
sList.Add("test");
iList.Add(i);
}
timer1000.Measure("sum numbers 1-20", 1, delegate
{
int result = 0;
for (int i = 0; i < 20; i++)
result += i;
iResult = result;
});
timer1000.Measure("sum numbers 1-100", 1, delegate
{
int result = 0;
for (int i = 0; i < 100; i++)
result += i;
iResult = result;
});
timer1000.Measure("foreach over List<String> (100 elems)", 1, delegate
{
foreach (string s in sList)
sResult = s;
});
timer1000.Measure("for over List<String> (100 elems)", 1, delegate
{
for (int i = 0; i < sList.Count; i++)
sResult = sList[i];
});
timer1000.Measure("foreach over List<int> (100 elems)", 1, delegate
{
foreach (int i in iList)
iResult = i;
});
timer1000.Measure("for over List<int> (100 elems)", 1, delegate
{
for (int i = 0; i < iList.Count; i++)
iResult = iList[i];
});
timer1000.Measure("for with check int[] (100 elems)", 1, delegate
{
for (int i = 0; i < 100; i++)
iResult = aList[i];
});
timer1000.Measure("for with bound Optimization int[] (100 elems)", 1, delegate
{
int[] arr = aList;
for (int i = 0; i < arr.Length; i++)
iResult = arr[i];
});
ValueType[] vArr = new ValueType[100];
timer1000.Measure("foreach over ValueType[] (100 elems)", 1, delegate
{
foreach (ValueType v in vArr)
iResult = v.x;
});
timer1000.Measure("Foreach method over ValueType[] (100 elems)", 1, delegate
{
ValueType.Foreach(vArr, delegate(ref ValueType v)
{
iResult = v.x;
});
});
}
static public void MeasureTypeReflection()
{
RuntimeTypeHandle typeHandle = default(RuntimeTypeHandle);
Type type = null;
object anObject = "aString";
object anArray = new string[0];
bool result = false;
timer1000.Measure("anObject.GetType()", 10, delegate
{