Document creation much slower under PHP 5.3 than 5.2

Topics: Developer Forum, User Forum
Jun 23, 2011 at 3:43 PM
Edited Jun 23, 2011 at 3:47 PM

Hi, 

We're currently migrating our project to more powerful hardware and also decided to use this opportunity to switch to PHP 5.3 from 5.2 we were using before.

However, in the process of testing we found out, that our script generating XLS documents are now executing several times slower, despite added hardware power. In the new setup we have two identical application servers, so we decided to downgrade one of them back to PHP 5.2 and compare script performance to check if it has anything to do with PHP version. Here is what we've found out.

Test program we used:

 

<?
    $objReader = PHPExcel_IOFactory::createReader('Excel5');
    $config = PaConfig::getInstance();
    $objPHPExcel = $objReader->load($config -> PATH_ROOT."templates/xls/Affiliates_overview_template_level3.xls");
    $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
        ->setLastModifiedBy("Maarten Balliauw")
        ->setTitle("Office 2007 XLSX  Document")
        ->setSubject("Office 2007 XLSX  Document")
        ->setDescription(" Document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("php result file");
        $i=0;
        $copy_sheet = $objPHPExcel->setActiveSheetIndex(1)->copy();
        for($i=0; $i<=200; $i++){
$start = PaDebug::getMicrotime();            
print_r("$i Start $start \n");             

$clone_start = PaDebug::getMicrotime();            
                $new = clone $copy_sheet;
$clone_stop = PaDebug::getMicrotime();            
print_r("Clone time ".($clone_stop - $clone_start)."\n");             
                
$sheet_add_start = PaDebug::getMicrotime();            
                $objPHPExcel->addSheet($new, $i);
$sheet_add_stop = PaDebug::getMicrotime();            
print_r("Add Sheet time ".($sheet_add_stop - $sheet_add_start)."\n");             
                
$title_start = PaDebug::getMicrotime();            
                $objPHPExcel->setActiveSheetIndex($i)->setTitle('aa'.$i);
$title_stop = PaDebug::getMicrotime();            
print_r("Title time ".($title_stop - $title_start)."\n");             
                
$sheet_start = PaDebug::getMicrotime();            
                for ($l = 'a'; $l<'z'; $l++ ){
                    for($j=1;$j<1000;$j++){
                        $objPHPExcel->setActiveSheetIndex($i)->setCellValue("$l$j","$l$j");
                    }
                }
$sheet_stop = PaDebug::getMicrotime();            
print_r("Sheet time ".($sheet_stop - $sheet_start)."\n");             
                
$stop = PaDebug::getMicrotime();            
echo "Memory ".memory_get_usage() . "\n\n" ;       
print_r("Total =>".($stop - $start)." \n\n");            
        }
        $objPHPExcel->removeSheetByIndex($i + 1);
    echo memory_get_usage() . "\n\n" ; 

?>

 

Here are results for 1st, 15th and 50th iteration.

PHP 5.3

0 Start 1308839210.6213
Clone time 0.002007007598877
Add Sheet time 2.0980834960938E-5
Title time 0.0013830661773682
Sheet time 1.5038568973541
Memory 54125384
Total =>1.5073959827423

...

15 Start 1308839282.4225
Clone time 0.0011379718780518
Add Sheet time 2.0980834960938E-5
Title time 9.4289009571075
Sheet time 1.9287619590759
Memory 688489944

Total =>11.358916044235

...

50 Start 1308840881.9705
Clone time 0.0011529922485352
Add Sheet time 4.0054321289062E-5
Title time 93.7625041008
Sheet time 2.9350831508636
Memory 2195938312

Total =>96.698873996735

 

PHP 5.2

0 Start 1308842497.0769
Clone time 0.0019261837005615
Add Sheet time 2.2172927856445E-5
Title time 0.0014350414276123
Sheet time 1.698254108429
Memory 52373176

Total =>1.701817035675

...

15 Start 1308842530.3258
Clone time 0.0011360645294189
Add Sheet time 1.5020370483398E-5
Title time 1.310131072998
Sheet time 1.5795361995697
Memory 663614720

Total =>2.8910360336304

...

50 Start 1308842683.5281
Clone time 0.0011169910430908
Add Sheet time 2.3841857910156E-5
Title time 4.3564808368683
Sheet time 1.5907838344574
Memory 2110293104

Total =>5.9486320018768

 

As you can see, memory usage and sheet manipulation times are close, but first operation on a newly created sheet under 5.3 slows down drasticaly the more sheets document contains. 

Could you please look into it and advice on what we could do to fix or avoid this problem. Perhaps there is other approach to copying sheets within document or some optimization tips we could use.

Thanks in advance