Re: Objects and Performance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Greg Donald wrote:
> On 6/8/05, NSK <nsk3@xxxxxxxxxxxxx> wrote:
> 
>>Hi, I am creating a small API in PHP, i.e. a collection of reusable functions
>>for other programmers. As it is now, it is just many functions together in
>>the same file. Do you think I should make it object-oriented? What are the
>>pros and cons of this approach in PHP?
>>
>>I am particularly very concerned about performance. Are object methods slower
>>than functions in php?
> 
> 
> Definitely not scientific but have a look:
> http://destiney.com/Benchmarks

If you want to compare object methods versus functions, you should
compare identical code inside a method to code inside a function.
Greg's benchmarks compare two different ideological code structures.
Quite honestly, the wrapping of mt_srand() inside a function or method
does not satisfy the definition of good coding, object-oriented or
otherwise.

Functions/methods should be used to abstract common operations.
mt_srand() already does this necessary abstraction, and putting a
further wrapper around it makes no sense.

Nobody can give you a good answer to your question without seeing the
specific code.  If your code requires a lot of global variables in order
to operate, you might consider abstracting this into a class.  Also, if
you find yourself creating several copies of the same variables, use
classes in order to allow two incarnations of the code to exist in the
same file as objects.

If your code does simple operations on a few operands and returns a
value, like our mt_srand() function, there is absolutely no point in
making it more complicated.

However, you could consider wrapping the functions inside a class and
using them as static methods.  This would essentially give them a
"namespace" that would allow any conflicts with other programmer's
function names to be more easily resolved.  In other words, renaming a
class is a whole lot simpler than renaming a function (think
search-and-replace "ClassName::" versus "functionprefix_" - the second
one might inadvertantly replace variables, etc., but the first is
unusual syntax)

So, in short, analyze the problems your code solves, and see if they
would be better solved by abstracting into classes, or by keeping the
code as functions.  Forget about minor performance differences from
program syntax unless you're writing code for a site with thousands of
hits per second.  In that case, you'll want to use a profiler like APD
to find the real bottlenecks in your code.  Otherwise your main
inefficiency is going to be programmer time rather than processor time:
make the code readable and maintainable and it will cut down on bugs and
work better than "faster" code.

Greg

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux