-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddIterateRemoveTest.cs
128 lines (105 loc) · 3.67 KB
/
AddIterateRemoveTest.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using NUnit.Framework;
namespace CollectionTest
{
[TestFixture]
public class AddIterateRemoveTest
{
private const int RUNS_TIME_MS = 10*1000;
private const int SIZE = 1000*1000;
private static readonly int[] INTS = new int[SIZE];
static AddIterateRemoveTest()
{
for (int i = 0; i < SIZE; i++)
INTS[i] = i;
}
[Test]
public static void PerfomanceTest()
{
Test(new List<int>());
Test(new LinkedList<int>());
Test(new HashSet<int>());
Test(new SortedSet<int>());
}
private static void Test(ICollection<int> ints)
{
Test(ints, ints.GetType().Name);
}
private static void Test(ICollection<int> ints, string name)
{
Stopwatch sw = new Stopwatch();
for (int size = SIZE; size >= 10; size /= 10)
{
long adding = 0, removing = 0, iterating = 0, searching = 0;
var runs = 0;
long endTime = Environment.TickCount + RUNS_TIME_MS;
do
{
runs++;
sw.Start();
TestAdd(ints, size);
sw.Stop();
adding += Convert.ToInt64(sw.Elapsed.TotalMilliseconds * 1000);
sw.Restart();
TestIterate(ints);
sw.Stop();
iterating += Convert.ToInt64(sw.Elapsed.TotalMilliseconds * 1000);
sw.Restart();
TestSearch(ints);
sw.Stop();
searching += Convert.ToInt64(sw.Elapsed.TotalMilliseconds * 1000);
sw.Restart();
TestRemove(ints, size);
sw.Stop();
removing += Convert.ToInt64(sw.Elapsed.TotalMilliseconds * 1000);
ints.Clear();
} while (endTime > Environment.TickCount);
String result = "<tr><td>" + name + "</td><td aligned=\"right\">" + String.Format("{0}", size)
+ "</td><td aligned=\"right\">" + Format(10*adding/runs)
+ "</td><td aligned=\"right\">" + Format(iterating/runs)
+ "</td><td aligned=\"right\">" + Format(10*searching/runs)
+ "</td><td aligned=\"right\">" + Format(10*removing/runs)
+ "</td></tr>";
Debug.WriteLine(result);
}
}
private static void TestRemove(ICollection<int> ints, int size)
{
for (int i = 0; i < size - 1; i++)
{
ints.Remove(INTS[i]);
}
}
private static bool TestSearch(ICollection<int> ints)
{
int searchEl = ints.Count - 1;
return ints.Contains(searchEl);
}
private static long TestIterate(ICollection<int> ints)
{
long sum = 0;
foreach (int i in ints)
{
sum += i;
}
return sum;
}
private static void TestAdd(ICollection<int> ints, int size)
{
for (int i = 0; i < size; i++)
{
ints.Add(INTS[i]);
}
}
private static string Format(long l)
{
return l < 1000
? "" + (l/10.0)
: l < 10000
? "" + l/10
: String.Format("{0}", l/10);
}
}
}