-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinq-vs-for.linq
106 lines (92 loc) · 4.86 KB
/
linq-vs-for.linq
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
<Query Kind="Program" />
void Main()
{
const int steps = 1000000;
Stopwatch sw = new Stopwatch();
List<O1> list = new List<O1>()
{
new O1() { ParentId = null, OrganizationalUnitId = "100", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.1", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.2", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.3", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.4", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.5", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.6", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.7", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.8", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.9", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.10", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100", OrganizationalUnitId = "100.11", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100.1", OrganizationalUnitId = "100.1.1", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100.1", OrganizationalUnitId = "100.1.2", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100.1", OrganizationalUnitId = "100.1.3", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100.1", OrganizationalUnitId = "100.1.4", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100.1", OrganizationalUnitId = "100.1.5", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") },
new O1() { ParentId = "100.2", OrganizationalUnitId = "100.2.1", EffectiveStartDate = DateTime.Parse("01/01/2000"), EffectiveEndDate = DateTime.Parse("01/01/2020") }
};
var efDt = DateTime.Parse("05/01/2010");
sw.Start();
for (int i = 0; i < steps; i++)
{
F1(list, "100.1.1", efDt);
}
sw.Stop();
Console.WriteLine("F1:\tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);
sw.Reset();
sw.Start();
for (int i = 0; i < steps; i++)
{
F2(list, "100.1.1", efDt);
}
sw.Stop();
Console.WriteLine("F2: \tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);
sw.Reset();
sw.Start();
for (int i = 0; i < steps; i++)
{
F1(list, "100.1.1", efDt);
}
sw.Stop();
Console.WriteLine("F1:\tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);
sw.Reset();
sw.Start();
for (int i = 0; i < steps; i++)
{
F2(list, "100.1.1", efDt);
}
sw.Stop();
Console.WriteLine("F2: \tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);
}
// not checking if any entry in list is null
string F1(List<O1> list, string orgId, DateTime effectiveDate)
{
var parent = (from r in list
where r.OrganizationalUnitId == orgId
&& r.EffectiveStartDate <= effectiveDate
&& ((r.EffectiveEndDate >= effectiveDate)
|| (r.EffectiveEndDate == DateTime.MinValue))
select r.ParentId).FirstOrDefault();
return parent;
}
string F2(List<O1> list, string orgId, DateTime effectiveDate)
{
//for(int i=0;i<list.Count;i++)
foreach(var c in list)
{
//var c = list[i];
if (c.OrganizationalUnitId == orgId && c.EffectiveStartDate <= effectiveDate
&& ((c.EffectiveEndDate >= effectiveDate)
|| (c.EffectiveEndDate == DateTime.MinValue)))
{
return c.ParentId;
}
}
return null;
}
class O1
{
public string ParentId { get; set; }
public string OrganizationalUnitId { get; set; }
public DateTime EffectiveStartDate { get; set; }
public DateTime EffectiveEndDate { get; set; }
}