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 ?