/
ctor-bench.cs
67 lines (52 loc) · 1.4 KB
/
ctor-bench.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
using System;
using System.Reflection;
class T {
public T () {
}
const int count = 1000000;
static void use_new () {
for (int i = 0; i < count; ++i)
new T ();
}
object Clone () {
return MemberwiseClone ();
}
static void use_clone () {
T t = new T ();
for (int i = 0; i < count; ++i)
t.Clone ();
}
static void use_activator () {
for (int i = 0; i < count; ++i)
Activator.CreateInstance (typeof (T));
}
static void use_ctor () {
ConstructorInfo ctor = typeof (T).GetConstructor (Type.EmptyTypes);
for (int i = 0; i < count; ++i)
ctor.Invoke (null);
}
static void Main () {
long start, end, new_val, perc;
start = Environment.TickCount;
start = Environment.TickCount;
use_new ();
end = Environment.TickCount;
Console.WriteLine ("new took {0}", end-start);
new_val = end-start;
start = Environment.TickCount;
use_clone ();
end = Environment.TickCount;
perc = ((end-start-new_val) * 100) / new_val;
Console.WriteLine ("clone took {0} {1} %", end-start, perc);
start = Environment.TickCount;
use_activator ();
end = Environment.TickCount;
perc = ((end-start-new_val) * 100) / new_val;
Console.WriteLine ("activator took {0} {1} %", end-start, perc);
start = Environment.TickCount;
use_ctor ();
end = Environment.TickCount;
perc = ((end-start-new_val) * 100) / new_val;
Console.WriteLine ("ctor took {0} {1} %", end-start, perc);
}
}