Colum
Colum

Reputation: 3914

Preformance difference between classes and just functions

I am writing a site in PHP and I am noticing that the page is taking 3-5 seconds to load (from a remote server), which is unacceptable. The software relies on around 12 classes to function correctly. I was wondering how much of a performance gain I would get if I rewrote most of the classes to just use regular php functions.

Thanks for any input.

Edit: I rely primarily on Redis, and using a simple MySQL query here and there.

Upvotes: 1

Views: 605

Answers (4)

Bogdan Constantinescu
Bogdan Constantinescu

Reputation: 5356

Rewriting all the application in procedural programming is probably the worst thing you can ever do. Object oriented programming is not about performance gain but writing programmer-friendly and easily maintainable applications (among others).

You should not ever think about rewriting an OO application procedural. That's not the purpose. If you ever have bigger resource consumption using OO rather than procedural programming (and that is very unlikely) you should probably think about better scaling the app. Hardware nowadays is not that expensive.

On the other hand, your application has many possible bottlenecks and OO is probably not even on the list.

Did you check:

  • your Internet connection?
  • your server's Internet connection?
  • your ping loss to your server?
  • your server's configuration? (Apache/Nginx/Lighttpd or whatever it is)
  • your database server's configuration?
  • your database queries?
  • your server's load?
  • the timing for a connection to Redis?
  • your firewall's rules for the ports used by Redis?
  • your Redis' configuration? (maxclients, timeout)

If you answered NO to at least one question above, please do check that and if the problem persists, let me know!

Upvotes: 2

shmeeps
shmeeps

Reputation: 7853

You wont find much of a difference, if anything at all between functions and classes.

Chances are, one or more of your classes is written inefficiently, or relies on something that is. Examples could include waiting on a remote server, image processing, some databases (such as overloaded databases) or countless other methods. You should try to profile your code to see where the problem lies.

Upvotes: 0

NikiC
NikiC

Reputation: 101946

The difference will probably not even be measurable.

Your problem most definitely isn't your code per se, but the way you access the database. Make sure your tables are appropriately indexed and you will see a substantial drop in page load times. You can use EXPLAIN SELECT ... to get further info on how a query actually runs and why it performs badly.

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 401182

Functions or classes should make little to no difference (totally negligible) : that's not what is making your website / application slow.


Hard to give you more information, as we don't know how your setup looks like, but you might want to take a look at the answer I posted to this question : it contains some interesting ideas, when it comes to performances of a PHP application.


BTW: 12 classes is really not a big number of classes, if I may...

Upvotes: 5

Related Questions