SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 61
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Canada
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Clean code, nice repository, conditional includes ... speed?

    Hi there,

    As I'm growing, evolving, into paid PHP scripting I have become more and more aware of my coding style. I code cleaner now. I follow good organisational standards like grouping related functions in seperate files, using seperate files for related classes (reading up on it sticking to one class = one file seemed not the way to go speed-wise), etc.

    And guess what? Those that have gone before me know what they're doing. Code maintenance is much easier. Writing a new script is much 'easier' as well: grab this, include_once, occassional copy & paste - and we're off.

    Now performance, speed, of course has become an issue. Single quote <> double quote. if-else <> switch-case. Looked at a lot of benchmarks. Sometimes we're talking about "issues" of 0.04ms vs. 0.06ms.

    But when I look at the reports of the profiler in NuSphere PhpED I see that including an empty file takes as much as 2.5ms - and if it has to wait for the diisk it can go up as high as 100ms.

    So now I'm left feeling a bit puzzled - yes, unsure even. My loader which loads 4 seperate functions files: good idea or just wasting time? My classes in seperate files: good idea or just wasting time?

    On a sidenote: from your professional experience, in what range does a perfomance increase start to matter? Very simplified question, I know. But let's say for a site which you expect to be very popular, and going from the idea of no caching. In that case, would you start to write differently for a 0.02ms speed increase (which multiplied times x instances in your script times x executions could add up) or....?

    Ruud

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Stillwater, MN
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Giving the full paths when including helps speed a bit.

  3. #3
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hubble Space Telescope territory, I think.

  4. #4
    SitePoint Zealot David C's Avatar
    Join Date
    Nov 2003
    Location
    New York!
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to try including one file with a large number of functions and classes (and no comments or unnecessary whitespace!) and to benchmark that against including a number of smaller files -- I bet it will prove faster. You can and should still develop using the file structure you're currently using.

  5. #5
    ********* 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 Ruud
    On a sidenote: from your professional experience, in what range does a perfomance increase start to matter?
    Short answer: mostly it never does.

    Real answer...

    It's an ROI calculation and a tricky one. In the alternative scenarios you have two outputs: response time and server load.

    Response time will affect customer take up of your service which is a difficult calculation. Fortunately it is a fairly easy thing to measure if you just want to get below a certain threshold. Say you run a user test and they say that it "feels slow". From that comment I would start setting goals for improving customer percieved work rates. The trouble is that inexperienced coders will start twiddling with their PHP scripts without looking at the whole problem.

    The first solution involves making them more productive. Can you reengineer the interface so that they can get more done faster? Can you send them related information so that they have to make fewer requests? Done that? Let's move on...

    A request from Europe to Japan will typicaly take 1-2 seconds before the HTTP header starts rolling in. This is partly the DNS lookups and the sheer distance an uncached page has to travel. If the page content is 50K then that will add another second even from a blindingly fast server into a broadband client, but this part is not relevant. The browser will be unable to paint the page anyway until the CSS comes in, which is likely in an external file and so will require another request (to the same server). Of course framed pages require multiple requests. No PHP issues yet.

    Unless your site is staggeringly popular neither your DNS record or your page will be cached. You can improve your DNS performance by using specialist companies, such as DynDNS, that have DNS servers around the globe. You can improve the fetch time by having your servers local to the market and by making sure your navigation pages at least can be cached by proxies (don't add session IDs to the URLs for example). No PHP issues yet.

    After that, your job is to get that first 2K of page out as quckly as possible. Make sure that long queries execute after the page header is sent. Mozilla wont even start painting the page until one second has elapsed to avoid the page jumping around whilst rendering boxes are assembled. Typically you will have longer whilst the styles come in - 3 seconds or more. As you only have to get the page header out, consider doing all of your PHP includes once the navigation is complete. You may have complex validation to do before the next page is clear, but even so you could send the invisible part of the page (the "head" tag) as long as you have the title available. Getting the first 2K out is a template engine issue so no PHP specific issues yet except for where to put the includes..

    Waiting for the stylesheet to load will trigger another request. This will be cached later (unless you are doing fancy bot blocking), but first impressions mean a lot so consider having styles on your landing pages in-line. Not a PHP issue of course.

    If you have a lot of data to come you definitely want to stream the page, especially if you have a lot of images. Browsers will do their best to paint the visible part of the page first. Most of this optimisation is achieved by only pulling rows from the database as you actually need them. Disc access time dwarfs processor speed and network access time dwarfs disc access. Optimise the queries if they take longer than a half second and stream the results. Not a PHP issue of course.

    The rest of the page could take minutes for all the user cares. Feel free to save a few miliseconds here just to have the warm comfy feeling that you will be the only person who ever knows about it.

    By comparison server load is a simple ROI calculation. Firstly calculate the cost of development time. It is usually about $1-2000 dollars a week on average. Assume a forty hour week. Get a clock and time how long it takes to group code together for optimisation. Round up to the nearest half day (it's distracting). Double it to allow time for the next coder to understand what you have done.

    Now measure the speed gain for the entire server request. It is important to measure this using the server benchmark tool, not within PHP, because of the time it takes to fire up the PHP/HTTP process itself (hint: the final percentage will be quite low). Go to your server company catalog and find out how much the extra hardware would cost to handle this with the unoptimised code. Consider software and hardware caching solutions as well, such as Zend Accelerator. How often is each section of the code refactored? This is your code churn rate. If it is 3 months (say) then that is how often you will get hit for the development cost and so you can compare this with rental prices of your hardware.

    I would give a sample calculation at this point, but it is more fun if you do the calculation yourself.

    Finally, factor in the developer costof doing all of these measurements .

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

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the alternative scenarios you have two outputs: response time and server load.
    Still to read what else Marcus has to say, but briefly Martin Fowler discusses this point above in his book, if it's of any interest ?

    On another matter of comments and whitespace, you can put a PHP file through an application which strips these out, to reduce file size ?

    Once you've done this, it's these files you upload to your server, keeping the original locally for you to work on later

    Btw, if you find such an application, post a link to this forum, as I'm still searching

  7. #7
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker
    On another matter of comments and whitespace, you can put a PHP file through an application which strips these out, to reduce file size ?

    Btw, if you find such an application, post a link to this forum, as I'm still searching
    FYI the function php_strip_whitespace is available in versions upwards of PHP5b3 (there is currently no manual entry for the function on php.net).
    Always open to question or ridicule

  8. #8
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sure at one time there was some script posted to remove whitespace and comments as well, though for the life of me I cannot remember the thread

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Chicago
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use a directory for developing, and for the final release have a compile function that takes everything, eliminates whitespace, and makes it into one big file? Then you edit the dev tree, and recompile all your classes + functions into one file - its not the neatest way of doing it, but its by far the fastest.

  10. #10
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Canada
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Widowmaker: maybe you mean the compiler class by mr. jeep?
    [quote="mr jeep"]a class which will merge all files into one, removing comments, unwanted spaces and unwanted carriage-return. It it very usefull if you always include/require a lot of files to increase speed.[/url]

    The way I read it from pure code-view is that combining files and stripping out comments/whitespace is probably the only speed issue that really matters.

    @Marcus - wow... that is excellent information written down in an excellent way. Makes me feel a wwwwhole lot better also! Thank you!

    Ruud

  11. #11
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very interesting stuff, Marcus. I don't claim to grasp all of this, so let's see if I can ask some intelligent questions.
    Quote Originally Posted by lastcraft
    The trouble is that inexperienced coders will start twiddling with their PHP scripts without looking at the whole problem.
    Yes, exactly. Even Schlossnagle, who has an excellent treatment of optimization in his book, seems to focus exclusively on server load and forget about response time.
    Quote Originally Posted by lastcraft
    A request from Europe to Japan will typicaly take 1-2 seconds before the HTTP header starts rolling in. This is partly the DNS lookups and the sheer distance an uncached page has to travel. If the page content is 50K then that will add another second even from a blindingly fast server into a broadband client, but this part is not relevant.
    I don't get this. Why is it not relevant? Perhaps if it's one second on a fast connection, but if it's 10 or 20 seconds, how can it not be relevant?
    Quote Originally Posted by lastcraft
    Unless your site is staggeringly popular neither your DNS record or your page will be cached.
    But for the benefit of the next access to the same page you can send caching headers to stimulate the browser to keep the page until next time. At least that's how I understand it.
    Quote Originally Posted by lastcraft
    After that, your job is to get that first 2K of page out as quckly as possible.
    You're making some assumptions about the usefulness of those 2K, I guess?

    Let me check. I wonder what's on the fist 2K of this page? I find this:

    Empowering Web Developers since 1997

    * Articles
    * Books
    * Blogs
    * Forums

    Hmmm...not very satisfying. What am I missing?
    Quote Originally Posted by lastcraft
    If you have a lot of data to come you definitely want to stream the page, especially if you have a lot of images. Browsers will do their best to paint the visible part of the page first. Most of this optimisation is achieved by only pulling rows from the database as you actually need them. Disc access time dwarfs processor speed and network access time dwarfs disc access. Optimise the queries if they take longer than a half second and stream the results. Not a PHP issue of course.

    The rest of the page could take minutes for all the user cares. Feel free to save a few miliseconds here just to have the warm comfy feeling that you will be the only person who ever knows about it.
    If I've understood correctly, this is where there may be a conflict between response time and server load considerations. Schlossnagle recommends using a reverse proxy/HTTP accelerator to deal with network latency. The reverse proxy gets the whole response from the real Web server before the user gets anything. In your hypothetical case, that would leave the user twiddling her thumbs for minutes.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  12. #12
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I remember Mr Jeep's script, though I was pondering on something else that has been posted, though since I cannot find it, I might look again at Mr Jeep's script, just to strip out the part(s) which handle the whitespace and comments

    Then re-post for public consumption later

    Why is it not relevant?
    Not sure myself in what context this is to be taken, though I think it's not relevant due to the distance between the server and client, is what Marcus is trying to get at ?

    ie There wouldn't be too much different between 5k and 50k - due to the distance ?? Not sure though, maybe Marcus can clear this point up

  13. #13
    ********* 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 dagfinn
    so let's see if I can ask some intelligent questions.
    Cool bananas. You always seem to involve me in deeper discussions .

    Quote Originally Posted by dagfinn
    Yes, exactly. Even Schlossnagle, who has an excellent treatment of optimization in his book, seems to focus exclusively on server load and forget about response time.
    I have only just got this book, but he does seem to take a systems view. It does seem one of the better PHP books out there though.

    Quote Originally Posted by dagfinn
    I don't get this. Why is it not relevant? Perhaps if it's one second on a fast connection, but if it's 10 or 20 seconds, how can it not be relevant?
    Ok, I didn't make this clear. Because whilst that content is coming in the browser will be fetching the CSS page and will be in it's "I am not going to start rendering until I have a good chunk of information" wait state. You basically have a couple of seconds for free where it doesn't matter what you do.

    Quote Originally Posted by dagfinn
    But for the benefit of the next access to the same page you can send caching headers to stimulate the browser to keep the page until next time. At least that's how I understand it.
    Absolutely. Once you have cached the page, all bets are off. If you avoid session state in the URL then ISP/in-house proxies can cache the page for the next person as well.

    Quote Originally Posted by dagfinn
    You're making some assumptions about the usefulness of those 2K, I guess?
    Yes absolutely, I kinda picked 2K out of the air. I guess you have these milestones in page loading...
    1) Header block with CSS file
    2) First visible content (so the user knows something has happened)
    3) Navigation (in case they are just passing through anyway)
    4) Useful content
    5) The rest of the content

    Quote Originally Posted by dagfinn
    Hmmm...not very satisfying. What am I missing?
    Ok, each site is different .

    Quote Originally Posted by dagfinn
    If I've understood correctly, this is where there may be a conflict between response time and server load considerations. Schlossnagle recommends using a reverse proxy/HTTP accelerator to deal with network latency. The reverse proxy gets the whole response from the real Web server before the user gets anything. In your hypothetical case, that would leave the user twiddling her thumbs for minutes.
    Possibly (I hadn't thought of that). The problem is that your first reports that you get are usually to do with server performance, whereas you have to actively seek out information on (percieved) response times. Now from the business owners point of view which would worry you most; "We can save 5% server cost" or "Our users think our system is slow". To me the second one raises alarm bells big time. Out goes the reverse proxy at the drop of a hat, no contest.

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

  14. #14
    SitePoint Zealot sike's Avatar
    Join Date
    Oct 2002
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker
    Yes, I remember Mr Jeep's script, though I was pondering on something else that has been posted, though since I cannot find it, I might look again at Mr Jeep's script, just to strip out the part(s) which handle the whitespace and comments
    a while back i posted a apache ant buildfile which removed comment blocks and stuff from sources. it was a php build system (kind of ) where you have a development tree and a release tree. not sure if you meant this thread.

    Sike

  15. #15
    SitePoint Zealot
    Join Date
    Feb 2003
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker
    On another matter of comments and whitespace, you can put a PHP file through an application which strips these out, to reduce file size ?

    Btw, if you find such an application, post a link to this forum, as I'm still searching
    http://pobs.mywalhalla.net/
    It's an obfuscator, so it might do a bit more than what you want.
    The last it is has been updated is August 10th 2003. Judging from the guestbook it still works for some people, but there seem to be problems for some people also.

    Maybe give it a try.

  16. #16
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    with my kids
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what' wrong with...
    php
    ...
    -w Display source with stripped comments and whitespace.
    ....

  17. #17
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've only just got the book myself as well on Sat., yet to really get stuck into it though,

    It does seem one of the better PHP books out there though.


    It's geared towards PHPversion5, though that's fine as it gives me plenty time to read it through, and time to think. Though I get the (brief) feeling, for example Smarty Templates, that the subject(s) are not really covered thoroughly enough ?

    Smarty for example, is only touched on, and considering it's meant to be advanced programming, to me I was disappointed

  18. #18
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks folks for the suggestions and links on the scripts for removing comments and whitespace

    Will be looking tomorrow, Sike - do you have a link so I can have a look at your thread ?

  19. #19
    SitePoint Zealot sike's Avatar
    Join Date
    Oct 2002
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker
    Thanks folks for the suggestions and links on the scripts for removing comments and whitespace

    Will be looking tomorrow, Sike - do you have a link so I can have a look at your thread ?
    sure

    http://www.sitepoint.com/forums/show...&highlight=ant

    Sike

  20. #20
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Montreal, Quebec, Canada
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should make a timer class to benchmark how long all the includes take to load. Also, I suggest using things such as require_once(). That means that a file will only be included once, and could help you out.

  21. #21
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Canada
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess that even though we can utilise the way the web works and use it for us, ensuring the user has the feel of smooth sailing, that in the end pure script speed does matter.

    I'm not working for large scale sites - but I suspect that if you work for a site the size and popularity of, say, Amazon or Yahoo you do say that 10 thousand requests on this script with a time saving of 0.02ms each adds up. Maybe not enough to get a team all joyful saying "let's rewrite everything!" but maybe enough to get you the job instead of the next person? If they want to incorporate a new feature and I can deliver it 0.02ms faster per request - that has to count for something?
    And if you don't work for a large site you still may be churning out code which will go on overworked servers such as the ones at iPowerw... ah, let's not name names :-) On those kind of shared hosting accounts the fact that your script runs just a tad bit faster should give you a bit of an advantage?

    On that note - OOP in PHP is said to be a tad slower then using function. Doesn't it follow then from the above that using OOP in PHP while attempting to maintain the highest speed and the lowest server load is a bit having a good First Aid practice in order to be able to shoot yourself in the foot?

    Ruud

  22. #22
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's say we have two almost identical apps: one written by Mr Spaghetti and one carefully refactored into lean & mean classes by Mr Oop. Both work, but Mr Spaghetti is very proud of a 0.02s speed advantage. He constantly annoys Mr Oop (who has better things to do) with the latest sightings on the edge of reality from his Hubble telescope.

    One day the client wants to make a big change to the site. The update is carried out, both sites are still using the "same" underlying code, and both have slowed right down. Mr Oop starts examining the system to find the bottleneck. Within an afternoon, with his nice modular design, he has identified the problem and swapped out the problem code. Mr Spaghetti spends all day just trying to figure out his own script - he's long forgotten how it works and the mess on the page doesn't explain itself. He spends all week looking for bottlenecks: because his script is so tangled, it's a huge effort to compare alternative ideas.

    The moral of the story: php is very fast. It's actually very difficult to write a "slow" script. Programming is much more about writing literate code which a person can read and work with easily than it is about making computers do stuff.

  23. #23
    SitePoint Member dgross's Avatar
    Join Date
    Jun 2004
    Location
    Michigan
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ruud
    On a sidenote: from your professional experience, in what range does a perfomance increase start to matter? Very simplified question, I know. But let's say for a site which you expect to be very popular, and going from the idea of no caching. In that case, would you start to write differently for a 0.02ms speed increase (which multiplied times x instances in your script times x executions could add up) or....?
    When does a performance increase start to matter? From my experience, there's two situations:

    1) The obvious: Your clients notice the slowness in certain areas. In this case, finding bottlenecks and optimizing for speed will only help retain those clients, cause face it, people have better things to do than waiting for a page to load.

    2) This is a bit more subtle, but if you've always thought about speed, and wrote code that is fast (avoiding pre-mature optimization of course), this does make a big difference. At my last job, our clients praised us because our site was always so much faster than our competitors. It wasn't due to some big speedup, it was just always the way it was designed.

    However, I guess you're asking about small amounts of milliseconds. All I can say is don't spend all your time optimizing if there's no need for it from a client perspective, or they won't really notice (i.e. don't optimize just because it's fun). There's probably better ways to spend your time. It's always a tradeoff, since all your optimizations will add up in time, but they also cost developer time.

    Not sure if this really helped...

    Sincerely,
    Dave Gross

  24. #24
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Canada
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dave, nice reply!

    McGruff - are the only options available OOP vs. spaghetti code?

    Ruud

  25. #25
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    very well said Marcus.

    I have asked a similiar question just the other day. I didnt' get me answers compared to this one,

    but it does fit in my question.

    say you did a scrpt that was pretty well coded, well as a matter as fact even if it wasn't coded properly, what would make a server have a high cpu usage? what would cause both cpu's to go to 100% on httpd ? I know this doesn't give very much info but my thread is in the php forum if you care to look.
    success is not by chance, it is by choice.


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
  •