PhpSpreadsheet VS Box\Spout讀取excel效能對比
阿新 • • 發佈:2018-11-27
phpspreadsheet版本:1.5.0
spout版本:2.7.3
在同樣的環境下,執行程式碼,spout的在記憶體使用和時間花費上都佔優,在phpspreadsheet讀取失敗的文件spout依然能正確完成讀取。
spout程式碼
<?php ini_set('memory_limit', '2G'); var_dump(memory_get_usage()); var_dump(microtime()); $loader = include 'vendor/autoload.php'; use Box\Spout\Common\Type; use Box\Spout\Reader\ReaderFactory; $file = '20.xlsx'; $type = pathinfo($file); $type = strtolower($type["extension"]); if ($type === 'xlsx') { $type = Type::XLSX; } else { $type = Type::CSV; } try{ $reader = ReaderFactory::create($type); $reader->open($file); $reader->setTempFolder('/dev/shm/'); $iterator = $reader->getSheetIterator(); $iterator->rewind(); $sheet1 = $iterator->current(); $rowIter = $sheet1->getRowIterator(); $data = array(); foreach ($rowIter as $rowIndex => $row) { $data[] = $row; } var_dump(count($data)); }catch (\Exception $e){ return []; } var_dump(microtime()); var_dump(memory_get_usage()); var_dump(memory_get_peak_usage());
phpspreadsheet程式碼
<?php ini_set('memory_limit', '2G'); var_dump(memory_get_usage()); var_dump(microtime()); $loader = include 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\IOFactory; $file = '20.xlsx'; try { $spreadsheet = IOFactory::load($file); $data = $spreadsheet->getActiveSheet()->toArray('', true, true); $spreadsheet->disconnectWorksheets(); unset($spreadsheet); var_dump(count($data)); } catch (Exception $e) { var_dump($e->getMessage()); } var_dump(microtime()); var_dump(memory_get_usage()); var_dump(memory_get_peak_usage());