Slow SET PROCEDURE TO and NEWOBJECT()
September 30, 2006 •
I’ve been doing some performance testing in Web Connection earlier today and as I was running through some of the stress routines I noticed that one of my samples was running noticeably slower than many others. Web Connection requests are typically pretty quick – usually somewhere around 100th of a second or even less so for many requests the FoxPro timer resolution doesn’t even register a time at all <s>…
But one of the requests was taking quite a bit longer than any of the others and to top it off this is a real simple request. It turns out that the slowdown is caused by an explicit SET PROCEDURE TO command…
This is one thing that’s bugged me about VFP for some time: Loading of libraries is pretty slow and in Web code you certainly would want to put any SET PROCEDURE TO code into your server’s startup code (that is with Web Connection – can’t do that with ASP/COM since there’s no persistence).
So you might say, yeah I never do that, but if you’re using NEWOBJECT() you maybe surprised to find that NEWOBJECT Is nearly as slow as loading a library from scratch. Try this code with a PRG
SET PROCEDURE TO
CLOSE ALL
Sec = SECONDS()
#DEFINE TYPE 0
lcLibrary = "wwPop3"
#IF Type = 0
SET PROCEDURE TO (lcLibrary)
#ENDIF
FOR x = 1 TO 1000
*** Preloaded
#IF Type = 0
loPop3 = CREATEOBJECT(lcLibrary)
#ENDIF
*** Load every time
#IF Type = 1
SET PROCEDURE TO (lcLibrary)
loPop3 = CREATEOBJECT(lcLibrary)
#ENDIF
*** NewObject
#IF Type = 2
loPop3 = NewObject(lcLibrary,lcLibrary + ".prg")
#ENDIF
*** 'Manual' check
#IF Type = 3
IF ATC(lcLibrary,SET("PROCEDURE") ) < 1
SET PROCEDURE TO (lcLibrary)
ENDIF
loPop3 = CREATEOBJECT(lcLibrary)
#ENDIF
ENDFOR
? SECONDS() - Sec
RETURN
NewObject is nearly 10 times slower that the ‘manual’ check shown on the bottom which is a pretty painful perf hit. That should make you think twice about using NEWOBJECT vs. SET PROCEDURE or SET CLASSLIB and then using plain CREATEOBJECT.
The problem is that NEWOBJECT() has to perform all sorts of checks for files and file locations I suspect, vs. the manual code assuming the code exists in the procedure stack only. So there’s no checking for a file inside of an APP/EXE vs. disk etc. Still 90% of the time the latter is probably way more efficient.
This is probably not a big issue in normal apps but in Web apps, that take 0.01 of a second having NEWOBJECT() or SET PROCEDURE can often double the request time which is rather significant.
Sylvain Bujold
October 03, 2006