-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerf.csx
More file actions
105 lines (87 loc) · 1.96 KB
/
Perf.csx
File metadata and controls
105 lines (87 loc) · 1.96 KB
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
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using static System.Console;
interface IExplicit
{
void Sample();
void Extra();
}
class Explicit : IExplicit
{
void IExplicit.Sample()
{
var list1 = new List<int>();
var list2 = new List<int>();
for (int i = 0; i < 1000; i++)
{
list1.Add(i);
}
Task.Run(() => Parallel.ForEach(list1, item => { list2.Add(item); })).Wait();
}
void IExplicit.Extra()
{
for (int i = 0; i < 1000; i++) { }
}
}
interface IImplicit
{
void Sample();
void Extra();
}
class Implicit : IImplicit
{
public void Sample()
{
var list1 = new List<int>();
var list2 = new List<int>();
for (int i = 0; i < 1000; i++)
{
list1.Add(i);
}
Task.Run(() => Parallel.ForEach(list1, item => { list2.Add(item); })).Wait();
}
public void Extra()
{
for (int i = 0; i < 1000; i++) { }
}
}
var stopWatch = new Stopwatch();
var times = 100000;
stopWatch.Start();
for (int i = 0; i < times; i++)
{
var obj = new Explicit() as IExplicit;
obj.Sample();
obj.Extra();
}
stopWatch.Stop();
WriteLine($"Explicit: {stopWatch.Elapsed.TotalMilliseconds:n}");
stopWatch.Reset();
stopWatch.Start();
for (int i = 0; i < times; i++)
{
((IExplicit)new Explicit()).Sample();
((IExplicit)new Explicit()).Extra();
}
stopWatch.Stop();
WriteLine($"Explicit: {stopWatch.Elapsed.TotalMilliseconds:n}");
stopWatch.Reset();
stopWatch.Start();
for (int i = 0; i < times; i++)
{
var obj = new Implicit() as IImplicit;
obj.Sample();
obj.Extra();
}
stopWatch.Stop();
WriteLine($"Implicit: {stopWatch.Elapsed.TotalMilliseconds:n}");
stopWatch.Reset();
stopWatch.Start();
for (int i = 0; i < times; i++)
{
new Implicit().Sample();
new Implicit().Extra();
}
stopWatch.Stop();
WriteLine($"Implicit: {stopWatch.Elapsed.TotalMilliseconds:n}");