Speeding up a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cleverbum@hotmail.com

    #1

    Speeding up a function

    I was hoping to parse a webpage and extract all link and image URLs
    from it and enter the new ones into mySQL tables, below is my code to
    do it, I've tried to optimise it as much as I can but it still takes
    too long to execute (server timeouts on a server which I can not
    control) I was wondering if there was some way to compile the code or
    if anyone can spot something which could be better written.
    Thanks in advance,
    Martin

    <?
    $start=microtim e_float();
    $uid=$_GET['uid'];
    $restriction= "dontstayin ";
    include("loadst uff.php"); //just contains a function which echos a tick
    or cross
    include("userpa ss.php"); //contains database access details.
    mysql_connect($ host,$user,$pas sword);
    @mysql_select_d b("$database" ) or die(cross());
    $result = mysql_query("SE LECT * FROM mark_toparse WHERE uid='$uid'");
    //mysql_close();
    $contents = mysql_result($r esult,0,"fconte nts");
    $fullorig = mysql_result($r esult,0,"origin alpath");
    $origpath = substr($origpat h,0,strrpos($fu llorig,"/")+1);
    $lines = explode(">",$co ntents);
    for($i=0;$i<cou nt($lines);$i++ ){
    $imgsrc = stristr($lines[$i],"<img");
    if($imgsrc!=fal se){
    $imgsrc = str_replace("'" ,"",$imgsrc) ;
    $f = strpos($imgsrc, "\"",strpos($im gsrc,"src"));
    $l = strpos($imgsrc, "\"",$f+1);
    $url = substr($imgsrc, $f+1,$l-$f-1);
    if(strncasecmp( $url,"http:",5) !=0){
    $url = $origpath . $url;
    $url= str_replace(":/","://",str_repla ce("//","/",$url));
    }
    $resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
    url='$url'");
    $a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
    if($a[0]==0){
    mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
    }
    }else{
    $link = stristr($lines[$i],"href=");
    if($link!=false ){
    $link = str_replace("'" ,"",$link);
    $f = strpos($link,"\ "");
    $l = strpos($link,"\ "",$f+1);
    $url= substr($link,$f +1,$l-$f-1);
    if(strncasecmp( $url,"http:",5) !=0){
    if(strncasecmp( $url,"mailto:", 7)==0||strncase cmp($url,"ftp:" ,4)==0||strncas ecmp($url,"msni m:",6)==0){
    //ignore ftp, mailto and msn links
    }else{
    $url = $origpath . $url;
    $url= str_replace(":/","://",str_repla ce("//","/",$url));
    if(eregi("\.jp[eg2]{1,2}$",$url)){
    $resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
    url='$url'");
    $a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
    if($a[0]==0){
    mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
    }
    }else{
    $resone = mysql_query("SE LECT count(*) FROM mark_linktodl WHERE
    url='$url'");
    $a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
    if($a[0]==0){
    mysql_query("IN SERT INTO mark_linktodl VALUES('','$url ')");
    }
    }
    }
    }elseif(eregi($ restriction,$ur l)){
    if(eregi("\.jp[eg2]{1,2}$",$url)){
    $resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
    url='$url'");
    $a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
    if($a[0]==0){
    mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
    }
    }else{
    $resone = mysql_query("SE LECT count(*) FROM mark_linktodl WHERE
    url='$url'");
    $a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
    if($a[0]==0){
    mysql_query("IN SERT INTO mark_linktodl VALUES('','$url ')");
    }
    }
    }
    }
    }

    }
    //mysql_query("DE LETE FROM mark_toparse WHERE
    originalpath='$ fullorig'");
    mysql_query("IN SERT INTO mark_parsed
    VALUES('','$ful lorig','".md5($ contents)."')") ;
    mysql_close();
    $finish=microti me_float();

    if(strcmp($_GET['debug'],"t")==0){
    $tim=$finish-$start;
    include("error_ image.php");
    echo imagepng(errori mage("Analysis: $tim s"));
    }else{
    echo tick();
    }

    function microtime_float ()
    {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
    }

    ?>

  • Rik

    #2
    Re: Speeding up a function

    Cleverbum@hotma il.com wrote:
    I was hoping to parse a webpage and extract all link and image URLs
    from it and enter the new ones into mySQL tables, below is my code to
    do it, I've tried to optimise it as much as I can but it still takes
    too long to execute (server timeouts on a server which I can not
    control) I was wondering if there was some way to compile the code or
    if anyone can spot something which could be better written.
    Thanks in advance,
    Martin

    Well, it would be simpler for us if you could describe what it is exactly
    what you're trying to do, instead of letting us decypher it.

    1. You can use a WHERE REGEXP to have a small result to check from the
    database.
    2. In the case, I really advise preg_replace() to change the img src
    instead of the exploding, looping strpos, str_replace etc. This can be a
    one-liner.
    3. MySQL has a handy REPLACE INTO, as long as you have a correct key, it
    would mean that no checking on already existing rows is required.
    --
    Rik Wasmus


    Comment

    • Chung Leong

      #3
      Re: Speeding up a function

      Rik wrote:
      >
      Well, it would be simpler for us if you could describe what it is exactly
      what you're trying to do, instead of letting us decypher it.
      >
      1. You can use a WHERE REGEXP to have a small result to check from the
      database.
      2. In the case, I really advise preg_replace() to change the img src
      instead of the exploding, looping strpos, str_replace etc. This can be a
      one-liner.
      3. MySQL has a handy REPLACE INTO, as long as you have a correct key, it
      would mean that no checking on already existing rows is required.
      --
      Rik Wasmus
      Combining all the insert/replace operation into one statement would
      help as well. Instead of doing a query immediate, store the links in
      different arrays like this:

      $images[$url] = true;

      That collapses duplicate links in the page being parsed. Then it's just
      a matter of looping through the arrays to build sql statements that
      would create the records in one fell swoop.

      Comment

      • Chung Leong

        #4
        Re: Speeding up a function

        Rik wrote:
        >
        Well, it would be simpler for us if you could describe what it is exactly
        what you're trying to do, instead of letting us decypher it.
        >
        1. You can use a WHERE REGEXP to have a small result to check from the
        database.
        2. In the case, I really advise preg_replace() to change the img src
        instead of the exploding, looping strpos, str_replace etc. This can be a
        one-liner.
        3. MySQL has a handy REPLACE INTO, as long as you have a correct key, it
        would mean that no checking on already existing rows is required.
        --
        Rik Wasmus
        Combining all the insert/replace operation into one statement would
        help as well. Instead of doing a query immediate, store the links in
        different arrays like this:

        $images[$url] = true;

        That collapses duplicate links in the page being parsed. Then it's just
        a matter of looping through the arrays to build sql statements that
        would create the records in one fell swoop.

        Comment

        Working...