Page 1 of 1

Function or Class - which is faster?

Posted: Mon Jun 08, 2009 10:48 pm
by computernoob
I'm trying to create a login script. For security, on every page I want to run a function that will check if certain session variables are declared and have a value. Is it faster to run a regular PHP function or is an object method just as fast?
Basically, which is faster:

Code: Select all

check_auth();
or

Code: Select all

$checker = new login();
$checker->check_auth();
So which one is faster?

Re: Function or Class - which is faster?

Posted: Tue Jun 09, 2009 11:51 am
by Benjamin
The function would be faster, but you should still use the class. The results are negligible.

:arrow: Moved to PHP - Theory and Design

Re: Function or Class - which is faster?

Posted: Tue Jun 09, 2009 12:20 pm
by alex.barylski
The class would faster as it's not really the one doing anything. :P

Method versus a Function...

In a trivial example like you have shown, the function will win. But a properly design class/object with it's methods will run faster than a complex procedural library.

So it's one trade off versus another, if OOP is done right you will almost always result in faster, easier to read source code.

Re: Function or Class - which is faster?

Posted: Tue Jun 09, 2009 1:02 pm
by kaisellgren
Don't worry about the performance, just care about the security first.

Re: Function or Class - which is faster?

Posted: Tue Jun 09, 2009 2:29 pm
by alex.barylski
Worry about the design first and foremost, but of course, if your asking this question, you have a lot to learn before you can effectively focus on design.

Best advice: Start using objects now, because it'll years before you really know what your doing, so the sooner you get started the better off you are. :)

Re: Function or Class - which is faster?

Posted: Tue Jun 09, 2009 6:15 pm
by omniuni
To some extent, I have found that using functions can be useful. I have a file of common functions that I've written that I include kind of automatically when I start a new script. I can call these from within the classes I create without having to worry about private vars and such. That said, I have found that for any thing that requires several functions, it is more efficient to create a class, and call these functions from the class as you need them. Also, of course, it allows the parsing engine to manage the memory required more efficiently in the long run.

Re: Function or Class - which is faster?

Posted: Thu Jun 11, 2009 12:30 am
by Raimo Roopertti
ALWAYS!
Since the introduction of classes, many have developed huge classes to do many kinds of "things", believing they make things easy.
In fact, they introduce two problems:
1. Since you have not written the class, you do not know exactly how it works.
2. You are loading a huge amount of code (that needs to be processed), just to load your class. Then you use ONE function of that class! Ludicrous!
If you need a newspaper, do you buy the whole Newsagent shop?
So, unless you intend to use the class INTENSIVELY, and for several similar objects, throughout your code, don't bother!
Same if you need the class only once.

Re: Function or Class - which is faster?

Posted: Thu Jun 11, 2009 2:31 am
by allspiritseve
computernoob wrote:So which one is faster?
I did a quick test with the code you posted, expecting the function to be marginally faster. Turns out, the class on average was faster by around .0000527 (10 trials). That's 52.7 millionths of a second faster, by the way.
Raimo Roopertti wrote:ALWAYS!
Since the introduction of classes, many have developed huge classes to do many kinds of "things", believing they make things easy.
So... you're advocating smaller classes?
Raimo Roopertti wrote:1. Since you have not written the class, you do not know exactly how it works.
I didn't build my car either...
Raimo Roopertti wrote:2. You are loading a huge amount of code (that needs to be processed), just to load your class. Then you use ONE function of that class! Ludicrous!
Got it. I will use TWO functions (methods?) from now on.
Raimo Roopertti wrote:So, unless you intend to use the class INTENSIVELY, and for several similar objects, throughout your code, don't bother!
Agreed, no moderate use of objects here. Go big or go home.
Raimo Roopertti wrote:Same if you need the class only once.
Yeah, just don't bother. You know you'll never need it in the future, who reuses code anymore these days?

Re: Function or Class - which is faster?

Posted: Thu Jun 11, 2009 4:25 am
by kaisellgren
allspiritseve wrote:
Raimo Roopertti wrote:2. You are loading a huge amount of code (that needs to be processed), just to load your class. Then you use ONE function of that class! Ludicrous!
Got it. I will use TWO functions (methods?) from now on.
:D