Что такое мастер dry в этом классе System.Drawing.Point, который делает его намного быстрее, чем моя простая структура?
Это немного быстрее. Я получаю 1-5 мс для класса Point и 2000 мс или больше для своей структуры.
Глядя на источник Points.cs , я не достаточно квалифицирован, чтобы определить, что он делает. , Я попытался реализовать IEquatable (возможно, неправильно) и не смог получить никакой выгоды.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
int elementsInSets = 10000;
int lookupCount = 10000;
// Point struct from System.Drawing
HashSet<Point> myPoints = new HashSet<Point>();
for (int i = 0; i < elementsInSets; i++)
{
myPoints.Add(new Point(i, i));
}
// My simple struct
HashSet<P> myPoints2 = new HashSet<P>();
for (int i = 0; i < elementsInSets; i++)
{
myPoints2.Add(new P(i, i));
}
sw.Start();
for (int j = 0; j < lookupCount; j++)
{
if (myPoints2.Contains(new P(j, j)))
{
//found
}
}
Console.WriteLine("simple P " + sw.ElapsedMilliseconds + "ms");
sw.Restart();
for (int j = 0; j < lookupCount; j++)
{
if (myPoints.Contains(new Point(j, j)))
{
// found
}
}
Console.WriteLine("Point " + sw.ElapsedMilliseconds + "ms");
}
}
public struct P
{
int x;
int y;
public P(int xCoord, int yCoord)
{
x = xCoord;
y = yCoord;
}
}