Created
May 29, 2012 10:12
-
-
Save jonelf/2823878 to your computer and use it in GitHub Desktop.
Performance test of searching in List of Dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
namespace Test | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int found = 0; | |
var test = new HousesTest(); | |
var watch = new Stopwatch(); | |
watch.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
found += test.Search(); | |
} | |
watch.Stop(); | |
Console.WriteLine(watch.ElapsedMilliseconds); | |
Console.WriteLine("Found: "+ found); | |
} | |
} | |
class HousesTest | |
{ | |
private readonly List<Dictionary<string, object>> _houses = new List<Dictionary<string, object>>(); | |
public HousesTest() | |
{ | |
Random rnd = new Random(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
var house = new Dictionary<string, object>(); | |
house["Street"] = "Orchard Street"; | |
house["StreetNumber"] = rnd.Next(1,21); | |
house["City"] = "Gotham"; | |
if (rnd.Next(1, 4) > 1) | |
house["Fiber"] = true; | |
if (rnd.Next(1, 8) > 5) | |
house["Bank"] = "JP Morgan"; | |
_houses.Add(house); | |
} | |
} | |
public int Search() | |
{ | |
int found = 0; | |
foreach (var house in _houses) | |
{ | |
object tmp; | |
house.TryGetValue("Fiber", out tmp); | |
bool hasFiber = tmp == null ? false : (bool)tmp; | |
if ((string)house["Street"] == "Orchard Street" && | |
(int)house["StreetNumber"] > 3 && hasFiber) | |
found++; | |
} | |
return found; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment