110,537
社区成员
发帖
与我相关
我的任务
分享
Dim a As New List(Of String)
For i = 1 To 10000000
a.Add(RndName)
Next
a.Add("ilovecsdn")
Dim t As Integer = GetTickCount()
Dim pd As Boolean = a.Contains("ilovecsdn")
msgbox(pd.ToString & vbCrLf & (GetTickCount() - t))
Dim a As New List(Of String)
For i = 1 To 10000000
a.Add(RndName)
Next
a.Add("ilovecsdn".GetHashCode )
Dim t As Integer = GetTickCount()
Dim pd As Boolean = a.Contains("ilovecsdn".GetHashCode )
msgbox(pd.ToString & vbCrLf & (GetTickCount() - t))
private const int DATA_COUNT = 10000000;
public void Execute()
{
var stringSet = new HashSet<string>(DATA_COUNT);
var hashCodeSet = new HashSet<int>(DATA_COUNT);
for (int i = 0; i < DATA_COUNT; i++)
{
var s = Guid.NewGuid().ToString();
stringSet.Add(s);
hashCodeSet.Add(s.GetHashCode());
}
var target = Guid.NewGuid().ToString();
stringSet.Add(target);
hashCodeSet.Add(target.GetHashCode());
int stringSize = Encoding.ASCII.GetByteCount(target) * (DATA_COUNT + 1);
int hashCodeSize = sizeof(int) * (DATA_COUNT + 1);
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < DATA_COUNT; i++)
{
stringSet.Contains(target);
}
stopwatch.Stop();
Debug.WriteLine($@"String > Size : {stringSize:N0}, Cost : {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
for (int i = 0; i < DATA_COUNT; i++)
{
hashCodeSet.Contains(target.GetHashCode());
}
stopwatch.Stop();
Debug.WriteLine($@"HashCode > Size : {hashCodeSize:N0}, Cost : {stopwatch.ElapsedMilliseconds} ms");
}
Dim a As New List(Of String)
For i = 1 To 10000000
a.Add(RndName)
Next
a.Add("ilovecsdn")
Dim t As Integer = GetTickCount()
Dim pd As Boolean = a.Contains("ilovecsdn")
msgbox(pd.ToString & vbCrLf & (GetTickCount() - t))
Dim a As New List(Of String)
For i = 1 To 10000000
a.Add(RndName)
Next
a.Add("ilovecsdn".GetHashCode )
Dim t As Integer = GetTickCount()
Dim pd As Boolean = a.Contains("ilovecsdn".GetHashCode )
msgbox(pd.ToString & vbCrLf & (GetTickCount() - t))
为什么我测试的用gethashcode反而慢了一倍的时间??? 前者是47毫秒后者是74毫秒