dmitryrusak has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, the question is about speed. There are two possibilites in my CGI script: 1) use a lots of prints of small chunks of HTML 2) create one huge string containing whole HTML page and print it only once at the end. What will be optimal? Best Regards, Dmitry
  • Comment on Many prints of small string or one print of huge string?

Replies are listed 'Best First'.
Re: Many prints of small string or one print of huge string?
by BrowserUk (Patriarch) on Dec 02, 2004 at 05:44 UTC

    Assuming that your script is going to be able to produce some output immediately, and then some more after some delay through DB access or similar?

    Depending upon the type of formatting of your HTML, some (most?) browsers will be able to start to render the page from the partial output from the early prints therebye giving the user a feeling that things are happening. This can be advantagous.

    If you save it all up and print it all in one go, it may result in a longer delay before the browser starts to render something, which may cause the perception of a slower site than they might otherwise have.

    This effect is particularly noticable when rendering large volumes of data in tables. If the rows are sent in separate chunks--and if the sizes of the cells are predefined--then most browsers will be able to render the table in chunks, rather having to wait for the whole table to arrive before being able to render anything.

    This is one of those cases were perceived performance is probably more important that absolute performance.


    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Thanks duff and BrowserUK, I think i will go for "print small chunks" sulution even if it will give me the better abs. performance, because in my case the data is really a huge table (which probably could be rendered partially).
Re: Many prints of small string or one print of huge string?
by duff (Parson) on Dec 02, 2004 at 05:18 UTC

    The best way to find out is to profile the code to see which satisfy your optimality criteria. Which you haven't specified BTW. The first may be slightly slower but may also use less memory. What are you trying to optimize?

    However, the first question that comes to my mind is "Why do you care?" Premature optimization is the root of all evil. The best thing to do is separate your program logic from your presentation logic, so you should really be using some sort of templating system. Most templating systems take care of handling the output for you so you don't have to worry about doing it piecemeal. :-)