Board index » delphi » TStringList, Sort, and Progress Bar

TStringList, Sort, and Progress Bar

All;

    I perform a customsort on a TStringlist one time after I fill it.  This
customsort performs exactly what I need but takes roughly 10 seconds (the
TStringlist has roughtly 3000 rows) to accomplish the task.  I would like to
know how I can provide the user a TprogressBar to show that it's working and
doing something instead of only the hour cursor.  Does anyone know how to do
this?

Thanks in advance;
Dean

 

Re:TStringList, Sort, and Progress Bar


In article <xgTd8.66756$dG.27784...@news1.rdc1.sdca.home.com>, "Dean Jones"

Quote
<ptsoftw...@home.com> writes:
>    I perform a customsort on a TStringlist one time after I fill it.  This
>customsort performs exactly what I need but takes roughly 10 seconds (the
>TStringlist has roughtly 3000 rows) to accomplish the task.  I would like to
>know how I can provide the user a TprogressBar to show that it's working and
>doing something instead of only the hour cursor.  Does anyone know how to do
>this?

If its taking 10 seconds I would reckon that your method or your algorithm is
not the best. If you somehow got pointers to the strings into a TList, sorted
them and put the strings back into the TStringList, I reckon it should not take
10 seconds.

But of course I could be wrong <g>

Alan Lloyd
alangll...@aol.com

Re:TStringList, Sort, and Progress Bar


Quote
Dean Jones wrote in message ...
>All;

>    I perform a customsort on a TStringlist one time after I fill it.
This
>customsort performs exactly what I need but takes roughly 10 seconds (the
>TStringlist has roughtly 3000 rows) to accomplish the task.  I would like
to
>know how I can provide the user a TprogressBar to show that it's working
and
>doing something instead of only the hour cursor.  Does anyone know how to
do
>this?

TStringList.Sort is virtual. You can override it with an instrumented
version where for example every comparison generates an event.

A progress bar also requires an upper bound, which depends on the
sorting algorithm used. I'm not exactly sure I'm getting the details
right here, but I think QuickSort requires exactly n*log(n) comparisons
and at most n*log(n) element exchanges. Log base 2, that is.

Groetjes,
Maarten Wiltink

Re:TStringList, Sort, and Progress Bar


Thanks Maarten for the input.  I left out a detail though.  I have a dialog
that is displayed with a TProgressBar on it.  When the user presses a
button, this is when I do my customsort on TStringList.  The customsort
function is located in a global unit with the rest of my commonly used
functions.  The question is how do I update the progressbar on my form when
the customsort function in located in a different *.pas file.  Or how do I
pass the progressbar into the customsort function as a parameter so I can do
updates to it.  Or is there another way to do it?

Thanks in advance;
Dean

Quote
"Maarten Wiltink" <maar...@kittensandcats.net> wrote in message

news:a5amg6$iua$1@news1.xs4all.nl...
Quote
> Dean Jones wrote in message ...
> >All;

> >    I perform a customsort on a TStringlist one time after I fill it.
> This
> >customsort performs exactly what I need but takes roughly 10 seconds (the
> >TStringlist has roughtly 3000 rows) to accomplish the task.  I would like
> to
> >know how I can provide the user a TprogressBar to show that it's working
> and
> >doing something instead of only the hour cursor.  Does anyone know how to
> do
> >this?

> TStringList.Sort is virtual. You can override it with an instrumented
> version where for example every comparison generates an event.

> A progress bar also requires an upper bound, which depends on the
> sorting algorithm used. I'm not exactly sure I'm getting the details
> right here, but I think QuickSort requires exactly n*log(n) comparisons
> and at most n*log(n) element exchanges. Log base 2, that is.

> Groetjes,
> Maarten Wiltink

Re:TStringList, Sort, and Progress Bar


Hi Dean,

I imagine your custom sort runs through some sort of for I := 0 to
Stringlist.Count-1 or something simalar. So why dont you send the
progressbar as part of the procedure to as it steps through the for couter
loop you work out the percentage left to go.

ie

procedure SortSList(SL:TStringList;Progressb:TProgressbar);
begin
for I := 0 to stringlist.count-1 do
 begin
    Progressbar.Position := (I div StringList.Count)*100;

 end;
end; // END procedure

if Your procedure doesn't have a thread running to run the procedure the add
Application.ProcessMessages; so that the screen updates.
This may slighty affect time or sorting .. but who cares it looks good

Thanks

Aaron

if you have any dramas email me supp...@qbdthebookshop.com

Quote
"Dean Jones" <ptsoftw...@home.com> wrote in message

news:h49e8.67289$dG.27983999@news1.rdc1.sdca.home.com...
Quote
> Thanks Maarten for the input.  I left out a detail though.  I have a
dialog
> that is displayed with a TProgressBar on it.  When the user presses a
> button, this is when I do my customsort on TStringList.  The customsort
> function is located in a global unit with the rest of my commonly used
> functions.  The question is how do I update the progressbar on my form
when
> the customsort function in located in a different *.pas file.  Or how do I
> pass the progressbar into the customsort function as a parameter so I can
do
> updates to it.  Or is there another way to do it?

> Thanks in advance;
> Dean
> "Maarten Wiltink" <maar...@kittensandcats.net> wrote in message
> news:a5amg6$iua$1@news1.xs4all.nl...
> > Dean Jones wrote in message ...
> > >All;

