美文网首页我爱编程
PHPExcel与PhpSpreadsheet内存占用对比及使用

PHPExcel与PhpSpreadsheet内存占用对比及使用

作者: 许文同学 | 来源:发表于2018-04-04 16:53 被阅读0次

话不多说先上结果对比图(数据怎么来的往下拉看代码)


image.png

注:

  • 数据有7列(1列为空,1列约25中文,其他列简单int),生成xlsx文件大小 2,699K
  • PhpSpreadsheet使用Memcached和Redis性能表现差不多。
  • 【save输出】的峰值占用计算是在【save输出】完成时内存峰值大于【save输出】前内存峰值的情况下:
    操作占用峰值 = 操作完成后峰值 - 操作前内存用量
    未突破之前峰值时不计算
  • PHPmemory_limit限制的是emalloc()分配的内存,即memory_get_peak_usage()不传true获取到的峰值内存用量(黄)。
  • 之所以要记录多次峰值内存是具体步骤的高占用内存可能在执行完成时被回收了。
  • PhpSpreadsheet缓存使用参阅:http://phpspreadsheet.readthedocs.io/en/develop/topics/memory_saving/

结论

  • 通过测试得出的各区间的峰值内存用量来看,save()保存输出的过程中是最消耗内存的。
  • EXCEL文档是基于xml的zip打包格式,改后缀为zip有惊喜,并非直接文本文件,每次修改都需要整体读入再覆写。数据行列过多建议使用文本拼接生成csv文件。
  • 几乎可以放弃使用外部缓存来解决内存溢出的想法
    下图为单次10W*7列使用Redis的操作记录,有420W次的Redis操作,Redis服务器在操作期内网络流入1G、流出1.3G,可见开销非常之高。然而效果却并不如人意。


    image.png

以下测试代码的示例

<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;

$str = 'startTime(s):'.time()."\r\n";
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root');
$str .= '╔new PDO:'.(memory_get_usage()/1024/1024)."MB\r\n";
$str .= '╚time(s):'.time()."\r\n";
$res = $pdo->query('SELECT * from excel_test LIMIT 100000');
$res = $res->fetchAll(PDO::FETCH_ASSOC);
$str .= '╔结果数组:'.(memory_get_usage()/1024/1024)."MB\r\n";
$str .= '╠此前峰值内存:'.(memory_get_peak_usage()/1024/1024)."MB\r\n";
$str .= '╚time(s):'.time()."\r\n";

$spreadsheet = new Spreadsheet();
$str .= '╔new SS:'.(memory_get_usage()/1024/1024)."MB\r\n";
$str .= '╠此前峰值内存:'.(memory_get_peak_usage()/1024/1024)."MB\r\n";
$str .= '╚↑time(s):'.time()."\r\n";
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setTitle('test');
$azs = range('A','Z');
$titles = array_keys($res[0]);
foreach ($titles as $k => $title) {
    $spreadsheet->getActiveSheet()->setCellValue(($azs[$k]).'1', $title);
}
foreach ($res as $i => $row){
    $m = 0;
    foreach ($row as $val) {
        $spreadsheet->getActiveSheet()->setCellValue(($azs[$m]).($i+2), $val);
        ++$m;
    }
}
$str .= '╔单元格完成:'.(memory_get_usage()/1024/1024)."MB\r\n";
$str .= '╠此前峰值内存:'.(memory_get_peak_usage()/1024/1024)."MB\r\n";
$str .= '╚↑time(s):'.time()."\r\n";
unset($res);
$str .= '╔unset数组:'.(memory_get_usage()/1024/1024)."MB\r\n";
$str .= '╚time(s):'.time()."\r\n";
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setPreCalculateFormulas(false);
$str .= '╔createWriter:'.(memory_get_usage()/1024/1024)."MB\r\n";
$str .= '╠此前峰值内存:'.(memory_get_peak_usage()/1024/1024)."MB\r\n";
$str .= '╚time(s):'.time()."\r\n";

$writer->save('./wmtest.xlsx');
$str .= '╔over:'.(memory_get_usage()/1024/1024)."MB\r\n";
$str .= '╚time(s):'.time()."\r\n";
$str .= '脚本内存峰值:'.(memory_get_peak_usage()/1024/1024)."MB\r\n";
$str .= '物理峰值:'.(memory_get_peak_usage(true)/1024/1024)."MB\r\n";
file_put_contents("./wmtest.txt",$str);

