Then for the sake of language purity, I preferred the OOP version
function SALDIBANCHE_onClick
public da, av, sd, mt1, mt2
da=0
av=0
sd=0
mt1 = time()
form.TEXTLABEL1.text = mt1
form.ban1.rowset.first()
do while not form.ban1.rowset.endOfSet
da=0
av=0
sd=0
form.mas1.rowset.first()
do while not form.mas1.rowset.endOfSet
// add to it
da += form.mas1.rowset.fields["dare"].value
av += form.mas1.rowset.fields["avere"].value
form.mas1.rowset.next() // move to next row
form.TEXTLABEL4.text = form.ban1.rowset.fields["CB"].value
sd = da - av
form.TEXTLABEL5.text = da - av
form.TEXTLABEL4.text = form.ban1.rowset.fields["CB"].value
enddo
form.ban1.rowset.BeginEdit()
form.ban1.rowset.fields["sal"].value = sd
form.ban1.rowset.Save()
form.ban1.rowset.next()
enddo
mt2=time()
mt = mt2-mt1
form.TEXTLABEL2.text = mt2
return
Both pushbutton works perfectly, but I noticed a time difference in the
performances. I put a timer and the difference is quite big. Less than 1
second for the old language and almost 5 second for the OOP.
There is anything wrong in my OOP function?
Since the OOP is more complex (we have to establish parent-child relation,
masterrowset etc) I expected a better performance, otherwise where is the
advantage of using the OOP?
Thank you for the attention.
Ciao
RB
in this case OOP would be slower than the old xBase command sum...to,
because you have to loop through the rowset and add each single value.
But you can speed up your OOP code:
1. declare the variables local (as you do in xdml)
2. shorten object references (yes, it saves time)
oRowset1 = form.ban1.rowset
and then use the short object reference, eg.
oRowset.first(), oRowset.next() etc.
da += oRowset1.fields["dare"].value
(Declare the object reference as local as well).
3. Delete this line, it's not necessary:
> form.ban1.rowset.BeginEdit()
Also, in the OOP code you update a textlable object on the form, which you
don't do in the other version.
> Since the OOP is more complex (we have to establish parent-child relation,
> masterrowset etc) I expected a better performance, otherwise where is the
> advantage of using the OOP?
----
Parent-child relations don't speed things up, on the contrary, they make
things slower. In many cases it is preferrable to do without them and use
findKey() or setRange() in the child table when necessary.
Finally, you can calculate a sum with a single sql statement. This is
probably the fastest way.
Try something like
SELECT cb, sum(mas.dare) AS sum_dare, ;
sum(mas.avere) AS sum_avere ;
FROM ban, mas ;
WHERE ban.cb = mas.cb ;
GROUP BY cb
> I expected a better performance, otherwise where is the
> advantage of using the OOP?
-----
OOP is safer to use. For one thing, you don't have to worry about workareas
(inadevertently closing an open table in the active workarea). There are
more advantages (look for an article on OOP in the Knowledge Base).
Hope this helps
Roland
Raffaele Battaglia wrote
> I want to update 2 numeric fields in a table of about 3000 records (MAS),
> grouped on a field of 20 records table (BAN) .
> As a beginner, the first tihing I did was :
> function BANCHE_onClick
> local da, av, sd
> Then for the sake of language purity, I preferred the OOP version
>
> function SALDIBANCHE_onClick
> public da, av, sd, mt1, mt2
>
> performances. I put a timer and the difference is quite big.
> Less than 1 second for the old language and almost 5 second
> for the OOP.
> There is anything wrong in my OOP function?
What is the time for the following version?
Function SALDIBANCHE_onClick
local da, av, r1, r2
r1 = form.ban1.rowset
r2 = form.mas1.rowset
form.TEXTLABEL1.text = time()
r1.first()
r2.first()
da=0
av=0
do
// add to it
da += r2.fields["dare"].value
av += r2.fields["avere"].value
form.TEXTLABEL4.text = r1.fields["CB"].value
form.TEXTLABEL5.text = da - av
// I am not sure if that's what you want to do
r1.BeginEdit()
r1.fields["sal"].value = form.TEXTLABEL5.text
r1.Save()
r1.next()
until not r2.next()
form.TEXTLABEL2.text = time() - form.TEXTLABEL1.text
return
Jean-Pierre Martel, editor
The dBASE Developers Bulletin
Thanks for your answers.
I applied your suggestions and the time shortened sensbly, especially in
the public-local change ( about 50%). The reference shortening does not make
a big difference, but is very useful in editing the program.
However the time cuts dramatically with the sql opening suggested by Roland.
I never used it before because I had problems (sintax problems), but it
works and solves the problem. I modified my program accordingly. I think
this is the solution for that kind of problem.
Thanks again.
Ciao.RB
> I applied your suggestions and the time shortened sensbly, especially in
> the public-local change ( about 50%). The reference shortening does not
make
> a big difference, but is very useful in editing the program.
-----
Thanks for the success report.
> However the time cuts dramatically with the sql opening suggested by
Roland.
-----
Glad you find it useful.
Roland
Then, might this code work as you want? If so, is it fast?
Function SALDIBANCHE_onClick
local da, av, sd, r1, r2
r1 = form.ban1.rowset
r2 = form.mas1.rowset
form.TEXTLABEL1.text = time()
r1.first()
r2.first()
da=0
av=0
do
// add to it
da += r2.fields["dare"].value
av += r2.fields["avere"].value
sd = da - av
form.TEXTLABEL4.text = r1.fields["CB"].value
form.TEXTLABEL5.text = sd
until NOT r2.next()
r1.BeginEdit()
do
r1.fields["sal"].value = sd
until NOT r1.next
r1.Save()
If I understand correctly, SAL has to be the same. If that's the case,
might the following code work properly? If so, is it fast?
Function SALDIBANCHE_onClick
local da, av, sd, r1, r2
r1 = form.ban1.rowset
r2 = form.mas1.rowset
form.TEXTLABEL1.text = time()
r1.first()
r2.first()
da=0
av=0
do
// add to it
da += r2.fields["dare"].value
av += r2.fields["avere"].value
until NOT r2.next()
sd = da - av
form.TEXTLABEL5.text = sd
// the following line might be put inside the next
// do...until loop if you want to see the value
// of the text be updated as the row pointer goes
// through the r1 rowset (that will slow the
// process).
form.TEXTLABEL4.text = r1.fields["CB"].value
r1.BeginEdit()
do
r1.fields["sal"].value = sd
until NOT r1.next
r1.Save()
Sorry.