Skip to content

Instantly share code, notes, and snippets.

@joncloud
Last active December 9, 2016 15:32
Show Gist options
  • Select an option

  • Save joncloud/1c790e97c1d14b93875ae02708eec902 to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/1c790e97c1d14b93875ae02708eec902 to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.10.0
OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Xeon(R) CPU E5-2673 v3 2.40GHzIntel(R) Xeon(R) CPU E5-2673 v3 2.40GHz, ProcessorCount=16
Frequency=10000000 Hz, Resolution=100.0000 ns, Timer=UNKNOWN
Host Runtime=Clr 4.0.30319.42000, Arch=32-bit RELEASE
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1073.0
Job Runtime(s):
        Clr 4.0.30319.42000, Arch=32-bit RELEASE
Method | Agencies | Applications |            Mean |      StdErr |        StdDev |          Median | Gen 0 | Bytes Allocated/Op |

---------- |--------- |------------- |---------------- |------------ |-------------- |---------------- |------ |------------------- | HashSet | 1 | 1 | 115.6737 ns | 0.1991 ns | 0.7179 ns | 115.6960 ns | 0.85 | 317.71 | SortedSet | 1 | 1 | 10.6965 ns | 0.0350 ns | 0.1311 ns | 10.6872 ns | 0.15 | 55.67 | HashSet | 1 | 5 | 244.1176 ns | 0.7455 ns | 2.7894 ns | 243.4527 ns | 1.04 | 388.70 | SortedSet | 1 | 5 | 11.1377 ns | 0.1284 ns | 0.4974 ns | 11.3848 ns | 0.17 | 61.95 | HashSet | 1 | 10 | 387.0614 ns | 1.1599 ns | 4.3399 ns | 385.1542 ns | 1.55 | 581.31 | SortedSet | 1 | 10 | 10.6185 ns | 0.0215 ns | 0.0776 ns | 10.6058 ns | 0.17 | 61.55 | HashSet | 10 | 1 | 396.4408 ns | 1.9550 ns | 7.5718 ns | 395.9990 ns | 1.62 | 605.23 | SortedSet | 10 | 1 | 10.8443 ns | 0.0426 ns | 0.1650 ns | 10.8627 ns | 0.14 | 52.78 | HashSet | 10 | 5 | 1,566.2951 ns | 2.4972 ns | 9.3437 ns | 1,566.8618 ns | 5.16 | 2,041.86 | SortedSet | 10 | 5 | 10.7373 ns | 0.0742 ns | 0.2872 ns | 10.6043 ns | 0.17 | 61.73 | HashSet | 10 | 10 | 3,252.1154 ns | 3.1071 ns | 11.6259 ns | 3,247.6321 ns | 11.36 | 4,685.34 | SortedSet | 10 | 10 | 10.6431 ns | 0.0384 ns | 0.1436 ns | 10.6079 ns | 0.16 | 57.79 | HashSet | 100 | 1 | 3,010.4944 ns | 5.4544 ns | 19.6662 ns | 3,008.8972 ns | 11.99 | 4,849.25 | SortedSet | 100 | 1 | 11.5568 ns | 0.0561 ns | 0.2099 ns | 11.5037 ns | 0.16 | 57.36 | HashSet | 100 | 5 | 16,682.6447 ns | 28.1175 ns | 105.2062 ns | 16,672.5887 ns | 45.21 | 24,767.05 | SortedSet | 100 | 5 | 10.6891 ns | 0.0490 ns | 0.1896 ns | 10.6736 ns | 0.14 | 52.75 | HashSet | 100 | 10 | 31,452.0675 ns | 117.1837 ns | 438.4611 ns | 31,287.2970 ns | 98.94 | 48,567.38 | SortedSet | 100 | 10 | 10.7478 ns | 0.0461 ns | 0.1726 ns | 10.7126 ns | 0.17 | 61.77 | HashSet | 1000 | 1 | 29,631.3334 ns | 56.2352 ns | 194.8046 ns | 29,620.4529 ns | 98.94 | 48,577.63 | SortedSet | 1000 | 1 | 10.8079 ns | 0.0571 ns | 0.2136 ns | 10.8625 ns | 0.17 | 61.92 | HashSet | 1000 | 5 | 211,168.8877 ns | 805.6560 ns | 2,904.8342 ns | 210,246.3216 ns | - | 230,273.26 | SortedSet | 1000 | 5 | 10.7465 ns | 0.0542 ns | 0.2100 ns | 10.6933 ns | 0.13 | 49.26 | HashSet | 1000 | 10 | 350,690.3678 ns | 493.8956 ns | 1,710.9044 ns | 351,037.5033 ns | - | 374,559.94 | SortedSet | 1000 | 10 | 10.7901 ns | 0.0625 ns | 0.2419 ns | 10.8333 ns | 0.16 | 57.37 |

struct InvoiceKey : IComparable<InvoiceKey>
{
public InvoiceKey(int agencyId, int applicationId)
{
AgencyId = agencyId;
ApplicationId = applicationId;
}
public int AgencyId { get; }
public int ApplicationId { get; }
public int CompareTo(InvoiceKey other) => other.GetHashCode() - GetHashCode();
public override bool Equals(object obj)
{
if (obj is InvoiceKey)
{
var other = (InvoiceKey)obj;
return other.AgencyId == AgencyId && other.ApplicationId == ApplicationId;
}
return false;
}
public override int GetHashCode()
{
int hash = 383;
unchecked
{
hash = (hash * 151) + AgencyId.GetHashCode();
hash = (hash * 151) + ApplicationId.GetHashCode();
}
return hash;
}
}
public class Sets
{
[Params(1, 10, 100, 1000)]
public int Agencies;
[Params(1, 5, 10)]
public int Applications;
List<InvoiceKey> keys;
[Setup]
public void Setup()
{
keys = new List<InvoiceKey>();
for (int i = 0; i < Agencies; i++)
{
for (int j = 0; j < Applications; j++)
{
keys.Add(new InvoiceKey(i, j));
}
}
}
T Populate<T>(Func<List<InvoiceKey>, T> factory)
where T : ISet<InvoiceKey>
{
return factory(keys);
}
[Benchmark]
public HashSet<InvoiceKey> HashSet() => Populate(list => new HashSet<InvoiceKey>(list));
[Benchmark]
public SortedSet<InvoiceKey> SortedSet() => Populate(list => new SortedSet<InvoiceKey>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment