Re: Objects and Performance

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

 



On 6/8/05, Greg Beaver <cellog@xxxxxxx> wrote:
> 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.

I called mt_srand() inside the constructor, so it's only called once,
same as the procedural version.  This being an example of a class, it
seems like the logical place to put it.  I can remove it from both
versions but it's not gonna change much as far as speed.

> 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.

I agree.  In the real world a given task will likely be more
complicated, but that's not the point here.

> However, you could consider wrapping the functions inside a class and
> using them as static methods.

There is little point in making classes to then only use the methods
statically.  You could just put all your similar functions in an
include file and be done with it.  Nothing more annoying than working
on someone else's OO code and seeing Foo::bar() all over the place. 
Pointless.

> This would essentially give them a
> "namespace" that would allow any conflicts with other programmer's
> function names to be more easily resolved.

If PHP needs namespaces then add them, last I heard the consensus was
that it didn't.  I've never needed them personally.  It doesn't seem
like enough justification to use objects just because you don't want
to rename a few functions in the rare event you actually have a
conflict.  Seriously, how many times have you been working on a PHP
project that was so big, with so many developers, that you bumped into
each other with function names?  Has yet to happen to me.

> 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)

Renaming anything is simple when you use good tools.

for file in *.php; do
cp $file $file.tmp
sed -e "s/foo\(/prefix_foo\(/g" $file.tmp >$file
rm $file.tmp
done

> make the code readable and maintainable and it will cut down on bugs and
> work better than "faster" code.

I always value efficient code over time to code.  Doing more with less
hardware should always be a programmer's first goal in my opinion. 
The overhead involved in bringing a web-based object into existence
for the extremely short period of time it will live can never justify
the 'maintainability' and 'readability' arguments I'm always hearing
preached.  I've never seen any OO PHP code that was easier to read or
maintain for that matter.


-- 
Greg Donald
Zend Certified Engineer
http://destiney.com/

-- 
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