我想在不允许重复的情况下存储一些像素位置,所以首先想到的是HashSet<Point>
或类似的类.然而,与HashSet<string>
相比,这似乎是非常缓慢的.
例如,此代码:
HashSet<Point> points = new HashSet<Point>();
using (Bitmap img = new Bitmap(1000, 1000))
{
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
points.Add(new Point(x, y));
}
}
}
大约需要22.5秒.
而以下代码(which is not a good choice for obvious reasons)只需1.6秒:
HashSet<string> points = new HashSet<string>();
using (Bitmap img = new Bitmap(1000, 1000))
{
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
points.Add(x + "," + y);
}
}
}
So, my questions are:
- 这有什么原因吗?我 Select 了this answer秒,但22.5秒比答案中显示的数字要多得多.
- 有没有更好的方法来存储不重复的点?