[coop] Refactor/reuse mono_value_box_handle/mono_value_box_checked and reduce raw...
[mono-project.git] / mono / benchmark / ctor-bench.cs
blob31099acdd55b9e03457eb0928b4885ab7a93590e
1 using System;
2 using System.Reflection;
4 class T {
6 public T () {
9 const int count = 1000000;
11 static void use_new () {
12 for (int i = 0; i < count; ++i)
13 new T ();
16 object Clone () {
17 return MemberwiseClone ();
20 static void use_clone () {
21 T t = new T ();
22 for (int i = 0; i < count; ++i)
23 t.Clone ();
26 static void use_activator () {
27 for (int i = 0; i < count; ++i)
28 Activator.CreateInstance (typeof (T));
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);
37 static void Main () {
38 long start, end, new_val, perc;
39 start = Environment.TickCount;
41 start = Environment.TickCount;
42 use_new ();
43 end = Environment.TickCount;
44 Console.WriteLine ("new took {0}", end-start);
45 new_val = end-start;
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);
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);
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);