我的PHP脚本花费的时间比PERL脚本多6倍,我的脚本是为站点地图编写xml文件,在这个过程中,我一次从数据库中获取20k数据并在写入XML文件后为其编写xml再次获取另外20k的数据并再次写入另一个xml文件,这样我创建了大约650个xml文件,每个文件包含20k个站点地图记录。意味着我们从DB中获取数据大约650次。
请帮助缩短这段时间或建议任何其他解决方案。
public function SitemapGenerate($dbh='')
{
global $sthsplit;
$total_count=0;
$sql= 'select count(1) COUNT from some_table';
$sth = oci_parse($dbh,$sql);
oci_execute($sth);
oci_fetch_all($sth, $res);
oci_close($dbh);
$total_count = $res['COUNT'][0];
$start=1; $end =20000;
$succesMsg = $this->fetch_data($start,$end,$total_count,$dbh);
// oci_close($dbh);
sleep(1);
}
public function fetch_data($start,$end,$total_count)
{
$sqlsplit = '';
$dbh = $this->connect('abc','abc','db');
$sqlsplit = "select * from some_table where rn between $start and $end";
$sthsplit = oci_parse($dbh,$sqlsplit);
oci_execute($sthsplit);
oci_close($dbh);
$this->splitquery($sthsplit,$total_count,$dbh);
}
public function removeControllCharfunction($str)
{
$str = preg_replace( "/</", '<', $str );
$str = preg_replace( "/>/", '>', $str );
$str = preg_replace( "/(<\s*a\b.*?\/\s*a\s*>)|(<\s*\/?\s*a\b.*?\s*>)/", ' ', $str );
$str = preg_replace( "/(<\s*img\b.*?\s*>)/", ' ', $str );
$str = preg_replace( "/(http\:\/\/|www\.)(.*?)[^\s\<]*/", ' ', $str );
$str = preg_replace( "/\b([\w\-\_\.]*?)(\@[\w\-\_\.]*?)(\s+?|$)/", ' ', $str );
$str = preg_replace("/\&/","&",$str);
return ($str);
}
public function splitquery($sthsplit,$total_count,$dbh)
{
global $filePath,$no_of_files,$file_num,$start_time,$global_record,$no_of_files_mb;
$counter = $ID = $NAME = $records = $cron_time = $time_end = 0;
$xml = $file_name = $imagePath = $largeimagePath = $fullpath = $final_xml = $title = $fullpath_mb = $xml_mb = $final_xml_mb = $file_name_mb = $final_xml_mb1 = $final_xml1 = '';
$ha = array();
while($ha = oci_fetch_array($sthsplit,OCI_ASSOC))
{
$counter++; $valid=0;$records++;$global_record++;
$ID = $ha['ID'];
$NAME = $ha['TITLE'];
$NAME = strtolower($NAME);
$NAME = preg_replace("/^\s+|\s+$/","",$NAME);
$NAME = preg_replace("/[ \'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:\"\<\>,\.\?\\\]+/","-",$NAME);
$NAME = preg_replace("/^-|-$/","",$NAME);
$title = $ha['TITLE'];
$title = $this->removeControllCharfunction($title);
//Mobile Sitemap Code Start here//
$fullpath_mb = "https://somepath?i=$ID";
$xml_mb="<url><loc>$fullpath_mb</loc><mobile:mobile/></url>\n";
$final_xml_mb .= $xml_mb;
//Mobile Sitemap code ends here//
$fullpath=<<<EOT
https://www.somepath/$NAME-$ID.html
EOT;
$xml =<<<EOT
<url><loc>$fullpath</loc>
EOT;
$imagePath = (isset($ha['PHOTO']) && $ha['PHOTO'] != '') ? $ha['PHOTO'] : '';
$largeimagePath = (isset($ha['LARGE_PHOTO']) && $ha['LARGE_PHOTO'] != '') ? $ha['LARGE_PHOTO'] : '';
$imagePath = $this->changeImghostDomain($imagePath);
$largeimagePath = $this->changeImghostDomain($largeimagePath);
$imagePath = preg_replace("/\&/","%26",$imagePath);
$largeimagePath = preg_replace("/\&/","%26",$largeimagePath);
$largeimagePath = preg_replace("/http:/","https:",$largeimagePath);
if((isset($largeimagePath)) && (!(preg_match("/add-image.gif$/i", $largeimagePath) > 0)) && ($largeimagePath != '') && (!(preg_match("/coming-soon.gif$/i", $largeimagePath) > 0)) && (!(preg_match("/ts\/t_product_sample/i", $largeimagePath) > 0)) && (preg_match("/(jpg|jpeg|gif|png|JPG|JPEG|GIF|PNG)$/i", $largeimagePath) > 0) && (preg_match("/imimg\.com/i", $largeimagePath) > 0))
{
$xml .=<<<EOT
<image:image><image:loc>$largeimagePath</image:loc><image:title>$title</image:title></image:image>
EOT;
$valid=1;
}
else if((isset($imagePath)) && (!(preg_match("/imimg\.com/i", $imagePath) > 0)) && ($imagePath != '') && (!(preg_match("/coming-soon.gif$/i", $imagePath) > 0)) && (!(preg_match("/ts\/t_product_sample/i", $imagePath) > 0)) && (preg_match("/(jpg|jpeg|gif|png|JPG|JPEG|GIF|PNG)$/i", $imagePath) > 0) && (preg_match("/imimg\.com/i", $largeimagePath) > 0))
{
$xml .=<<<EOT
<image:image><image:loc>$imagePath</image:loc><image:title>$title</image:title></image:image>
EOT;
$valid=1;
}
$xml .=<<<EOT
<xhtml:link rel="alternate" media="only screen and (max-width: 640px)" href="https://somepath?i={$ha['ID']}" /></url>\n
EOT;
$final_xml .= $xml;
if($counter==20000)
{
// echo "not real: ".(memory_get_peak_usage(false)/1024/1024)." MiB\n";
$file_num++;
$counter=0;
$final_xml1 =<<<EOT
<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">\n$final_xml</urlset>
EOT;
$file_name ="$filePath/$file_num.xml";
$no_of_files .=<<<EOT
https://www.somepath$file_num.xml (Have Complete 20000 Url's)\n
EOT;
$myfile = fopen($file_name, "w") or die("\nFailed To Open File \n$file_name \nAt LINE: ".__LINE__ ."\nIn FILE:".__FILE__);
fwrite($myfile,$final_xml1);
fclose($myfile);
sleep(1);
$final_xml1 = null;
$final_xml = null;
//Mobile XML Code starts here//
$final_xml_mb1=<<<EOT
<?xml version="1.0" encoding="UTF-8"?>\n <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">\n$final_xml_mb</urlset>
EOT;
$file_name_mb="$filePath/$file_num.xml";
$no_of_files_mb .= <<<EOT
https://somepath-$file_num.xml (Have Complete 20000 Url's)\n
EOT;
$myfile1 = fopen($file_name_mb, "w") or die("\nFailed To Open File \n$file_name_mb \nAt LINE: ".__LINE__ ."\nIn FILE:".__FILE__);
fwrite($myfile1,$final_xml_mb1);
fclose($myfile1);
sleep(1);
$final_xml_mb = null;
$final_xml_mb1 = null;
$xml = null;
//Mobile XML code ends here//
echo "not real: ".(memory_get_peak_usage(false)/1024/1024)." MiB\n";
// ob_flush();
// echo "not real: ".(memory_get_peak_usage(false)/1024/1024)." MiB\n";
}
}
if($counter>0 && $counter<20000)
{
$file_num++;
$final_xml=<<<EOT
<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">\n$final_xml</urlset>
EOT;
$file_name=<<<EOT
$filePath/$file_num.xml
EOT;
$no_of_files .=<<<EOT
https://www.somefilepath$file_num.xml (Have Complete $counter Url's)\n
EOT;
$myfile = fopen($file_name, "w") or die("\nFailed To Open File \n$file_name \nAt LINE: ".__LINE__ ."\nIn FILE:".__FILE__);
fwrite($myfile,$final_xml);
fclose($myfile);
sleep(1);
$final_xml = '';
//Mobile XML Code starts here//
$final_xml_mb=<<<EOT
<?xml version="1.0" encoding="UTF-8"?>\n <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">\n$final_xml_mb</urlset>
EOT;
$file_name_mb="$filePath/$file_num.xml";
$no_of_files_mb .= <<<EOT
https://somefilepath-$file_num.xml (Have Complete $counter Url's)\n
EOT;
$myfile1 = fopen($file_name_mb, "w") or die("\nFailed To Open File \n$file_name_mb \nAt LINE: ".__LINE__ ."\nIn FILE:".__FILE__);
fwrite($myfile1,$final_xml_mb);
fclose($myfile1);
sleep(1);
$final_xml_mb = '';
$xml = null;
//Mobile XML code ends here//
// ob_flush();
}
$msg='';
$this->write_index_xml($file_num);
$this->write_mb_index_xml($file_num);
if($records==20000 && $global_record < $total_count)
{
$start= $global_record+1;
if(($total_count-$global_record)> 20000)
$end = $global_record+20000;
else
$end = $global_record+($total_count-$global_record);
oci_free_statement($sthsplit);
$this->fetch_data($start,$end,$total_count,$dbh);
}
}
public function write_index_xml($count)
{
global $filePath;
$s=0;
$newmasterfile = $sitemap_index_final = $sitemap_index_file_name = '';
$mtime = date("Y-m-d").'T'.date("G:i:s");
$sitemap_index_final=<<<EOT
<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.google.com/schemas/sitemap-image/1.1">
EOT;
for($s=1;$s<=$count;$s++)
{
$sitemap_index_final .=<<<EOT
<sitemap>
<loc>https://www.somefilepath$s.xml</loc>
<lastmod>$mtime+5:30</lastmod>
</sitemap>
EOT;
}
$sitemap_index_final .='</sitemapindex>';
$sitemap_index_file_name="$filePath/indexSSL.xml";
if(file_exists($sitemap_index_file_name))
{
$newmasterfile = "$filePath/indexSSL.xml\n";
}
$myfile = fopen($sitemap_index_file_name, "w") or die("\nFailed To Open File \n$sitemap_index_file_name \nAt LINE: ".__LINE__ ."\nIn FILE:".__FILE__);
fwrite($myfile,$sitemap_index_final);
fclose($myfile);
$newmasterfile = $sitemap_index_final = $sitemap_index_file_name = null;
}
public function write_mb_index_xml($count)
{
global $filePath;
$s=0;
$newmasterfile = $sitemap_index_final = $sitemap_index_file_name = '';
$mtime = date("Y-m-d").'T'.date("G:i:s");
$sitemap_index_final=<<<EOT
<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.google.com/schemas/sitemap-image/1.1">
EOT;
for($s=1;$s<=$count;$s++)
{
$sitemap_index_final .=<<<EOT
<sitemap>
<loc>https://somefilepath-$s.xml</loc>
<lastmod>$mtime+5:30</lastmod>
</sitemap>
EOT;
}
$sitemap_index_final .='</sitemapindex>';
$sitemap_index_file_name="$filePath/prod.xml";
if(file_exists($sitemap_index_file_name))
{
$newmasterfile = "$filePath/prod.xml\n";
}
$myfile = fopen($sitemap_index_file_name, "w") or die("\nFailed To Open File \n$sitemap_index_file_name \nAt LINE: ".__LINE__ ."\nIn FILE:".__FILE__);
fwrite($myfile,$sitemap_index_final);
fclose($myfile);
$newmasterfile = $sitemap_index_final = $sitemap_index_file_name = null;
}
public function connect($userid,$password,$dbName)
{
$dbh = oci_connect($userid,$password,$dbName);
if ($dbh)
return $dbh;
else
{
$emsg = oci_error();
$msg.= "\n\n {$emsg['message']}\n\n";
echo "\n\n not connected $msg \n\n";
exit;
}
}
`
答案 0 :(得分:0)
我制作了两个不同的简单脚本,一个用于perl,另一个用于php,令我惊讶的是这些脚本证明perl在命令行脚本(cron jobs)中花费的时间几乎是PHP的四分之一
示例PHP脚本:
<?php
$start_time = (float) array_sum(explode(' ',microtime()));
for($i=1;$i<=20000000;$i++)
{
echo 'hello world';
}
$time_end = (float) array_sum(explode(' ',microtime()));
$cron_time = ($time_end - $start_time);
$cron_time = intval($cron_time);
echo "\ntime taken===".$cron_time;
?>
PERL脚本代码
#!/usr/bin/perl -w
use strict;
my $time_start = time();
for(my $i=1;$i<=20000000;$i++)
{
print 'hello world';
}
my $time_end = time();
my $cron_time = $time_end - $time_start;
$cron_time = int($cron_time);
print "\ntime taken===".$cron_time;
Perl脚本需要17秒,而php需要66秒。