PHPExcel 测试结果

startTime(s):1522826206
╔结果数组:54.641174316406MB
╠此前峰值内存:104.72563934326MB
╚time(s):1522826207
╔单元格完成:198.46007537842MB
╠此前峰值内存:198.46117401123MB
╚↑time(s):1522826216
╔unset数组:144.33098602295MB
╚time(s):1522826216
╔createWriter:145.26038360596MB
╠此前峰值内存:198.46117401123MB
╚time(s):1522826216
╔over:164.63647460938MB
╚time(s):1522826272
脚本内存峰值:413.024269104MB
物理峰值:500MB

PhpSpreadsheet 测试结果

startTime(s):1522825234
╔new PDO:0.40194702148438MB
╚time(s):1522825234
╔结果数组:54.532043457031MB
╠此前峰值内存:104.61650848389MB
╚time(s):1522825234
╔new SS:56.107322692871MB
╠此前峰值内存:104.61650848389MB
╚↑time(s):1522825234
╔单元格完成:269.50070953369MB
╠此前峰值内存:269.5018081665MB
╚↑time(s):1522825244
╔unset数组:215.37158966064MB
╚time(s):1522825245
╔createWriter:216.2133102417MB
╠此前峰值内存:269.5018081665MB
╚time(s):1522825245
╔over:235.63749694824MB
╚time(s):1522825306
脚本内存峰值:483.96257019043MB
物理峰值:512MB

PhpSpreadsheet 使用Redis缓存测试结果

╔new PDO:0.52817535400391MB
╚time(s):1522826626
╔结果数组:54.658271789551MB
╠此前峰值内存:104.74273681641MB
╚time(s):1522826626
╔new SS:56.377334594727MB
╠此前峰值内存:104.74273681641MB
╚↑time(s):1522826626
╔单元格完成:100.63970184326MB
╠此前峰值内存:110.61833953857MB
╚↑time(s):1522826912
╔unset数组:46.510581970215MB
╚time(s):1522826912
╔createWriter:47.347480773926MB
╠此前峰值内存:110.61833953857MB
╚time(s):1522826912
╔over:67.271064758301MB
╚time(s):1522828298
脚本内存峰值:315.16254425049MB
物理峰值:410MB

相关文章

  • PHPExcel与PhpSpreadsheet内存占用对比及使用

    话不多说先上结果对比图(数据怎么来的往下拉看代码) 注: 数据有7列(1列为空,1列约25中文,其他列简单int)...

  • 关于PHP文件Excel导入导出操作

    PHPExcel使用 PHPExcel 中文使用手册详解--转载 PhpSpreadsheet使用 phpspre...

  • php-xlswriter 使用

    导大表excel时使用PhpSpreadsheet会占用大量内存,而使用php-xlswriter 可以降低内存使...

  • 2022-03-12 excel导入导出1

    PHPExcel已经不再维护所以php中excel导入导出,建议使用PhpSpreadsheet。 1. 安装 使...

  • PhpOffice/PhpSpreadsheet读取和写入Exc

    PhpSpreadsheet是一个纯PHP编写的组件库,它使用现代PHP写法,代码质量和性能比PHPExcel高不...

  • 原生PHP导出excel文件

    一直以为PHP导出excel需要用PhpSpreadsheet或者PHPExcel这类插件,但其实原生PHP也可以...

  • Android创建多进程

    为何要使用多进程 1.分散内存的占用我们知道Android系统对每个应用进程的内存占用是有限制的,而且占用内存越大...

  • PHPexcel溢出

    使用PHPExcel导出文件,会出现由于文件体积问题导致PHP内存溢出,需要对PHPExcel的参数优化。文件过大...

  • Android 性能优化

    一. 内存优化 Android 垃圾回收与内存优化 不要过度使用枚举,枚举占用的内存空间要比整型大。 适度使用静态...

  • Linux面试常考命令总结

    1. linux查看内存,cpu占用命令? 使用top,查看cpu占用情况: 使用free,查看内存占用情况: 使...

网友评论

    本文标题:PHPExcel与PhpSpreadsheet内存占用对比及使用

    本文链接:https://www.haomeiwen.com/subject/gcwkhftx.html