SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question My 1st PHP OOP site, the performance seems slow.

    Hi

    I have just built my 1st php site using objects but it seems to be much slower than usual sites I've built.

    I've used __autoload to load classes, and foreach a lot more as opposed to while loops are these things slow? Or could it just be thats its using a lot more memory by holding arrays of objects compared to just records of mysql results.

    I found it better to code with OOP but if its going to be slower I'm not sure I can continue this way.

    Any ideas will be apprieciated.

    Thanks

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless your website is subject to huge amounts of traffic or is performing loads of processor-heavy operations (graphic/video processing) the performance loss even in badly written app won't be noticable. In 99.9% cases you'll need to look into configuring your server, caching, making sure you're serving as few files as possible and that they are minimised, and all networking related matters rather than optimising your PHP.

    In most cases PHP processing accounts for less than 1% of response time, the other 99% is network transfer.

    The only back-end reason for the slowness may be unoptimised database queries.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by andygrant View Post
    I've used __autoload to load classes, and foreach a lot more as opposed to while loops are these things slow? Or could it just be thats its using a lot more memory by holding arrays of objects compared to just records of mysql results.
    Technically speaking there may be a slight overhead in some of these features, but they are extremely small.

    If you think the site is going slow, you should use a profiler to figure out where the slowness is. Xdebug works well for this - Combine it with either webgrind, wincachegrind or kcachegrind (Depending on your platform) to read the output.

    Most likely, the bottleneck is in the database calls. Make sure to minimise the number of calls as much as possible, and analyse any complex queries (use mysqls describe feature).

  4. #4
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The site is running on shared hosting so I can't install anything on the server. Do profiler tools normally required installing things?

  5. #5
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I noticed that my OOP site was 24 times slower than my normal sites.

    Turns out, all those function calls for objects (have __get / __set / standard functions) slow stuff down quite a bit.

    Old way:
    - I would have $data = db row
    - and access it with $data['foo']
    - DB work would make up ~75% of the page build time.

    OOP way:
    - have an object $data
    - have a for loop to populate the $data object ( $data->foo ) from the db select.
    - and then another function call to get the property value ( $data->foo ).
    - and since i'm a lazy typer, I have __get / __set for those...
    - DB work would make up ~25% of the page build time (same sql time as before, but more loops/ifs/stuff slowing things down).

    All this is WAY slower than the old way...

    For a "simple" page that would display some data from the database (2 selects and 4 loops, 2 get / 2 echo), I ended up having the 2 selects, plus the 4 loops, plus 500+ function calls, + some more IFs/Loops in my objects to set the data correctly.

    Problem is, once you get used to OOP, and find out that it's easier to maintain the code that way, you can't go back...

    I would be interested to see how others solved the performance problem, on shared servers (developing on WAMP where I can install anything), since everyone keeps saying that there is only "a slight overhead in some of these features, but they are extremely small."

  6. #6
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My classes aren't particulally complicated but I guess the OOP technique I used issues more SELECT statements than before.

    Basically all my classes are created using an id, then they do a SELECT using that id and loop through the fields populating each variable in the object. If a classes extends another class they each do SELECTS as well, rather than if I did a single SELECT accross multiple tables at once the old way.

    So if I did a search for 'news items' it would get the id's for each news item and create each news object using the id, and each object would do a SELECT to get the full details.

    I'm not sure how else to do it really, does this sound like a poor technique?

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by andygrant View Post
    I'm not sure how else to do it really, does this sound like a poor technique?
    Yes, that is probably your bottleneck. IO is some of the most expensive operations you can make in any program.

    Instead of having the object being responsible for loading itself, how about using a separate object, that creates the entities. Eg.:

    PHP Code:
    $articles = new ArticleGateway($db);
    foreach (
    $articles->selectNewest() as $article) {
      
    // ... do stuff with that $article

    The articlegateway will then issue a single query and create an instance of Article for each row in that resultset.

  8. #8
    Always learning viveknarula's Avatar
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to review your code. Make sure that you close() every MySql connections after opening it in the start.

    Select only those fields those you require instead of using "*" in the select queries.

  9. #9
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On a shared environment, performance is going to be an issue. You can't benefit from streamlining apahce, moving processor intensive business logic to C or C++ or installing various PHP modules -- APC for instance -- that can significantly decrease an application's performance. There are couple things that you can do however to increase an application's responsiveness. Make sure your properly index your database tables and cache whenever possible. Caching is your best bet to increasing performance.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Basically all my classes are created using an id, then they do a SELECT using that id and loop through the fields populating each variable in the object. If a classes extends another class they each do SELECTS as well, rather than if I did a single SELECT accross multiple tables at once the old way.
    If you spotted this one, there may be more.

    Sometimes you have to be pragmatic, and recognise that for the one or two instances your application contains cases like this, you build in something that can break out of the particular paradigm you have bought into.

    My models are not terribly complex but I have found if I build in a passThru( $sql ) ; method as I go, I have to option to circumvent (undermine?) the dogma, and just get the complex join I need. It becomes a brittle break point though.

    If not, timely caching is another way round it, as has already been said.

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    objects and classes tend to express solutions in a more english like way, but by adding redundant calls or taking a longer path to the end result.

    it's a balance guys, you can't have OOP code and expect a fully optimised solution, it just does what you tell it to do
    Last edited by antirealm; Feb 18, 2009 at 07:28. Reason: edit...

  12. #12
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my opinion if you create your own classes, you may have fully optimized solution.

  13. #13
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by salahsoft View Post
    In my opinion if you create your own classes, you may have fully optimized solution.
    i'm intrigued, please explain more.

  14. #14
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by antirealm View Post
    objects and classes tend to express solutions in a more english like way,
    No they dont

    Quote Originally Posted by antirealm View Post
    but by adding redundant calls or taking a longer path to the end result.
    How so? Redundant calls? That makes no sense at all. OOP is all about code reuse so how where get redundancy in that is beyond me.

    Quote Originally Posted by antirealm View Post
    it's a balance guys, you can't have OOP code and expect a fully optimised solution, it just does what you tell it to do
    Sorry that's absolute BS -.-
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  15. #15
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Write the complete code for your own application instead of using the ready-made classes. You will have a complete optimization of code.

  16. #16
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by salahsoft View Post
    Write the complete code for your own application instead of using the ready-made classes. You will have a complete optimization of code.
    I'm pretty sure that's a logical fallacy >.>... Just because you write your classes doesn't make your code optimized. In fact depending on your knowledge, writing your own classes can make your application incredibility bloated and slow.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  17. #17
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes OOP is about code reuse, correct. but what does that have to do with how OOP reads?

    $rectangle->draw()
    $image->compress()
    $thread->execute()
    if ($list->isEmpty())
    $list->add( $person->firstName() )

    What's not english about that?

    how do you claim that using OOP will result in an optimised solution? some optimisation can be acheived by the compiler yes, but not everything.

    a lot of the time you don't know what an object does you just know it's interface. how many times have u seen an object filled with properties and methods to make it more flexible?

    what if you want to take an object and cut out all the crap you don't need? some of these GUI classes are overloaded with IMO not always necessary fields and methods

  18. #18
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by antirealm View Post
    yes OOP is about code reuse, correct. but what does that have to do with how OOP reads?

    $rectangle->draw()
    $image->compress()
    $thread->execute()
    if ($list->isEmpty())
    $list->add( $person->firstName() )
    PHP Code:
    $od9834l->ialdi(); 
    That's not english. But it's valid. Code readability is determined by usage of classes or objects.

    Quote Originally Posted by antirealm View Post
    how do you claim that using OOP will result in an optimised solution? some optimisation can be acheived by the compiler yes, but not everything.
    I'm not saying that it will or will not. Using Classes and objects isn't intrinsically optimized or un-optimized. Optimization to write or rewrite so as to maximize efficiency and speed in retrieval, storage, or execution. It's a process.

    Quote Originally Posted by antirealm View Post
    a lot of the time you don't know what an object does you just know it's interface. how many times have u seen an object filled with properties and methods to make it more flexible?
    What does that have to do with PHP?

    Quote Originally Posted by antirealm View Post
    what if you want to take an object and cut out all the crap you don't need? some of these GUI classes are overloaded with IMO not always necessary fields and methods
    In PHP? Sure why not? It's a scripted language isn't it? Source is visible right? I don't see why that would be a problem...

    Optimization in PHP is much different than in C++, but I assure you optimizing C++ code is completely possible. In any case, the scope of this forum, this thread is PHP.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  19. #19
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, sure. the way a library or class is written will have some effect on it's readability.

    $od9834l->ialdi();

    here someone chose a bad variable name, and someone else (possibly the same person) chose a bad method name. ok there is an exception if ialdi happens to be an important signal to that object in a weird world. like $modem->ath0();

    i mean you could write a function hangUp(), or whatever that command does to make it more english, but that's another topic on gateways and/or abstractions

    i don't recommend going to change source code of a library/class that is shared across multiple apps, just to make that one app "better". the other apps may and probably will break, and require at least some refactoring, unfortunately. i mean you could copy the whole library and use just that one copy for that app but that really really really eliminates the whole point of code reuse and code extension

    i guess my final view on this topic is that if your oop site is slow then try and find bottlenecks in your code and see why it is so slow. is it really the fact that it is now oop that is the problem? this could be anything from using poor oop techniques to poor use of __autoload

  20. #20
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    names of function, classes and variables does not affect the speed of execution.

  21. #21
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by salahsoft View Post
    names of function, classes and variables does not affect the speed of execution.
    It actually does, but the impact it has on execution is generally not noticeable.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  22. #22
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    It actually does, but the impact it has on execution is generally not noticeable.
    sounds interesting, please give an example.

  23. #23
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by salahsoft View Post
    sounds interesting, please give an example.
    PHP is an interpreted language. Even formatting of your code has (faintest) impact on performance because the parser needs to traverse the source on execution time (as opposed to compiled languages).

  24. #24
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    It actually does, but the impact it has on execution is generally not noticeable.
    TBH dosen't make sense. I can say that big variable name take little bit more memory. However , the speed of execution is same.

    Also want to make sure calling an extra method/function doesn't slow down the performance of the system. Here is the example
    Example 1
    PHP Code:
    echo 'Umang'
    Example 2
    PHP Code:
    echoMyName();
    function 
    echoMyName(){
    echo 
    'Umang';

    Example 1 and 2 both have same execution time. I am not very sure how much you know about software development.Try calculating Big O of the code.

  25. #25
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    PHP is an interpreted language..
    Agreed
    Quote Originally Posted by decowski View Post
    Even formatting of your code has (faintest) impact on performance .
    Agreed.
    Quote Originally Posted by decowski View Post
    because the parser needs to traverse the source on execution time (as opposed to compiled languages).
    Agreed to certain extend.

    Formatting of the code for any language is vital for us rather than for the computer which is going to interpret or compile the code. It improves our readability .For most languages spaces, tabs , whitespace ,etc are removed even before it is parsed.

    BTW, backspace is not same as detele. (just for info)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •