PHPExcel 1.8.0 - Creating many sheets by cloning a template sheet gets slower with each clone

不想你离开。 提交于 2019-12-24 03:15:25

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/26211682/phpexcel-1-8-0-creating-many-sheets-by-cloning-a-template-sheet-gets-slower-wi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!