Function or Class - which is faster?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
computernoob
Forum Newbie
Posts: 1
Joined: Mon Jun 08, 2009 10:41 pm

Function or Class - which is faster?

Post 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?
Last edited by Benjamin on Tue Jun 09, 2009 11:54 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Function or Class - which is faster?

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Function or Class - which is faster?

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Function or Class - which is faster?

Post by kaisellgren »

Don't worry about the performance, just care about the security first.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Function or Class - which is faster?

Post 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. :)
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Function or Class - which is faster?

Post 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.
Raimo Roopertti
Forum Newbie
Posts: 6
Joined: Wed Apr 22, 2009 4:23 am

Re: Function or Class - which is faster?

Post 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.
Last edited by Benjamin on Thu Jun 11, 2009 8:15 am, edited 1 time in total.
Reason: Removed Spam Link ... And I'll find the rest too
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Function or Class - which is faster?

Post 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?
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Function or Class - which is faster?

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