I was working on my website written in php/mysql. When I first wrote it, it was spaghetti with lots of php embedded in html and the like - very hard to maintain.
I rewrote the whole thing with a nice modular structure with OOPS, and now it is much easier to maintain and expand.
But when testing the site performance using webwait and siege, the newer, better structured version seems to run and load slower than the spaghetti code version.
There's a difference of nearly 1 second in loading time - 2.39s vs 3.81s
Nothing else was changed except the php code - not the js, not the css
So what is the problem here? Should I revert back to the old code? Has this happened to others?
Edit:
What I want to know is how others deal with this.
I can think of a couple of points to consider:
On top of that, the usual obviously applies (can you optimize the algorithm, enable caching or precompilation, and so on - but while those may help dramatically, they're not specific to OOP)
Profile the code. I've no idea how one does that in PHP, but it's the only reasonable way to work out what's going on.
Sometimes making code more elegant will have a detrimental effect on performance, but not usually to that extent. You need to work out where the time's going, and fix that bit.
There's a difference of nearly 1 second in loading time - 2.39s vs 3.81s
That is a difference of 3.81-2.39 = 1.42s which is over 50% of the smaller value so it isn't a small number to my mind. Have you run your tests multiple times so that an initial compilation/interpretation cost is being amortized properly? Have you considered trying to introduce timers to see where there is more time being taken than before? Those would be my suggestions as it seems you may have introduced a lot of abstraction and are now seeing the price for that.
"Should I revert back to the old code?"
If I say revert, you'll say "see, I knew OO was a blown unit, no one can make an OO application that works." That would be wrong.
If I say don't revert, you'll say, "but it's unacceptably slow."
So, what's left?
You have to write it better. Go forward. Rewrite your OO so that it actually works. OO isn't "magic" -- it doesn't guarantee anything. There are bad OO programs and good OO programs. In your case, you obviously have room for improvement.
So get some performance profiling tools and find out where the time has gone.
Also, don't "optimize" -- rewrite.
Odds are very good that you have some kind of search going on that takes up a lot of time. Eliminate search. Use better containers and collections (hash maps, sets, etc.)
Try setting up Xdebug and seeing what it can tell you. Someone else mentioned that you should check it out with a profiler. I agree, and Xdebug can provide you with one as well as some other useful functionality. Your IDE of choice may even integrate with Xdebug.
One thing to consider: are you using APC or some other PHP opcode caching solution? If not, all your code is being re-interpreted from scratch every time you load a page. This could definitely affect performance.
OOP means a lot of function calls, and function calls in dynamic languages are slow. So "translating" old code into OOP version will slow it down. Do a complete rewrite.