C# 为什么 hashset 在我的测试中很慢。请帮助我理解
C# why is hashset slow in my test. Please help me understand
哈希集测试已过 time:5.38
Linq 测试已过 ime:0.64
Foreach 循环测试已结束 [=24=].32
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace _08_ArrayCheck
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var a = Enumerable.Range('a','z'-'a'+1).Select(c=>(char)c).ToArray();
double sTime = 0;
double fTime = 0;
sTime = sw.Elapsed.TotalMilliseconds;
Hashset(a,'a');
fTime = sw.Elapsed.TotalMilliseconds;
Console.WriteLine("Hashset test Elapsed time:"+string.Format("{0:0.##}",(fTime-sTime)));
sTime = sw.Elapsed.TotalMilliseconds;
Linq(a,'a');
fTime = sw.Elapsed.TotalMilliseconds;
Console.WriteLine("Linq test Elapsed ime:"+string.Format("{0:0.##}",(fTime-sTime)));
sTime = sw.Elapsed.TotalMilliseconds;
Foreachloop(a,'a');
fTime = sw.Elapsed.TotalMilliseconds;
Console.WriteLine("Foreach loop test Elapsed ime:"+ string.Format("{0:0.##}",(fTime-sTime)));
sw.Stop();
}
public static bool Hashset(char[] a, char x)
// isn't hashset supposed to be'faster code performance wise
{
HashSet<char> hashSearch = new HashSet<char>(a);
return hashSearch.Contains(x);
}
public static bool Linq(char[] a, char x)//linq solution
{
return a.Contains(x);
}
public static bool Foreachloop(char[] a, char x)//foreach loop
{
foreach(char s in a)
{
if(s==x) return true;
}
return false;
}
}
}
请帮我理解我无法理解的。我的编码老师告诉我们哈希集的性能应该更快。
但在我上面的测试代码中证明它没有
或者我的做法有问题吗?
您正在测量创建 HashSet 和搜索它的时间。尝试仅测量查找时间。
您还在搜索范围中的第一个元素 'a',线性搜索将在仅测试第一个元素后成功,并将 return 成功。
哈希集测试已过 time:5.38
Linq 测试已过 ime:0.64
Foreach 循环测试已结束 [=24=].32
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace _08_ArrayCheck
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var a = Enumerable.Range('a','z'-'a'+1).Select(c=>(char)c).ToArray();
double sTime = 0;
double fTime = 0;
sTime = sw.Elapsed.TotalMilliseconds;
Hashset(a,'a');
fTime = sw.Elapsed.TotalMilliseconds;
Console.WriteLine("Hashset test Elapsed time:"+string.Format("{0:0.##}",(fTime-sTime)));
sTime = sw.Elapsed.TotalMilliseconds;
Linq(a,'a');
fTime = sw.Elapsed.TotalMilliseconds;
Console.WriteLine("Linq test Elapsed ime:"+string.Format("{0:0.##}",(fTime-sTime)));
sTime = sw.Elapsed.TotalMilliseconds;
Foreachloop(a,'a');
fTime = sw.Elapsed.TotalMilliseconds;
Console.WriteLine("Foreach loop test Elapsed ime:"+ string.Format("{0:0.##}",(fTime-sTime)));
sw.Stop();
}
public static bool Hashset(char[] a, char x)
// isn't hashset supposed to be'faster code performance wise
{
HashSet<char> hashSearch = new HashSet<char>(a);
return hashSearch.Contains(x);
}
public static bool Linq(char[] a, char x)//linq solution
{
return a.Contains(x);
}
public static bool Foreachloop(char[] a, char x)//foreach loop
{
foreach(char s in a)
{
if(s==x) return true;
}
return false;
}
}
}
请帮我理解我无法理解的。我的编码老师告诉我们哈希集的性能应该更快。
但在我上面的测试代码中证明它没有
或者我的做法有问题吗?
您正在测量创建 HashSet 和搜索它的时间。尝试仅测量查找时间。
您还在搜索范围中的第一个元素 'a',线性搜索将在仅测试第一个元素后成功,并将 return 成功。