如何重新编码我的 php 脚本以尽快运行? [英] How can I re-code my php script to run as quickly as possible?
本文介绍了如何重新编码我的 php 脚本以尽快运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行一个脚本,该脚本将检查域名的可用性(10 次)并输出域(如果可用)和时间戳(以毫秒为单位).
你能找到任何会减慢脚本速度的东西吗?如果您可以调整并重新发布或建议可以做得更好的地方,将不胜感激!谢谢.
';//print_r($line);//echo '</pre>';}//对每一行做一些事情.回声 $tmp[0];echo "
";回声 $tmp[1];//echo $tmp[2];echo "
";echo udate('H:i:s:u');echo "<br><br>";返回 $tmp;}while ($loops <= 10){$suffixes=urlencode("com.au");$domain = "sampledomain";$fuzzysearch = "0";$returnUrl="http://mydomain.com.au/test.php";$url = "https://apidomain.com.au/check.php?domain=" .$域."&suffixes=" .$后缀."&fuzzysearch=".$模糊搜索;$output = GetCurlPage("$url");++$循环;}?>
解决方案
慢,因为你需要使 10 curl 到外部站点
两个建议
- 更新您的 test.php/check.php 以允许在一次 curl 调用中检查多个域名(而不是逐一检查,传递一个数组)
- 使用 curl_multi_exec 允许并行 curl 10 个不同的 URL同时
我更喜欢建议 1
I'm running a script which will check the availability (10 times) of a domain name and output the domain, if available and a timestamp (with milliseconds).
Can you find anything which is slowing down the script even marignally? If you could please adjust and re-post or advise what can be done better, it would be very much appreciated! Thank you.
<?php
date_default_timezone_set('Australia/Brisbane');
$loops = 0;
function udate($format, $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
function GetCurlPage ($pageSpec)
{
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec ($ch);
curl_close ($ch);
$tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
$tmp = explode('<br>', $tmp);
foreach ($tmp AS $line) {
//echo '<pre>';
//print_r($line);
//echo '</pre>';
}
// Do something with each line.
echo $tmp[0];
echo "<br>";
echo $tmp[1];
//echo $tmp[2];
echo "<br>";
echo udate('H:i:s:u');
echo "<br><br>";
return $tmp;
}
while ($loops <= 10)
{
$suffixes=urlencode("com.au");
$domain = "sampledomain";
$fuzzysearch = "0";
$returnUrl="http://mydomain.com.au/test.php";
$url = "https://apidomain.com.au/check.php?domain=" .
$domain . "&suffixes=" . $suffixes . "&fuzzysearch=" . $fuzzysearch;
$output = GetCurlPage("$url");
++$loops;
}
?>
解决方案
The slowness because you need to make 10 curl to external site
Two suggestions
- update your test.php/check.php to allow multiple domain name check at one curl call (instead of checking one-by-one, pass an array)
- use curl_multi_exec to allow parallel curl 10 different URLs at the same time
I would prefer suggestion 1
这篇关于如何重新编码我的 php 脚本以尽快运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文