为什么DeleteCriticalSection比InitializeCriticalSection慢得多?

时间:2011-03-09 03:05:44

标签: performance winapi critical-section

当应用程序关闭或服务停止时,我需要删除超过20000个CriticalSections。但我发现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

有什么好的建议吗?

0 个答案:

没有答案