2

I am working on a reporting tool using PHPExcel 1.8.0. I need to constantly clone template sheets and fill them with data. The first sheets are generated very fast, but this process gets slower and slower with each sheet cloned. Here is the code i use for cloning sheets:

        $resultSheet = clone $sourceSheet; // instance of PHPExcel_Worksheet
        $resultSheet->setTitle($newSheetName);

        $sourceSheet->getParent()->addSheet($resultSheet,0);
        $sourceSheet->getParent()->setActiveSheetIndex($sourceSheet->getParent()->getIndex($resultSheet));
        return $resultSheet;

Measuring the execution time in seconds using microtime() for creating up to 24 clones off of one sheet (2 Samples for every clone) gives me this:

duplicateSheet (2 Samples) --- 0.046000003814697
duplicateSheet (2 Samples) --- Summarized difference: 0.046000003814697

duplicateSheet (4 Samples) --- 0.50999999046326
duplicateSheet (4 Samples) --- Summarized difference: 0.21099996566772

duplicateSheet (6 Samples) --- 0.69600009918213
duplicateSheet (6 Samples) --- Summarized difference: 0.39299988746643

...

duplicateSheet (46 Samples) --- 21.375
duplicateSheet (46 Samples) --- Summarized difference: 20.99299955368

duplicateSheet (48 Samples) --- 23.653000116348
duplicateSheet (48 Samples) --- Summarized difference: 23.266999483109

The summarized difference is the time spend only for cloning a sheet.

Is there a reason for this behavior ? How can i speed this up ?

Lucian Depold
  • 1,999
  • 2
  • 14
  • 25
  • Do you actually need to clone the worksheets? Can't you simply fill in your data in the loaded template and then just save the file as its new name? – Mark Baker Oct 06 '14 at 07:55
  • Hi Mark, i think i need to clone it because i need the template sheet to stay the same for later use: I am binding data to cellblocks of the template sheet. Those cellblocks are then duplicated in vertical direction inside the resultsheet dependent of the number of data-elements. The cellblocks are copied from the template sheet for this. – Lucian Depold Oct 06 '14 at 08:08

1 Answers1

2

I investigated the problem further and it seems that not the cloning itself causes everything to get slower but the setTitle() method of the worksheet (Lines 794-848) & the update of the formula references inside it (Line 843 & 844). I logged the time around this call:

        $logger = Zend_Registry::get('microtimeLogger');
        $logger->log("updateFormulaCellReferences");
    if ($updateFormulaCellReferences) {
        PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->_parent, $oldTitle, $newTitle);
    }
        $logger->log("updateFormulaCellReferences");

Result:

duplicateSheet (2 Samples) --- 0.041999816894531
duplicateSheet (2 Samples) --- Summarized difference: 0.041999816894531
updateFormulaCellReferences (24 Samples) --- 0.60699987411499
updateFormulaCellReferences (24 Samples) --- Summarized difference: 0.036999940872192

duplicateSheet (4 Samples) --- 0.48899984359741
duplicateSheet (4 Samples) --- Summarized difference: 0.19499969482422
updateFormulaCellReferences (26 Samples) --- 1.0539999008179
updateFormulaCellReferences (26 Samples) --- Summarized difference: 0.072999954223633

...

duplicateSheet (48 Samples) --- 23.306999921799
duplicateSheet (48 Samples) --- Summarized difference: 22.933000087738
updateFormulaCellReferences (70 Samples) --- 23.871999979019
updateFormulaCellReferences (70 Samples) --- Summarized difference: 18.763000011444

The setTitle has a flag to disable this update.

Disabling it saves 18 seconds and is a acceptable solution for me, because i do not use such references.

Lucian Depold
  • 1,999
  • 2
  • 14
  • 25