Page 1 of 1

Speed.. functions or plain code?

Posted: Fri Feb 10, 2006 12:19 pm
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?

Posted: Fri Feb 10, 2006 12:25 pm
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...

Posted: Fri Feb 10, 2006 12:25 pm
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..

Posted: Fri Feb 10, 2006 12:29 pm
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 :)

Posted: Fri Feb 10, 2006 12:39 pm
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.

Posted: Fri Feb 10, 2006 12:43 pm
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..