0

当应用程序关闭或服务停止时,我有超过 20000 个关键部分需要删除。但是我发现 DeleteCriticaSection 函数很慢。我做一个测试:

var
  CC: array[1..20000] of TRTLCriticalSection;

procedure TForm1.Button1Click(Sender: TObject);
var
  T: Cardinal;
  I: Integer;
begin
  Memo1.Lines.Add('InitializeCriticalSection:');
  T :=  GetTickCount;
  for I := 1 to 20000 do
    InitializeCriticalSection(CC[i]);
  Memo1.Lines.Add(IntToStr(GetTickCount - T));
  EnterCriticalSection(CC[3009]);
  Memo1.Lines.Add('--------------');
  LeaveCriticalSection(CC[3009]);
  Memo1.Lines.Add('DeleteCriticalSection:');
  T := GetTickCount;
  for I := 1 to 20000 do
    DeleteCriticalSection(CC[i]);
  Memo1.Lines.Add(IntToStr(GetTickCount - T));
end;

结果:

InitializeCriticalSection:
62
--------------
DeleteCriticalSection:
2262

有什么好的建议吗?

4

0 回答 0