PHPExcel 1.8.0 - 通过克隆模板表创建多个工作表,每个克隆都会变慢

时间:2014-10-06 07:33:08

标签: php phpexcel

我正在使用PHPExcel 1.8.0编写报表工具。我需要不断克隆模板表并用数据填充它们。第一张纸的生成速度非常快,但每次克隆纸张时,此过程会变得越来越慢。这是我用于克隆表的代码:

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

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

使用microtime()以秒为单位测量执行时间,从一张纸上创建多达24个克隆(每个克隆2个样本)给我这个:

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

汇总的差异是仅用于克隆工作表的时间。

这种行为有原因吗?我怎样才能加快速度呢?

1 个答案:

答案 0 :(得分:2)

我进一步研究了这个问题,似乎克隆本身并没有导致一切变慢,但是工作表的setTitle()方法(第794-848行)&更新其中的公式参考(第843行和844行)。我记录了这个电话的时间:

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

结果:

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

setTitle有一个标志来禁用此更新。

禁用它会节省18秒,对我来说是一个可以接受的解决方案,因为我不使用此类参考。