SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    General questions

    Hi there,

    I've got a few basic questions, would love if you could provide some insight. Recently I've been going optimization/efficiency crazy on my site coding. I'm coding a potentially high-traffic site, and anything that will make it run smooth I will definately jump for.

    I'm looking for an *efficient* and simple way to provide different languages on my website. Would you happen to know of a technique or a piece of code that will make this easy and efficient? Personally I think the most efficient way would be to use a $lang array in a separate PHP file (with the appropriate translations) then swap the file based on the users selection.

    I noticed some people are moving their sites entirely to OO programming in PHP5. I'm assuming they are doing that because it helps organize code, but are they also doing that because it can make the site faster? ie. the processing time. I've read that the new Zend engine is a lot more efficient with the way it's structured classes and such, if I were to code my site entirely in OO classes, would this be a good thing? lol

    I've heard of a practice called "releasing vital memory", a friend told me that you should always destroy/unset a block of data at the earliest time, ie. if you aren't using the variable anywhere else on the script you can use unset() or mysql_free_result to release the memory allocation. This is ultimately done at the end of the scripts execution, but it could make a difference if you get rid of it earlier...right?

    Sorry if my questions sound amateurish - I haven't learned all the nitty-gritty behind the Zend engine so while I'm coding I'm just wondering what the best practices are. My theory is even if I can reduce a scripts execution by a couple milliseconds in high-traffic it should make a big difference.
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by davedibiase
    I'm looking for an *efficient* and simple way to provide different languages on my website. Would you happen to know of a technique or a piece of code that will make this easy and efficient? Personally I think the most efficient way would be to use a $lang array in a separate PHP file (with the appropriate translations) then swap the file based on the users selection.
    One hint:

    If you're using multiple languages and you have translators preparing the entries, I suggest you try parse_ini_file() function instead of include() -- there is not much difference performancewise, but you'll have an easier job when inserting the translations: just give your translators the ini file with its simple syntax they will have much less chance to mess up.

  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 davedibiase
    I think the most efficient way would be to use a $lang array in a separate PHP file (with the appropriate translations) then swap the file based on the users selection.
    That's what I have been doing when I have needed i18n. It's probably the best/most effecient solution overall.

    Quote Originally Posted by davedibiase
    I noticed some people are moving their sites entirely to OO programming in PHP5. I'm assuming they are doing that because it helps organize code, but are they also doing that because it can make the site faster?
    When C++ came out there was a lot of polemics about the overhead of OO making the code run slower. While - in theory - an OO structure is overhead compared to a procedural one, reality differs. This is because OO helps you organize your code, and the organization translates to effeciency. A good advice is to never ever optimize your code for performance if it comes at the cost of organization.

    Quote Originally Posted by davedibiase
    I've heard of a practice called "releasing vital memory", a friend told me that you should always destroy/unset a block of data at the earliest time (...) This is ultimately done at the end of the scripts execution, but it could make a difference if you get rid of it earlier...right?
    I can't say for sure, but I'm quite sure that it doesn't really matter unless your scripts are very memory-consuming.

    Quote Originally Posted by davedibiase
    Sorry if my questions sound amateurish - I haven't learned all the nitty-gritty behind the Zend engine so while I'm coding I'm just wondering what the best practices are. My theory is even if I can reduce a scripts execution by a couple milliseconds in high-traffic it should make a big difference.
    If you haven't got it already, I can recommend you to install Xdebug which is a fantastic tool for profiling your code. It should help you identify the parts of your code that could be optimized.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    United Kingdom
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Generally speaking the Zend Engine is not particularly fast when it comes to OO, in fact, it's quite horribly slow (though you'd only really notice in benchmarks).

    However, with PHP5.1 comes three brand new executors, afaik these are basically different ways for the Zend Engine to process the opcodes for a PHP script. A short benchmark of this feature is here: http://www.sebastian-bergmann.de/blo....html#extended

    Aside from these new executors, the Zend Engine has been tweaked for much better performance with OO scripts. However, there are no numbers to back up exactly how much faster.

    I agree that structure is extremely important, but I believe that sometimes, it's alright to sacrifice a little structure for a performance enhancement, it all comes down to how much structure you sacrifice, and how much performance you gain.

    In short, OOP in PHP5 is looking increasingly promising (what with the various performance enhancement and the possibility of namespaces in the future). As a pardigm it's built towards structure, not speed, but as has been mentioned, better structure lends itself towards more efficient code.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I am planning to post a proof of concept of my own VM for PH next week on the internals list.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by davedibiase
    I've heard of a practice called "releasing vital memory", a friend told me that you should always destroy/unset a block of data at the earliest time, ie. if you aren't using the variable anywhere else on the script you can use unset() or mysql_free_result to release the memory allocation. This is ultimately done at the end of the scripts execution, but it could make a difference if you get rid of it earlier...right?
    If your variables are local (which they should be), it's not a problem, because they're cleaned automatically when the function completes.

  7. #7
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BerislavLopac: The ini file looks *so* useful, I wish I would have known about it earlier, I could have used it in another project. Actually I'm going to be doing a majority of the translations (French and Italian are on my list - too bad I can't speak German, ack!).

    kyberfabrikken: I think I'm just going to set a PHP file with the appropriate translations. Like you said on smaller less important scripts I think it would actually make the execution slow, because it's not worth unsetting variables, but on some large 500-1000 line scripts I'm working with, it might make all the difference. Xdebug looks great, I'm going to install it right after this post.

    Lazesharp: I think my goal here *is* performance. Since I'm the one maintaining the code structure won't be that important, I just want to stress the performance part.

    It's probably best if I describe my situation:

    All my vital site tasks are currently OO and stored in one central location, my /class folder. All my standard processes are kept in these classes ie. configuration, user authorization, messaging, form checking, website template etc.

    For certain things ie. template and authorization, I've realized that having the classes is useful. But for the more widely used things ie. configuration (which does database connection and stores a set of variables) I'm debating if I should keep it in OO.

    Also is the Zend Performance Suite really worth the money? Has anyone had any experience with it and should I spend my time looking into it? Like I said, I'm very worried that my server wont be able to handle the incoming traffic at points of the day, instead of spending big $$$ on upgrading server specs, maybe I could spend a lesser amount on speed software.

    No?
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  8. #8
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by davedibiase
    Also is the Zend Performance Suite really worth the money? Has anyone had any experience with it and should I spend my time looking into it? Like I said, I'm very worried that my server wont be able to handle the incoming traffic at points of the day, instead of spending big $$$ on upgrading server specs, maybe I could spend a lesser amount on speed software.
    http://eaccelerator.net/HomeUk might be worth a look...

  9. #9
    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 davedibiase
    Like you said on smaller less important scripts I think it would actually make the execution slow, because it's not worth unsetting variables, but on some large 500-1000 line scripts I'm working with, it might make all the difference.
    I didn't say that. I said that you might notice something for very memory-hungry scripts. That is - If you're dealing with large variables. (such as reading the contents of a file into a variable). In theese cases you'd better make sure that you keep no references to it, the moment your done with it. As stereofrog mentioned, you don't have to explicitly unset() them, as long as they are kept in a local scope. Eg. when you return from a function, all local variables are automagically unset.

    Quote Originally Posted by davedibiase
    I think my goal here *is* performance. Since I'm the one maintaining the code, structure won't be that important, I just want to stress the performance part.
    Now where have I heard that before ?

  10. #10
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol erm sorry no you didn't say that. What I meant to say that it wouldn't make a difference unless dealing with large amounts of data, then wanted to point out that maybe calling an extra function to unset it would take up more time, but what do I know :-p

    Yeah, I'm sure people come on Sitepoint screaming for better performance, I think the eaccelerator looks pretty good (and it's free). I'm going to try that when the time comes, for now I'm just going to keep doing what I'm doing.

    Thanks a ton :-)
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    United Kingdom
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Incidentally, calling unset() for every variable you're no longer using is _probably_ (though I can't be certain) more of a performance drain than leaving them set unless you're dealing with a huge amount of data. This would be due to the overhead of having to call a function.

  12. #12
    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 Lazesharp
    Incidentally, calling unset() for every variable you're no longer using is _probably_ (though I can't be certain) more of a performance drain than leaving them set unless you're dealing with a huge amount of data. This would be due to the overhead of having to call a function.
    Actually unset isn't a function - it's a language-construct, so I think it wouldn't matter. But the size of the php-script will increase, which might in return mean more work for the parser. I suppose ...

  13. #13
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lazesharp: Which was exactly the point I was trying to make in post #7 :-) you've confirmed my thoughts.

    EDIT: or as has been mentioned it's NOT a function - so it shouldn't matter really.

    Another question: when exactly does a class destroy itself? the last instance of it's use, or is it destroyed at the end of script execution with everything else?
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  14. #14
    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 davedibiase
    Another question: when exactly does a class destroy itself? the last instance of it's use, or is it destroyed at the end of script execution with everything else?
    Since you can't un-declare a class, I would think that it doesn't happen until php shuts down.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    United Kingdom
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Actually unset isn't a function - it's a language-construct, so I think it wouldn't matter. But the size of the php-script will increase, which might in return mean more work for the parser. I suppose ...
    While this is true, a) the parser will need some special logic to pick unset() out as a construct and not treat it like a function and b) the actual act of unallocating the memory hasn't been taken into account

    Another question: when exactly does a class destroy itself? the last instance of it's use, or is it destroyed at the end of script execution with everything else?
    I assume you mean object? In which case, in the same way that variables work. The references section of the manual does well to explain just how variables in PHP work and that is that every variable is actually a reference to some "variable content". This means that creating a reference to another variable actually merely copies the original variables reference.

    Now, if you apply this to objects you realise that all objects are references to some "content" (ie. the actual object) and as with variables, the content itself (in this case, the actual object) is automatically unallocated by the gc when there are no references to that content (since the only way you can refer to some existing "content" is by using an existing reference to it.

    It's a little complex to explain all this but if you think about it, it does make sense.

    In short, unsetting objects, references and variables all have the same effect, in that, the content itself will only be destroyed when no more references point to it. Regardless however, the entry in the symbol table for that reference/variable/object will be freed.

  16. #16
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So basically there is nothing I can really do but write neat, clean and efficient code :-p I can live with that *cough* lol
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  17. #17
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by davedibiase
    So basically there is nothing I can really do but write neat, clean and efficient code :-p I can live with that *cough* lol
    Yeah!

    Actually, if you can find a bottleneck, then you are at liberty to attack that problem anyway you can. As long as you hide the optimisations and ugliness behind some kind of facade, the damage to the rest of the application can be minimised. You might want to place it in a separate branch of your version control as well, as it's likely to be retuned evertime there is a change in your hardware/software/configuration.

    Before you resort to code smithing though, have a look at caching, code generation and precalculation first. Where do you think your bottlenexk lies?

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  18. #18
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm wondering what you think of this idea.

    The site I'm coding does a *lot* of image manipulation (ie. thumbnails mostly) so instead of producing the thumbnails on demand I'm creating a queue list that processes them as needed.

    So every so often if the server is too busy it places it into a MySQL then a cron task is used to process them every so often (in order).

    Until the thumbnail is created a different "waiting" image is used. Good idea or bad?
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  19. #19
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by davedibiase
    So basically there is nothing I can really do but write neat, clean and efficient code :-p I can live with that *cough* lol
    Remember Premature optimization is the root of all evil.

    To echo the thoughts of other posters:
    1) write clear, well organized, maintainable code
    1b) write tests before coding step1
    ... some time later
    2) use xdebug or other profilers to locate _actual_ bottlenecks (these often lurk where least expected)
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  20. #20
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you could also something like this if you got PHP powered by goofy

    PHP Code:
    public function restuffStuffINode $nodeElement ) {
         
    goofy {
               .
    fieldd $nodeElement
         
    }


  21. #21
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by davedibiase
    The site I'm coding does a *lot* of image manipulation (ie. thumbnails mostly) so instead of producing the thumbnails on demand I'm creating a queue list that processes them as needed.
    It's possible, but hidden cron jobs can be a pain for anyone who is maintaining the application. A bug in the process could cause a lot of confusion. Still, you are definitely on the right lines.

    Other options can include creating the thumbnail on demand and then caching it, or generating the thumbnail on the main image upload. The main thing is to do it just once.

    Which graphics library does the calculations? There may be a faster one available just for generating thumbnails.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  22. #22
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, well I will be the one maintaining it and if anyone else is touching it they come to me first :-p yeah bugs with cron jobs are a pain to find.

    Actually I'm creating an storing the thumb on my hd, and it wont happen many times for the same images - so catching wouldn't really make sense.

    I'm using imagick, I wrote a class to call the convert binary (which does the thumbs) using exec() but apparently there is a php extension I can use on top of it. I'm not sure which would be faster (I'm assuming my class since it's calling the binary directly instead of going through a PHP library then the software).

    I haven't looked up which software is the fastest but in my experience imagick has been very good (and the quality is excellent). Plus it has support for a HUGE list of file types - not that it's necessary lol.
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  23. #23
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Until the thumbnail is created a different "waiting" image is used. Good idea or bad?


    Depends. If you have paying subscribers then they may not be too keen to have to wait to see the thumbnail(s)? Just a consideration as I don't actually know what your site is about, bar it has a projected increase in visitors

  24. #24
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by davedibiase
    if I were to code my site entirely in OO classes, would this be a good thing?
    Yes. It takes time to learn though and more than one early design will be discarded along the way. I'd recommend that you forget about optimisation and put all your effort into learning about OOP and testing.

  25. #25
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would recommend both PoEAA (Patterns of Enterprise Application Architechture, Fowler) and also Domain Driven Design, Evans. Fowlers book on Refactoring is another suggestion though I don't own this book.

    Find out more here, www.martinfowler.com on the whole series of books by this author I would suggest that it would proberly be a good idea to learn unit testing along side object oriented programming, rather than wait until you've learnt object oriented programming, then to learn unit testing.

    That way, you'll learn quickly to test first before anything else which is important (in fact, the whole point of unit testing)


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
  •