Recent

Author Topic: Faster Method Needed --(SOLVED)  (Read 4083 times)

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Faster Method Needed --(SOLVED)
« on: April 26, 2014, 07:54:18 pm »
The code below is doing exactly what I need it too, however it takes over 5 min to complete the job.

For example I have over 2000 lines in a text file. Each line
looks similar to the following:

$PG92NVU3  PG92NVU3
$P8M00Q76  8M00Q76
$PVCSYQVT  VCSYQVT
$PNSFGBJZ   NSFGBJZ
$PDNTHV7G  DNTHV7G
$PM7S2SL2   M7S2SL2
$P9W4JR8M   9W4JR8M
$PHL2T3GD   HL2T3GD
$PZZX4QTA   ZZX4QTA

The changes to the new file would look like the following:
*$PG92NVU3*   PG92NVU3
*$P8M00Q76*   8M00Q76
*$PVCSYQVT*   VCSYQVT
*$PNSFGBJZ*   NSFGBJZ
*$PDNTHV7G*   DNTHV7G
*$PM7S2SL2*   M7S2SL2
*$P9W4JR8M*   9W4JR8M
*$PHL2T3GD*   HL2T3GD
*$PZZX4QTA*   ZZX4QTA

As I said the below code works, the issue is its taking forever
to make the needed changes.

Any ideas ?

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

  x: Integer;
  Target: String;
implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  memo1.Lines.LoadFromFile('C:\Users\CCW\Desktop\BCD.ppc');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  application.ProcessMessages;
  For x := 0 to memo1.Lines.Count -1 do
  begin
  Target := Copy(memo1.Lines[x],1,Length(Memo1.Lines[x]));

  Insert('*',Target,1);
  Insert('*',Target,11);
  Memo1.lines.Strings[x] := Target;


  //label1.Caption := Target;
  end;
  memo1.Lines.SaveToFile('C:\Users\CCW\Desktop\BCD-Ready.ppc');

end;

end.

« Last Edit: April 26, 2014, 08:52:26 pm by wjackson153 »
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Faster Method Needed
« Reply #1 on: April 26, 2014, 07:58:09 pm »
Yes, load the memo contents to a TStringList and do the operations there. After that reload the memo with the new contents.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Faster Method Needed
« Reply #2 on: April 26, 2014, 08:12:21 pm »
Code: [Select]
Memo1.BeginUpdate;
try
 ... // your current code, no need for application.processmessages though, it will only process the message queue once so it's kinda useless
finally
  Memo1.EndUpdate;
end;

Scoops

  • Full Member
  • ***
  • Posts: 105
Re: Faster Method Needed
« Reply #3 on: April 26, 2014, 08:42:06 pm »
if you want to use memo try

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  For x := 0 to memo1.Lines.Count -1 do
  begin
  Memo1.Lines [x] := AnsiReplaceStr (Memo1.lines[x],'$','*$');
  Memo1.Lines [x] := AnsiReplaceStr (Memo1.lines[x],'  ','*  ');
  end;

end;     

add 'strutils' to to your uses clause first

but as said before you will speed things up using tstringlist

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Faster Method Needed
« Reply #4 on: April 26, 2014, 08:51:53 pm »
Thanks for all the recommendations,  Yes Tstrings was blazing
fast.

Though leledumbo has often had good advise for me in the past, this time was not that case :)   Your suguestion slowed it down even more then my method :P

But nonetheless thanks for the feedback.


Solved....
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Faster Method Needed --(SOLVED)
« Reply #5 on: April 27, 2014, 12:29:30 am »
According to my measurement, Begin/EndUpdate speeds up a 2000 lines memo with 'abcdefgh' string on every line about 5 times. And TStringList does it about 50 times.

Try to avoid  GUI operations at all for more than some hundred lines.
« Last Edit: April 27, 2014, 12:44:05 am by typo »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Faster Method Needed --(SOLVED)
« Reply #6 on: April 27, 2014, 01:09:17 am »
TStringList does it about 50 times.
According to the OP:
... it takes over 5 min to complete the job.
If using TStringList speeds the process up by 50 times, then it takes more than (5*60)/50=6 seconds. It should take a fraction of a second.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Faster Method Needed --(SOLVED)
« Reply #7 on: April 27, 2014, 01:11:48 am »
Yes, probably wjackson153 can confirm this.

Not a fraction of a second, but 6 seconds.
« Last Edit: April 27, 2014, 01:48:52 am by typo »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Faster Method Needed --(SOLVED)
« Reply #8 on: April 27, 2014, 03:48:05 am »
Not a fraction of a second, but 6 seconds.
A fraction of a second. Check the attached project.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Faster Method Needed --(SOLVED)
« Reply #9 on: April 27, 2014, 03:58:55 am »
Well, I agree.

So, more than 300 times faster.
« Last Edit: April 27, 2014, 04:16:36 am by typo »

 

TinyPortal © 2005-2018