Recent

Author Topic: Slow dispose  (Read 3698 times)

bigeno

  • Sr. Member
  • ****
  • Posts: 264
Slow dispose
« on: June 29, 2014, 01:33:01 pm »
I've big problem with speed of my project, I made sample test with problem.
Code: [Select]
type
  PStruct= ^TStruct;
  TStruct=record
    Size1: Integer;
    Size2: Integer;
  end;

var
  Form1: TForm1;
  st : PStruct;
  pp : array of PStruct;
  i: integer;

implementation

{$R *.lfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 SetLength(pp,600000);
 for i:=0 to 600000 do begin
   New(st);
   st^.Size1:=-1;
   st^.Size2:=-1;
   pp[i]:=st;
 end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
 for i:=0 to 600000 do
   Dispose(PStruct(pp[i]));
end;

The problem is with slow Dispose when I use cmem, with cmem dispose procedure is 10,15x slower then without cmem.
I need cmem in my project, can some one help with this ? ( I use Windows x64 and fpc,lazarus from trunk).

Leledumbo

  • Hero Member
  • *****
  • Posts: 8523
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Slow dispose
« Reply #1 on: June 29, 2014, 01:47:57 pm »
Code: [Select]
{$mode objfpc}
uses
  cmem,sysutils,dateutils;
 
type
  PStruct= ^TStruct;
  TStruct=record
    Size1: Integer;
    Size2: Integer;
  end;

var
  st : PStruct;
  pp : array of PStruct;
  i: integer;
  bt,et: ttime;
begin
 SetLength(pp,600000);
 for i:=0 to 600000 do begin
   New(st);
   st^.Size1:=-1;
   st^.Size2:=-1;
   pp[i]:=st;
 end;
 bt := now;
 for i:=0 to 600000 do
   Dispose(PStruct(pp[i]));
 et := now;
 writeln(secondspan(bt,et):1:6);
end.
Doesn't happen with above program on x86_64-linux with or without optimization. Even cmem actually makes the program faster: 0.010 s vs 0.012 s (without cmem) in average. If after you test above program the result is still the same, that would probably M$ faulty implementation of C memory manager.
Follow this if you want me to answer: **://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

**://pascalgeek.blogspot.com
**s://bitbucket.org/leledumbohttps://github.com/leledumbo
Code first, think later - Natural programmer B)

bigeno

  • Sr. Member
  • ****
  • Posts: 264
Re: Slow dispose
« Reply #2 on: June 29, 2014, 02:05:57 pm »
Thanks, and sorry, there is no problem (just noticed what I was doing) ;)
My production version of project is ok, the problem is with IDE Debugger (whe I use Run on lazarus), only in that case Dispose is slow but this is no problem for me.  :)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Slow dispose
« Reply #3 on: June 29, 2014, 03:12:22 pm »
You would gain a little speed perhaps by changing it to
Code: [Select]
pp : array of TStruct;The pointer is making you use more memory for it.

edit: Well.. whatever you need for project, that seems just a hacky way to replicate behavior of TObject.
« Last Edit: June 29, 2014, 03:14:37 pm by User137 »

 

TinyPortal © 2005-2018