Performance of ob_start()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sathamoth
Forum Newbie
Posts: 1
Joined: Sun May 25, 2003 3:04 am
Location: Helsinki

Performance of ob_start()

Post by Sathamoth »

Hi there,

I'm having some pretty weird issues with ob_start() function. I have a merely complex script, which parses XML file (its functionality is located in own class, which is included from another php file). Because this script can output pretty much data, I'm buffering the output and then send it to browser gzipped.

However, the execution time of my script is 23 seconds with large XML file, so I started to investigate possibilities of optimization. I thought - of course - that XML parsing was taking too long, but when I started to count seconds with getmicrotime() function, I found out that ob_start("ob_gzhandler") was taking 14 seconds of that 23! How that is possible, and where is the problem? All data is printed after ob_start() call, and at the end of the script ob_end_flush() is called. I have also tried using non-gzipped output, with pretty same processing time. The code which relates to this problem is below:

Code: Select all

$before = getmicrotime();
ob_start("ob_gzhandler");
$after = getmicrotime();

// Here comes some XML parsing and printing some large text data
// ...

print "Time usage for ob_start: " . ($after - $before) . " seconds\n";
ob_end_flush();

// This is standard getmicrotime from php.net
function getmicrotime()
{
        list($usec, $sec) = explode(" ",microtime());
        return ((float)$usec + (float)$sec);
}
Post Reply