Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
OOP : Method Call slower than function call
Message
 
À
21/09/1998 14:23:01
Dave Nantais
Light speed database solutions
Ontario, Canada
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00139174
Message ID:
00139205
Vues:
20
Hi Dave,

Since I was the one who stated this... :)

>it was stated that a method call is UP TO 7 times slower than a function call.

Just let me make clear that it's not always 7 times, it can be less, and in most cases it's less, but it's always slower than a function call. How much depends on the number of objects, on the number of object levels, on the number of class levels, on the number of methods, etc.

>What conditions make the method call that much slower.

The reason why a method call is slower caused by the way VFP handles methods and function calls. In strong typed languages a function or method call can be compiled and either the adress of that function or an index in an adress list can be determined at compile time. Not so in VFP. Since it doesn't know with what kind of object it deals when you tell VFP to execute oObject.MyMethod() all it knows is that it should try to execute a method named "MyMethod".

At runtime the following happens (roughly, only the developers of VFP know what exactly happens). First of all VFP finds a variable "oObject". Since the name only is of no use, it looks up this variable in a so called name list and determines the NTI, the name table index. Some references like THIS, etc. can directly be looked up. Probably, the list is searched sequentially, so there more variables you have, the more time a method call takes (not much, since the search is performed in memory and written in C++).

With that NTI it can retrieve a structure that identifies the object. This structure points to a list with all available method. Again this list has to be searched. Now, VFP knows the method. But before it can execute it, it has to search the class hierarchy for a method of the same name that contains some code. Finally, it can call the method.

If you use references that have more than one object like Thisform.Pageframe1.Page1.Grid1.Colum1.Textbox.Refresh, it has to perform this search for every part of the reference, first for thisform, the for Pageframe1, then for Page1, etc. This search is always based on the name, not on an index. VFP has to compare strings to find the actual method.

BTW, this is the reason why using WITH...ENDWITH speeds up method call. In this case, the initial search has only to be performed once and VFP has already the proper object to seach the method. Therefore almost all of my methods start with WITH THIS.

>Are there certain guidelines i can follow to minimize the time required for a method call ?

Use WITH...ENDWITH if you call more than one method or access more than one property of the same obejct.

>Also if disk access is the slowest part of the process at say 10000 times slower than executing a method call or 70000 times slower than
>making a function call then isn't the difference in speed irrelevant?

In most cases yes. The actual execution of a function/method is in most cases much slower than the function call itself. It's only in large loops where this can be important, and I'd vote in favour of readable code over code that is optimized to save a few milliseconds.

Christof
--
Christof
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform