Speed.. functions or plain code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
crazy_fox
Forum Newbie
Posts: 3
Joined: Fri Feb 10, 2006 11:39 am

Speed.. functions or plain code?

Post by crazy_fox »

I am making an engine that will be looping many times, a switch statement. Should i get the algorithm located in the statements into functions? Will that increase the execution speed? Or should i leave is as a plain code?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

First of all, do you know that it is too slow. If you don't -- why optimize.

All inline will technically be the fastest. But you need to maintain the code, so if there is reusable code and you duplicate it several times you will have to maintain all the copies in parallel.

And finally, why are you looping around a switch statement in the first place? Is sounds like there might be a better design...
(#10850)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

if you mean looping over plain code is faster than looping over functions...looping over code is faster as you avoid calls and return of calls to functions..
User avatar
crazy_fox
Forum Newbie
Posts: 3
Joined: Fri Feb 10, 2006 11:39 am

Post by crazy_fox »

Right, i am getting some results back from a database. Base on a bit of the info i get back, i decide which algo has to be excecuted..

We are using some functions right now, but i was wondering wether it would be faster getting it all in functions...

Thanks for the answer :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sounds kinda like a situation for a state machine, but I'd like to see the decision logic before I can fully make such a recommendation.
User avatar
crazy_fox
Forum Newbie
Posts: 3
Joined: Fri Feb 10, 2006 11:39 am

Post by crazy_fox »

The array i get back from every fetch array.. i use one of the columns to trigger one of the cases. The only other possible way would be to use multiple ifs, but i avoid them in such situations for simplicity.

Notice: There are other developers working on the same file.. so..
Post Reply