Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

oop advantages

0 views
Skip to first unread message

Raffaele Battaglia

unread,
Nov 29, 2003, 10:21:27 AM11/29/03
to
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
sele 1
use ban
sele 2
use mas
do while .not. Eof()
sele 1
mcb = CB
mNom = Ban
sele 2
sum dare to da for cb= mCB
sum avere to av for cb= mCB
sd= da-av
sele 1
repla sal with sd
skip
enddo
return

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


Roland Wingerter

unread,
Nov 29, 2003, 10:53:32 AM11/29/03
to
Hi Raffaele,

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
>

Jean-Pierre Martel

unread,
Nov 30, 2003, 12:35:45 AM11/30/03
to
In article <bqaa02$l9f$1...@news.dbase.com>, r...@igeco87.it says...

> 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

Raffaele Battaglia

unread,
Nov 30, 2003, 4:28:07 AM11/30/03
to
Hi Roland
Hi Jean-Pierre

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


Roland Wingerter

unread,
Nov 30, 2003, 5:31:25 AM11/30/03
to
Raffaele Battaglia wrote
> Thanks for your answers.
----
Raffaele, you are welcome.

> 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


Raffaele Battaglia

unread,
Nov 30, 2003, 12:03:39 PM11/30/03
to

Hi Jean-Pierre
The time for your function is very low ( less than 1 sec) but it gives
different results. The SAL (balance) in the BAN table is always the same
for the various CB. I will spend some time later in order to understand
where is the difference.
Ciao.
RB


Jean-Pierre Martel

unread,
Dec 1, 2003, 1:53:15 AM12/1/03
to
In article <bqd4bv$3f$1...@news.dbase.com>, r...@igeco87.it says...

>
> The time for your function is very low ( less than 1 sec)
> but it gives different results. The SAL (balance) in the
> BAN table is always the same for the various CB.

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()

Jean-Pierre Martel

unread,
Dec 1, 2003, 2:54:52 AM12/1/03
to
In article <bqd4bv$3f$1...@news.dbase.com>, r...@igeco87.it says...
>
> The time for your function is very low ( less than 1 sec)
> but it gives different results. The SAL (balance) in the
> BAN table is always the same for the various CB.

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()

Raffaele Battaglia

unread,
Dec 2, 2003, 5:18:31 PM12/2/03
to
Apparently it did not change with respect to the previous one.
Ciao.
RB


Jean-Pierre Martel

unread,
Dec 3, 2003, 12:53:11 AM12/3/03
to
In article <bqivhm$uh9$1...@news.dbase.com>, r...@igeco87.it says...

> Apparently it did not change with respect to the previous one.

Sorry.

0 new messages