Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Speed problem
Message
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Speed problem
Miscellaneous
Thread ID:
00445751
Message ID:
00445751
Views:
43
Hi everybody,

I have a quite simple program for pre-populating a table. For RI it has 28152 iterations, for MA ~236000. For some reason, it works really slow (1min.56 sec. for RI, 27 min. for MA). I suspect server's problem, but our Network Administrator tells me, that server is ok. I tried switching order between scan...endscan and for...next. Once this program executes really fast, but then again worked very slow. Could you please tell, is something wrong with the code?
How can I speed up this program?
********************************************************************
*  Description.......: PrepopulateStats - program to pre-populate Stats table
*  Calling Samples...: PrepopulateStats('MA','1990','2001')
*  Parameter List....: tcState, tcStartYr, tcEndYr
********************************************************************
lparameters tcState, tcStartYr, tcEndYr
local lnSeconds, lnMinutes, lnStartTime, lnElapsedTime
* note clock reading for generating final timing statistics
lnStartTime = seconds()                         && # seconds since midnight

**** Validation parameters first
if empty(tcState) or vartype(tcState)<>'C'
     tcState='MA'
else
     tcState=upper(tcState)
endif
if empty(tcStartYr) or vartype(tcStartYr)<>'C'
     tcStartYr='1990'
endif
if empty(tcEndYr) or vartype(tcEndYr)<>'C'
     tcEndYr='2001'
endif
local lnStartYr, lnEndYr, lnTnRec, lnTally, PrevOnEsc, PrevEscape, lnCount, ;
     halt, lnUpdateNumber, msgTail, loTherm, lnYr, lnPeriod, lnGrp

lnStartYr=val(tcStartYr)
lnEndYr=val(tcEndYr)
select ccode, town from towns ;
     where State=tcState and ;
     left(town,2)<>'RR' ;
     union ; && 1 State record
select 'ZZ' as ccode, 'ZZZZ' as town ;
     from towns ;
     order by 1, 2 ;
     into cursor curTowns
lnTnRec=_tally
lnTally=lnTnRec*3*17*(lnEndYr-lnStartYr+1) && Total number of iterations
* support user Escapes for interrupting the main loop
PrevOnEsc = on('escape')                  && save previous Escape handler
PrevEscape = set('escape')                && previous Escape enablement state
set escape on                             && enable escape handling
halt = .f.                                && allow loop to run until this flag is toggled
on escape halt = .t.                      && force immediate termination if user escapes
store 0 to lnCount
local lcOldStatusBar
lcOldStatusBar=set('status bar')
set status bar on && To display number of processed records
set message to 'Pre-populating statistics table for '+tcState+' '+transform(lnTally)+' records'
do case
case lnTally<100 && Very rare case
     lnUpdateNumber=1
case between(lnTally,100,100000)
     lnUpdateNumber=100
case lnTally>100000
     lnUpdateNumber=val('1'+replicate('0',len(transform(lnTally))-3))
endcase
* assemble fixed portion of status bar message outside of loop, for speed
msgTail = "/" + transform(lnTally) + ".  Wait or press Esc to cancel ..."
if used('WorkStats')
     use in WorkStats
endif
use (tcState+'Stats') in 0 again alias WorkStats && order tag Geo
*--- instantiate thermometer bar class....
loTherm = newobject("thermometer", "wg.vcx","","Progress for: "+dbf('WorkStats'),lnTally)
loTherm.show()
local m.cYear, m.ccode, m.town, m.period, m.UseGrp, ;
     m.NumSales, m.YtdNumSale, m.VolSales, m.YtdVolSale, ;
     m.MedSale, m.YtdMedSale
store 0 to m.NumSales, m.YtdNumSale, m.VolSales, m.YtdVolSale, ;
     m.MedSale, m.YtdMedSale
select curTowns
scan
     scatter memvar
     for lnYr=lnStartYr to lnEndYr
          m.cYear=str(lnYr,4)
          for lnPeriod=1 to 17
               if lnPeriod<=12
                    m.period=padl(lnPeriod,2,'0')
               else
                    if lnPeriod<17
                         m.period='Q'+str(lnPeriod-12,1)
                    else
                         m.period='YR'
                    endif
               endif

               for lnGrp=1 to 3
                    if lnGrp=1
                         m.UseGrp='1FA'
                    else
                         if lnGrp=2
                              m.UseGrp='CND'
                         else
                              m.UseGrp='ALL'
                         endif
                    endif
                    lnCount=lnCount+1
* check for user Escape
                    if m.halt                                   && user escaped
                         exit                                   && fall out of loop
                    endif
                    if mod(lnCount,100) = 0
                         set message to 'Record # '+transform(lnCount)+m.msgTail
                    endif     
** Update thermometer 
                    if mod(lnCount,lnUpdateNumber) = 0
                         loTherm.update(lnCount)
                    endif
                    *select WorkStats
*                    if !seek (m.ccode+m.town+m.cYear+m.period+m.UseGrp,'WorkStats','Geo') && Record doesn't already exist, insert new
** Insert new record
*                    append blank
*                    endif
*                    gather memvar
                insert into WorkStats from memvar
               next
          next
     next
endscan
if lcOldStatusBar='OFF'
     set status bar off
endif
use in curTowns
use in WorkStats
use in towns
** Determine elapsed time
lnElapsedTime = seconds()
lnSeconds=lnElapsedTime - lnStartTime
lnMinutes=int(lnSeconds/60)
lnSeconds=round(lnSeconds-lnMinutes*60,0)
wait "Elapsed time for pre-populating statistics table is " + ;
     iif(lnMinutes>0, transform(lnMinutes)+" min. ","")+;
     transform(lnSeconds) + " sec." window nowait
Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform