SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ob_start and ob_end_flush and CPU or RAM

    Hi team,
    i have 2 short questions
    i am programming and very often using cookies to store temporary info so i used in all templates ob_start and ob_end_flush.
    first question: should i use them every templates or only in place when i redirecting or putting cookie?
    Second question if i use ob_start and ob_end_flush very often which of them will be loaded CPU or RAM, will be effected it on loading page speed?
    Thank in advance

    Said.

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,832
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Good question. I started using ob_start() several years ago in my scripts because it enabled me to output headers later in the script if necessary and allowed me to retrieve the HTML output into a variable so I could cache it to file or do string replacements. I assumed that it would use a little more memory, but I wasn't sure because at least with my code, PHP seems to flush the output to the browser all at once instead of in chunks.

    So I wrote a quick little script just now to do some testing and measured the memory allocated by PHP (but not necessarily used by the script) using the memory_get_peak_usage() function. I tested on a Windows localhost development server and tested using both true and false for the real_usage parameter. I saved a short text string concatenated with a counter variable into an array, echoing out the string on each loop. I had the loop run 10,000 iterations.

    In each case, using ob_start did increase the amount of memory allocated by PHP. The amount varied depending on what I did, with a low of approximately 20% more to 100% more. As I mentioned, this was on a Windows machine so the actual performance may vary on a Linux server.

    ob_start() is going to use more CPU as well. How much more? I can't say. With everything there are trade-offs. Using ob_start() gives the programmer more flexibility to do things as I mention in my first paragraph. With the flexibility comes a performance hit. The speed performance hit will be very tiny, we are talking thousandths of a second. If you are working on a very high traffic website or have extremely limited memory resources, using ob_start() may be something to be concerned about. Otherwise, I cannot see any reason to avoid using something that allows you so much more flexibility.

  3. #3
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cheesedude View Post
    Good question. I started using ob_start() several years ago in my scripts because it enabled me to output headers later in the script if necessary and allowed me to retrieve the HTML output into a variable so I could cache it to file or do string replacements. I assumed that it would use a little more memory, but I wasn't sure because at least with my code, PHP seems to flush the output to the browser all at once instead of in chunks.

    So I wrote a quick little script just now to do some testing and measured the memory allocated by PHP (but not necessarily used by the script) using the memory_get_peak_usage() function. I tested on a Windows localhost development server and tested using both true and false for the real_usage parameter. I saved a short text string concatenated with a counter variable into an array, echoing out the string on each loop. I had the loop run 10,000 iterations.

    In each case, using ob_start did increase the amount of memory allocated by PHP. The amount varied depending on what I did, with a low of approximately 20% more to 100% more. As I mentioned, this was on a Windows machine so the actual performance may vary on a Linux server.

    ob_start() is going to use more CPU as well. How much more? I can't say. With everything there are trade-offs. Using ob_start() gives the programmer more flexibility to do things as I mention in my first paragraph. With the flexibility comes a performance hit. The speed performance hit will be very tiny, we are talking thousandths of a second. If you are working on a very high traffic website or have extremely limited memory resources, using ob_start() may be something to be concerned about. Otherwise, I cannot see any reason to avoid using something that allows you so much more flexibility.
    as i understood this commands using more memory and CPU. for my website online 100-150 look like it is very big load. i started to use it just to prevent "header sent...... " when i wanted to assign cookies and redirects.
    so your advise is not to use every where this commands if i have a lot of people online (100-150 persons)?

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,832
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by SBABANLI View Post
    as i understood this commands using more memory and CPU. for my website online 100-150 look like it is very big load. i started to use it just to prevent "header sent...... " when i wanted to assign cookies and redirects.
    so your advise is not to use every where this commands if i have a lot of people online (100-150 persons)?
    Using ob_start will use more memory and a little more CPU. Whether or not to use ob_start is going to depend on you and your preferences and what kind of web hosting you can afford. Web hosting and memory are pretty cheap these days. But if you are on a limited budget and need to get as much out of your web hosting as you possibly can, then using ob_start is probably not a good idea because it will use more memory. How much capacity do you have on the server you are on? Can you afford the memory hit using ob_start?

    If your only basis for using ob_start is to prevent "header already sent" errors (including cookies), a better idea would be to re-examine your code so that almost all of the logic, database queries, and such are run before you start outputting HTML content. "Header already sent" errors occur when a header or cookie is sent after HTML is being output. Your code should determine the incoming request, load the PHP logic, run the queries (if there are any), then start outputting HTML last. Doing this will reduce your programming flexibility but you will not have any "header sent" errors.


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
  •