php 每次迭代后脚本花费的时间越来越多
php Script takes more and more time after each iteration
我正在使用 PHPExcel 创建 excel 文件,使用 php。首先看下面的代码,这段代码的问题是,在将一些数据保存到 excel 之后,它只保留在进程中,但不保存任何东西。我认为脚本会保存任何数据以捕获和临时值,这会导致脚本在每次迭代后加载越来越多。
解释:
首先,这段代码从一个 excel 文件中逐一检索一些整数值。 sample.xls(此文件仅包含 A 列中的值。)。假设它从单元格 A1 中检索到的第一个值是 1212,则代码设置 $target=1212,在 curl 函数检索 1212 的数据并在结果文件夹中另存为 html 为 1212.html 之后。在 dom 图书馆开始他们的工作之后。 1212.html 文件包含 table 三列多行。所以 dom 获取 td 和 tr 的数据并将各自的值保存在 excel 单元格中,最后将数据保存到 excel 结果文件夹中作为 1212.xlsx,再次发生相同的过程对于 sample.xls 中的单元格 A2,检索一些值,如 1213,并开始抓取等等。
问题:
在这里,第一个值(如 1212)花费的时间很少,然后第二个值 1213 花费的时间多一点,再多一点,在四个或五个值之后,执行时间很长(很多分钟),请帮忙我这次减少了,并使这个过程更快。谢谢
代码:
<?php
......
ini_set('include_path', ini_get('include_path').';../Classes/');
include_once 'PHPExcel.php';
include_once 'Excel2007.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->....//set some properties//
$excel->read('sample.xls'); // added excel reader from which we need to take some values
$x=1;
while($x<=$excel->sheets[0]['numRows']) { // reading row by row
$y=1;
while($y<=$excel->sheets[0]['numCols']) {// reading column by column
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$target = $cell;
// $objWorksheet = $objPHPExcel->getActiveSheet();
// $highestRow = $objWorksheet->getHighestRow();
// for($row=1; $row < $highestRow; ++$row){
// $objPHPExcel->getActiveSheet()->removeRow($row,$row);
// }
/* some lines of code using curl to fetch data for $target value
........... */
//below is the code which retrives data from html table and saves into excel file.
$url='results/'.$target.'.html';
include_once('dom.php');
$html=file_get_html($url);
$record_find='first';
foreach($html->find('table#GridView1') as $e){
if($record_find=='first')
$i=1;
$j=0;
foreach($e->find('tr') as $e1){
$distno=trim($e1->find('td', 0)->innertext);
$acno=trim($e1->find('td', 1)->innertext);
$partno=trim($e1->find('td', 2)->innertext);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$j, $distno);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$j, $acno);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$j, $partno);
$j++;
}
}
$objPHPExcel->getActiveSheet()->setTitle($target);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('excelresult/'.$target.'.xlsx');
$y++;
}
$x++;
}
?>
卷曲:
$debug = 1;
$url = "url";
$f = fopen('log.txt', 'w');
$cookies = 'cookies.txt';
touch($cookies);
$useragent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/36.0.1985.125 Chrome/36.0.1985.125 Safari/537.36';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$html = curl_exec($ch);
curl_close($ch);
preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);
$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 985000);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
// Collecting all POST fields
$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__LASTFOCUS'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['cns_fer'] = 2;
$postfields['xttPd'] = $target;
$postfields['tsfDes'] = "Search";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch);
curl_close($ch);
file_put_contents('results/'.$target.'.html', $ret);
类似于:
for($row=1; $row < $highestRow; ++$row){
$objPHPExcel->getActiveSheet()->removeRow($row,$row);
}
每次迭代都会花费很多时间,而且大部分时间都是多余的....
如果您需要从现有工作表中删除所有数据,请使用
$objPHPExcel->getActiveSheet()->removeRow(1,$highestRow);
相反,无需循环(尤其是虚假循环删除已经多次删除的内容),一次调用即可删除所有内容
我也很困惑为什么您使用一个库来读取文件 (excel Reader) 而另一个库用于写入 (PHPExcel) 而您可以使用一个库 (PHPExcel)这两个目的....虽然你似乎对 excel Reader 做的很少,因为你正在遍历该电子表格中的每个单元格,然后似乎根本没有对它做任何事情
编辑
我最后一条评论的意思是这样的:
<?php
......
ini_set('include_path', ini_get('include_path').';../Classes/');
include_once 'PHPExcel.php';
include_once 'Excel2007.php';
$excel->read('sample.xls'); // added excel reader from which we need to take some values
$x=1;
while($x<=$excel->sheets[0]['numRows']) { // reading row by row
$y=1;
while($y<=$excel->sheets[0]['numCols']) {// reading column by column
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$target = $cell;
/* some lines of code using curl to fetch data for $target value
........... */
//below is the code which retrives data from html table and saves into excel file.
$url='results/'.$target.'.html';
include_once('dom.php');
$html=file_get_html($url);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->....//set some properties//
$record_find='first';
foreach($html->find('table#GridView1') as $e){
if($record_find=='first')
$i=1;
$j=0;
foreach($e->find('tr') as $e1){
$distno=trim($e1->find('td', 0)->innertext);
$acno=trim($e1->find('td', 1)->innertext);
$partno=trim($e1->find('td', 2)->innertext);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$j, $distno);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$j, $acno);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$j, $partno);
$j++;
}
}
$objPHPExcel->getActiveSheet()->setTitle($target);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('excelresult/'.$target.'.xlsx');
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
$y++;
}
$x++;
}
?>
我正在使用 PHPExcel 创建 excel 文件,使用 php。首先看下面的代码,这段代码的问题是,在将一些数据保存到 excel 之后,它只保留在进程中,但不保存任何东西。我认为脚本会保存任何数据以捕获和临时值,这会导致脚本在每次迭代后加载越来越多。 解释:
首先,这段代码从一个 excel 文件中逐一检索一些整数值。 sample.xls(此文件仅包含 A 列中的值。)。假设它从单元格 A1 中检索到的第一个值是 1212,则代码设置 $target=1212,在 curl 函数检索 1212 的数据并在结果文件夹中另存为 html 为 1212.html 之后。在 dom 图书馆开始他们的工作之后。 1212.html 文件包含 table 三列多行。所以 dom 获取 td 和 tr 的数据并将各自的值保存在 excel 单元格中,最后将数据保存到 excel 结果文件夹中作为 1212.xlsx,再次发生相同的过程对于 sample.xls 中的单元格 A2,检索一些值,如 1213,并开始抓取等等。
问题:
在这里,第一个值(如 1212)花费的时间很少,然后第二个值 1213 花费的时间多一点,再多一点,在四个或五个值之后,执行时间很长(很多分钟),请帮忙我这次减少了,并使这个过程更快。谢谢
代码:
<?php
......
ini_set('include_path', ini_get('include_path').';../Classes/');
include_once 'PHPExcel.php';
include_once 'Excel2007.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->....//set some properties//
$excel->read('sample.xls'); // added excel reader from which we need to take some values
$x=1;
while($x<=$excel->sheets[0]['numRows']) { // reading row by row
$y=1;
while($y<=$excel->sheets[0]['numCols']) {// reading column by column
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$target = $cell;
// $objWorksheet = $objPHPExcel->getActiveSheet();
// $highestRow = $objWorksheet->getHighestRow();
// for($row=1; $row < $highestRow; ++$row){
// $objPHPExcel->getActiveSheet()->removeRow($row,$row);
// }
/* some lines of code using curl to fetch data for $target value
........... */
//below is the code which retrives data from html table and saves into excel file.
$url='results/'.$target.'.html';
include_once('dom.php');
$html=file_get_html($url);
$record_find='first';
foreach($html->find('table#GridView1') as $e){
if($record_find=='first')
$i=1;
$j=0;
foreach($e->find('tr') as $e1){
$distno=trim($e1->find('td', 0)->innertext);
$acno=trim($e1->find('td', 1)->innertext);
$partno=trim($e1->find('td', 2)->innertext);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$j, $distno);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$j, $acno);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$j, $partno);
$j++;
}
}
$objPHPExcel->getActiveSheet()->setTitle($target);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('excelresult/'.$target.'.xlsx');
$y++;
}
$x++;
}
?>
卷曲:
$debug = 1;
$url = "url";
$f = fopen('log.txt', 'w');
$cookies = 'cookies.txt';
touch($cookies);
$useragent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/36.0.1985.125 Chrome/36.0.1985.125 Safari/537.36';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$html = curl_exec($ch);
curl_close($ch);
preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);
$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 985000);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
// Collecting all POST fields
$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__LASTFOCUS'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['cns_fer'] = 2;
$postfields['xttPd'] = $target;
$postfields['tsfDes'] = "Search";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch);
curl_close($ch);
file_put_contents('results/'.$target.'.html', $ret);
类似于:
for($row=1; $row < $highestRow; ++$row){
$objPHPExcel->getActiveSheet()->removeRow($row,$row);
}
每次迭代都会花费很多时间,而且大部分时间都是多余的....
如果您需要从现有工作表中删除所有数据,请使用
$objPHPExcel->getActiveSheet()->removeRow(1,$highestRow);
相反,无需循环(尤其是虚假循环删除已经多次删除的内容),一次调用即可删除所有内容
我也很困惑为什么您使用一个库来读取文件 (excel Reader) 而另一个库用于写入 (PHPExcel) 而您可以使用一个库 (PHPExcel)这两个目的....虽然你似乎对 excel Reader 做的很少,因为你正在遍历该电子表格中的每个单元格,然后似乎根本没有对它做任何事情
编辑
我最后一条评论的意思是这样的:
<?php
......
ini_set('include_path', ini_get('include_path').';../Classes/');
include_once 'PHPExcel.php';
include_once 'Excel2007.php';
$excel->read('sample.xls'); // added excel reader from which we need to take some values
$x=1;
while($x<=$excel->sheets[0]['numRows']) { // reading row by row
$y=1;
while($y<=$excel->sheets[0]['numCols']) {// reading column by column
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$target = $cell;
/* some lines of code using curl to fetch data for $target value
........... */
//below is the code which retrives data from html table and saves into excel file.
$url='results/'.$target.'.html';
include_once('dom.php');
$html=file_get_html($url);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->....//set some properties//
$record_find='first';
foreach($html->find('table#GridView1') as $e){
if($record_find=='first')
$i=1;
$j=0;
foreach($e->find('tr') as $e1){
$distno=trim($e1->find('td', 0)->innertext);
$acno=trim($e1->find('td', 1)->innertext);
$partno=trim($e1->find('td', 2)->innertext);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$j, $distno);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$j, $acno);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$j, $partno);
$j++;
}
}
$objPHPExcel->getActiveSheet()->setTitle($target);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('excelresult/'.$target.'.xlsx');
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
$y++;
}
$x++;
}
?>