Mike Yearwood
Toronto, Ontario, Canada
Versions des environnements
Network:
Windows 2008 Server
>>>>>The age old questions. Should I use a property or a method? Is one faster than the other?
>>>>
>>>>From Tracy's link:
>>>>
>>>>In general, methods represent actions and properties represent data.
>>>>
>>>>Rarely stray from the generalization. That way, in general a property will be faster than a method as there is no cumbersome execution likely to occur.
>>>
>>>What do methods act on? Nothing?
>>
>>Well, obviously parameters. Surely you know a method need not read/write any properties at all. However, imagine a property with a huge set of code fired from a get method and other properties also firing their own set(s) of code. All you're trying to do is get the value and it takes a few seconds? I'd rather rely on accessing a property taking minimal execution time and minimal debugging time too.
>
>Surely I do know a method not read or write any properties at all. I'm not sure you're answering the question that was asked. You're comparing a "huge set of code" to a simple get accessor. My understanding of the original question was which is faster, OTHER THINGS BEING EQUAL? i.e. equal amounts of code.
>
>Personally I also prefer the cleanness of a property. Whether it's actually faster than an equally simple method, I don't know.
A property should not be firing much, if any code, and so should be faster than a method. A method must be executing code and returning a result, where a property does not execute anything and simply returns a result. So it should at least be nominally faster to use a property versus an empty method - and one should not overload a property with much code so that behavior can be expected/relied upon.
Précédent
Répondre
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement