Skip to content

Instantly share code, notes, and snippets.

@adbre
Last active May 14, 2018 07:58
Show Gist options
  • Save adbre/2f9d117690f546de648c04ee31bf9048 to your computer and use it in GitHub Desktop.
Save adbre/2f9d117690f546de648c04ee31bf9048 to your computer and use it in GitHub Desktop.

100 000 records

Creating test data (100000 records)... DONE
    Elapsed....: 00:00:00.0340276
    # per µs...: 2,94
    µs per #...: 0,34

Creating subset (1000 records... DONE
    Elapsed....: 00:00:00.0003953
    # per µs...: 2,53
    µs per #...: 0,40

TEST 1: Filtering with subset as Array... DONE
    Elapsed....: 00:00:00.4449301
    # per µs...: 0,22
    µs per #...: 4,45

TEST 2: Filtering with subset as List... DONE
    Elapsed....: 00:00:00.7225637
    # per µs...: 0,14
    µs per #...: 7,23

TEST 3: Filtering with subset as Dictionary... DONE
    Elapsed....: 00:00:00.0027382
    # per µs...: 36,52
    µs per #...: 0,03

TEST 4: Filtering with subset as HashSet... DONE
    Elapsed....: 00:00:00.0029626
    # per µs...: 33,75
    µs per #...: 0,03

100 records

Creating test data (100 records)... DONE
    Elapsed....: 00:00:00.0002003
    # per µs...: 0,50
    µs per #...: 2,00

Creating subset (1 records... DONE
    Elapsed....: 00:00:00.0001631
    # per µs...: 0,01
    µs per #...: 163,10

TEST 1: Filtering with subset as Array... DONE
    Elapsed....: 00:00:00.0002622
    # per µs...: 0,38
    µs per #...: 2,62

TEST 2: Filtering with subset as List... DONE
    Elapsed....: 00:00:00.0002215
    # per µs...: 0,45
    µs per #...: 2,22

TEST 3: Filtering with subset as Dictionary... DONE
    Elapsed....: 00:00:00.0002141
    # per µs...: 0,47
    µs per #...: 2,14

TEST 4: Filtering with subset as HashSet... DONE
    Elapsed....: 00:00:00.0002229
    # per µs...: 0,45
    µs per #...: 2,23
void Main()
{
var ids = new Guid[100000];
Run($"Creating test data ({ids.Length} records)", ids.Length, () => {
for (var i=0; i < ids.Length; i++) {
ids[i] = Guid.NewGuid();
}
});
var rnd = new Random();
var subset = new Guid[(int)(ids.Length * 0.01)]; // 1% of total
var idsIndexes = new HashSet<int>();
Run($"Creating subset ({subset.Length} records", subset.Length, () => {
for (var i=0; i < subset.Length; i++) {
while (true) {
var index = rnd.Next(0, ids.Length);
if (!idsIndexes.Add(index)) continue;
subset[i] = ids[index];
break;
}
}
});
Run($"TEST 1: Filtering with subset as Array", ids.Length, () => ids.Where(id => subset.Contains(id)).ToList());
var subsetList = subset.ToList();
Run($"TEST 2: Filtering with subset as List", ids.Length, () => ids.Where(id => subsetList.Contains(id)).ToList());
var subsetDictionary = subset.ToDictionary(id => id, id => true);
Run($"TEST 3: Filtering with subset as Dictionary", ids.Length, () => ids.Where(id => subsetDictionary.ContainsKey(id)).ToList());
var subsetHashSet = new HashSet<Guid>(subset);
Run($"TEST 4: Filtering with subset as HashSet", ids.Length, () => ids.Where(id => subsetHashSet.Contains(id)).ToList());
}
void Run(string prompt, int records, Action action)
{
Console.Write($"{prompt}... ");
var sw = Stopwatch.StartNew();
action();
sw.Stop();
Console.WriteLine("DONE");
Console.WriteLine($" Elapsed....: {sw.Elapsed}");
Console.WriteLine($" # per µs...: {records / (sw.Elapsed.TotalMilliseconds * 1000):N}");
Console.WriteLine($" µs per #...: {(sw.Elapsed.TotalMilliseconds * 1000) / records:N}");
Console.WriteLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment