开发者

Why is DeleteCriticalSection so much slower than InitializeCriticalSection?

开发者 https://www.devze.com 2023-02-16 03:25 出处:网络
I have more than 20000 CriticalSections need to delete when application close or serivce stop. But I found the DeleteCriticaSection function i开发者_如何学Cs very slow. I do a test:

I have more than 20000 CriticalSections need to delete when application close or serivce stop. But I found the DeleteCriticaSection function i开发者_如何学Cs very slow. I do a test:

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;

The Result:

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

Have any good suggestions?

0

精彩评论

暂无评论...
验证码 换一张
取 消