Solved

Using ListBoxes dramatically slows my program

Posted on 1997-11-29
6
140 Views
Last Modified: 2010-04-04
Hi ..
  I have an app that reads a file looking for strings specified by the user. I used to have it do they could specify the up to 10 strings via edit boxes, then I'd take the contents of the edit boxes and move it to a array of string 10 big. I'd then loop around checking the input file record for any of these 10 strings. This worked OK, but I wanted to allow the user to input an unlimited number of strings, so I switched over to using a list box for input.
  This slowed the execution of my program by almost 300%! Here's the code I'm using:

for y:=0 to ListBox3.items.count - 1 do
    begin
      x:=0;
      x := Pos(ListBox3.items[y], inrec);
      if x <> 0 then
      begin
        writeln(outfile, inrec);
        goto doagain;
      end;
    end;

.. The code I had before looked almost identical except I used string arrays instead of the listbox:

for y:=1 to 10 do
    begin
      x:=0;
      x := Pos(input_str[y], inrec);
      if x <> 0 then
      begin
        writeln(outfile, inrec);
        goto doagain;
      end;
    end;

.. How can I get around this? I really have to use listBoxes
so I can accept a lot of strings easily, but it really seems to be slowing me down!!

Regards,
  Shawn
0
Comment
Question by:aztec
6 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1352423
I guess the only way is keeping with the array of strings, and use the ListBox just to show the strings to the user.
0
 

Author Comment

by:aztec
ID: 1352424
I don't want to use just arrays of strings because I had a lot of string arrays in my program before and it blew it out of the water. The user could potentially add a lot of strings and to make a string array hundreds big would crash my program like before. Can't I use pointers to strings? I've heard this may help me, but I don't know how to do it??

Shawn
0
 
LVL 1

Accepted Solution

by:
anilms earned 50 total points
ID: 1352425
You can do this - use the ListBox for UI purposes only and then use a Linked List for storing all the strings. You have two choices here - either to create your own list handling functions or to use a TList object. The TList object is faster than the ListBox but if you define your own list processing functions, it will be faster still. You will have to write code to add,delete and traverse a list. You can avoid all this if you use TList. TList is basically a collection of pointers.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Expert Comment

by:anilms
ID: 1352426
Sorry, the above was not an answer - it was more of a guideline. I did not purposefully put it as an answer. I can give an answer based on more input.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1352427
Agreed with you both; i said 'array of strings' just for the sake of simplicity.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1352428
how do you ascertain that using listbox is 300 times slower?
I tested you code and found that there are not much difference in speed of both method. This is true event with the use of memo box. Is there any thing wrong in your 'doagain' code?
May be I have misunderstood your requirement. Could you give us more detail.
The following are my test program which use
a. array of string
b. listbox
c. memo
(Note that I have remove the goto statement for [b] and [c].)

procedure TForm1.Button1Click(Sender: TObject);
var x, y: integer;
label doagain;
begin
  for x:=1 to 10 do
    input_Str[x]:= listbox1.items[x-1];
  doagain:
  inrec:= InputBox('Data entry', 'Data = ', '');
  for y:=1 to 10 do begin
    x:=0;
    x := Pos(input_str[y], inrec);
    if x <> 0 then begin
      writeln(outfile, inrec);
      goto doagain;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var x, y: integer;
begin
 while InputQuery('Data entry',
    'Click [OK] to confirm or [Cancel] to quit:', inrec) do begin
   for y:=0 to ListBox1.items.count - 1 do begin
     x:=0;
     x := Pos(ListBox1.items[y], inrec);
     if x <> 0 then
       writeln(outfile, inrec);
   end;
 end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var x, y: integer;
begin
  while InputQuery('Data entry',
    'Click [OK] to confirm or [Cancel] to quit:', inrec) do begin
    for y:=0 to memo1.lines.count - 1 do begin
      x:=0;
      x := Pos(memo1.lines[y], inrec);
      if x <> 0 then
        writeln(outfile, inrec);
    end;
  end;
end;


0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
proper way to parse url in delphi 2 194
Multiple image collision 13 74
Delphi and Access based Enumeration 9 59
How to Get Images From Server using App Tethering 11 21
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question