My favorites | Sign in
Project Logo
                
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Some experimenting with linq perforamnce inspired by http://tiredblogger.wordpress.com/2008/07/25/iqueryable-methods-on-activereports-controlcollections/ .
* Author: Scott Willeke, http://blog.scott.willeke.com, scott at willeke dot com
*
*/
using System;
using System.Collections.Generic;
using System.Linq;


namespace LinqWithCollections
{
class Program
{
List<Label> _controlsSimpleList;
IQueryable<Label> _controlsQueryable;
List<Indicator> _indicators;
private bool IsSpanish;
const int TestIterationCount = 10;
const int indicatorCount = 400;

static void Main()
{
Program p = new Program();
p.CreateSampleData();
TimeTest(p.NoLinqTest);
TimeTest(p.WithLinqExtensionMethods);
TimeTest(p.WithLinqQueryable);
Console.ReadLine();
}

private void WithLinqQueryable()
{
var foundCount = 0;
foreach (var indicator in _indicators)
{
var indicatorHeader = string.Format("i{0}", indicator.Id);
var indicatorControl = _controlsQueryable.SingleOrDefault(x => x.Name == indicatorHeader);
if (indicatorControl != null)
{
foundCount++;
indicatorControl.Text = IsSpanish
? indicator.SpanishText
: indicator.EnglishText;
}
}
if (foundCount != indicatorCount/2)
throw new InvalidOperationException("invalid foundCount");
}

private void WithLinqExtensionMethods()
{
var foundCount = 0;
foreach (var indicator in _indicators)
{
var indicatorHeader = string.Format("i{0}", indicator.Id);
var indicatorControl = _controlsSimpleList.SingleOrDefault(x => x.Name == indicatorHeader);
if (indicatorControl != null)
{
foundCount++;
indicatorControl.Text = IsSpanish
? indicator.SpanishText
: indicator.EnglishText;
}
}
if (foundCount != indicatorCount / 2)
throw new InvalidOperationException("invalid foundCount");
}

private void NoLinqTest()
{
var controlLookup = new Dictionary<string, Label>();
_controlsSimpleList.ForEach(x => controlLookup[x.Name] = x);

var foundCount = 0;
foreach (var indicator in _indicators)
{
var indicatorHeader = string.Format("i{0}", indicator.Id);
Label indicatorControl;
if (controlLookup.TryGetValue(indicatorHeader, out indicatorControl))
{
foundCount++;
indicatorControl.Text = IsSpanish
? indicator.SpanishText
: indicator.EnglishText;
}
}
if (foundCount != indicatorCount/2)
throw new InvalidOperationException("invalid foundCount");
}

#region Helper Methods
private static void TimeTest(Action testMethod)
{
var testName = testMethod.Method.Name;
using (new TimerX(testName))
{
for (var i = 0; i < TestIterationCount; i++)
{
testMethod();
}
}
}

private void CreateSampleData()
{
_indicators = new List<Indicator>();
for (var i = 0; i < indicatorCount; i++)
{
_indicators.Add(new Indicator(i));
}
_controlsSimpleList = new List<Label>();
for (var i = 0; i < indicatorCount; i = i + 2)
{
_controlsSimpleList.Add(new Label(i));
}
_controlsQueryable = _controlsSimpleList.AsQueryable();
}
#endregion
}

#region Helper Classes
internal class TimerX : IDisposable
{
private DateTime _startTime;
private DateTime _stopTime;
private readonly string _label;

public TimerX(string label)
{
_label = label;
Start();
}

private void Start()
{
_startTime = DateTime.Now;
}

public void Stop()
{
_stopTime = DateTime.Now;
}

void IDisposable.Dispose()
{
Stop();
Trace();
}

private void Trace()
{
var msg = _label + ": " + (_stopTime - _startTime).TotalMilliseconds + " ms";
Console.WriteLine(msg);
}
}

internal class Label
{
public Label(int i)
{
this.Name = "i" + i;
this.Text = "text" + i;
}

public string Name;
public string Text;
}

internal class Indicator
{
public int Id;
public string SpanishText;
public string EnglishText;

public Indicator(int i)
{
this.Id = i;
this.SpanishText = "spanish" + i;
this.EnglishText = "english" + i;
}
}
#endregion
}
Show details Hide details

Change log

r2 by activescott on Jul 27, 2008   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 4307 bytes, 184 lines
Hosted by Google Code