Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

performance - How can I re-code my php script to run as quickly as possible?

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;
    }           
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...