> > >    I perform a customsort on a TStringlist one time after I fill it.
> > This
> > >customsort performs exactly what I need but takes roughly 10 seconds
(the
> > >TStringlist has roughtly 3000 rows) to accomplish the task.  I would
like
> > to
> > >know how I can provide the user a TprogressBar to show that it's
working
> > and
> > >doing something instead of only the hour cursor.  Does anyone know how
to
> > do
> > >this?

> > TStringList.Sort is virtual. You can override it with an instrumented
> > version where for example every comparison generates an event.

> > A progress bar also requires an upper bound, which depends on the
> > sorting algorithm used. I'm not exactly sure I'm getting the details
> > right here, but I think QuickSort requires exactly n*log(n) comparisons
> > and at most n*log(n) element exchanges. Log base 2, that is.

> > Groetjes,
> > Maarten Wiltink

Re:TStringList, Sort, and Progress Bar


Quote
"Dean Jones" <ptsoftw...@home.com> wrote in message

news:h49e8.67289$dG.27983999@news1.rdc1.sdca.home.com...

Quote
> Thanks Maarten for the input.  I left out a detail though.  I have a
dialog
> that is displayed with a TProgressBar on it.  When the user presses a
> button, this is when I do my customsort on TStringList.  The customsort
> function is located in a global unit with the rest of my commonly used
> functions.  The question is how do I update the progressbar on my form
when
> the customsort function in located in a different *.pas file.  Or how do I
> pass the progressbar into the customsort function as a parameter so I can
do
> updates to it.  Or is there another way to do it?

1. 10 seconds to sort 3000 items is simply way way way too slow.

2. In D5 and above tStringList has a CustomSort method, you simply provide a
compare function.

3. Pass the progress bar as a parameter:

        procedure MyCustomSort (. . .; pBar : tProgressBar);
        . . .
            pBar.StepIt;
        . . .

Re:TStringList, Sort, and Progress Bar


In article <h49e8.67289$dG.27983...@news1.rdc1.sdca.home.com>, "Dean Jones"

Quote
<ptsoftw...@home.com> writes:
> The question is how do I update the progressbar on my form when
>the customsort function in located in a different *.pas file.  Or how do I
>pass the progressbar into the customsort function as a parameter so I can do
>updates to it.  Or is there another way to do it?

I think your major problem is not that, but is obtaining some value which is a
measure of "sort done" or "sort left to do". Most sorts repeatedly loop through
the items, changing item positions until none have been changed during a loop.
In that situation it is difficult to measure a "sort completed" because at any
one time the sort does not know what's left to do.

For a bubble sort one might use the count of items interchanged in a loop, but
that would be meaningless unless its a proportion of the first loop count - and
even that might also be meaningless - I'm not into that type of maths.

The real answer is to use an efficient sort, when 3000 would be done in less
than half a  second, and you would not need a progress bar.

Alan Lloyd
alangll...@aol.com

Re:TStringList, Sort, and Progress Bar


Quote
AlanGLLoyd wrote in message

<20020225115530.17007.00000...@mb-co.aol.com>...

Quote
>In article <h49e8.67289$dG.27983...@news1.rdc1.sdca.home.com>, "Dean
Jones"
><ptsoftw...@home.com> writes:

>> The question is how do I update the progressbar on my form when
>>the customsort function in located in a different *.pas file.  Or how do
I
>>pass the progressbar into the customsort function as a parameter so I can
do
>>updates to it.  Or is there another way to do it?

>I think your major problem is not that, but is obtaining some value which
is a
>measure of "sort done" or "sort left to do". Most sorts repeatedly loop
through
>the items, changing item positions until none have been changed during a
loop.
>In that situation it is difficult to measure a "sort completed" because at
any
>one time the sort does not know what's left to do.

Only Bubblesort does that, and it's not very hard to determine
the upper bound for it, either.

(For an N element list, Bubblesort makes N passes, bubbling small
values to the beginning of the list. After N passes, the first N
elements are sorted, so every pass after the first is one shorter
than the previous one - start with a full pass, end with a pass
comparing only the last two elements. Sum all integers from 2 to N,
that's _exactly_ ?N(N+1)-1 comparisons, and _up to_ that number of
exchanges of two elements. So make the comparisons the metric,
instrument for that, and there is your progress bar.)

But Bubblesort is not a good sorting algorithm for arbitrarily
long lists, or even for lists of bounded but considerable length.
Not that that's the point; the point is that for any _given_
sorting algorithm, the total number of comparisons involved is
very likely easy to determine, and is also a reasonable metric.

Groetjes,
Maarten Wiltink

Re:TStringList, Sort, and Progress Bar


In article <a5gt8g$aq...@news1.xs4all.nl>, "Maarten Wiltink"

Quote
<maar...@kittensandcats.net> writes:
>Not that that's the point; the point is that for any _given_
>sorting algorithm, the total number of comparisons involved is
>very likely easy to determine, and is also a reasonable metric.

As I said ...

Quote
>I'm not into that type of maths.

... and as I also said ...

Quote
>The real answer is to use an efficient sort, when 3000 would be done in less
>than half a  second, and you would not need a progress bar.

Alan Lloyd
alangll...@aol.com

Other Threads