Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / benchmark / ctor-bench.cs
1 using System;
2 using System.Reflection;
3
4 class T {
5
6         public T () {
7         }
8
9         const int count = 1000000;
10
11         static void use_new () {
12                 for (int i = 0; i < count; ++i) 
13                         new T ();
14         }
15
16         object Clone () {
17                 return MemberwiseClone ();
18         }
19         
20         static void use_clone () {
21                 T t = new T ();
22                 for (int i = 0; i < count; ++i) 
23                         t.Clone ();
24         }
25         
26         static void use_activator () {
27                 for (int i = 0; i < count; ++i) 
28                         Activator.CreateInstance (typeof (T));
29         }
30         
31         static void use_ctor () {
32                 ConstructorInfo ctor = typeof (T).GetConstructor (Type.EmptyTypes);
33                 for (int i = 0; i < count; ++i) 
34                         ctor.Invoke (null);
35         }
36         
37         static void Main () {
38                 long start, end, new_val, perc;
39                 start = Environment.TickCount;
40
41                 start = Environment.TickCount;
42                 use_new ();
43                 end = Environment.TickCount;
44                 Console.WriteLine ("new took {0}", end-start);
45                 new_val = end-start;
46
47                 start = Environment.TickCount;
48                 use_clone ();
49                 end = Environment.TickCount;
50                 perc = ((end-start-new_val) * 100) / new_val;
51                 Console.WriteLine ("clone took {0} {1} %", end-start, perc);
52
53                 start = Environment.TickCount;
54                 use_activator ();
55                 end = Environment.TickCount;
56                 perc = ((end-start-new_val) * 100) / new_val;
57                 Console.WriteLine ("activator took {0} {1} %", end-start, perc);
58
59                 start = Environment.TickCount;
60                 use_ctor ();
61                 end = Environment.TickCount;
62                 perc = ((end-start-new_val) * 100) / new_val;
63                 Console.WriteLine ("ctor took {0} {1} %", end-start, perc);
64
65         }
66 }
67