doom910730 2012-10-17 16:57
浏览 37
已采纳

自定义标点函数制作脚本运行在PHP的60s运行时限制

I am importing allot of product data from an XML file (about 5000 products). When I run the script I can make it work in about 10-12 seconds.

Now, when I add this punctuation function which makes sure each product description ends with a punctuation sign, the code runs until the php 60 seconds loadtime on my server but I'm not getting any errors. I have error reporting turned on. I just get a final error that the script could not load in 60 seconds.

The question is, looking at this function, is it that resource consuming? What can I do to make it faster?

Upon comments, the problem could be the pregmatch loop. Let me explain what this function does. It check if the last character of the string is an punctuation mark. If it's not, it matches the last char and checks again.

function punctuation($string){
    if(strlen($string) > 5){
        // Get $last_char
        $desired_punctuation = array(".",",","?","!");
        $last_char = substr($string, -1); 
        // Check if $last_char is in the $desired_punctuation array
        if(!in_array($last_char, $desired_punctuation)){ 
        // strip the $mytrim string and get only letters at the end;
            while(!preg_match("/^[a-zA-Z]$/", $last_char)){
                $string = substr($string, 0, -1);
                $last_char = substr($string, -1);
            }
            // add "." to the string
            $string .= '.'; 
        } 
    }
    return $string;
}

If the function is ok, the long runtime must come from something else which I'll have to discover.

I just want your input on this part.

  • 写回答

1条回答 默认 最新

  • dongzhuanlei0768 2012-10-17 17:11
    关注

    It looks like you should be able to replace the while loop with a single preg_replace()

    function punctuation($string){
      if(strlen($string) > 5){
        // Get $last_char
        $desired_punctuation = array(".",",","?","!");
        $last_char = substr($string, -1); 
        // Check if $last_char is in the $desired_punctuation array
        if(!in_array($last_char, $desired_punctuation)){ 
          // remove any trailing non-word characters, and add a period
          $string = preg_replace("/[\W]*$/", '.', $string);
        } 
      }
      return $string;
    }
    

    It would have to be tested if a preg_match() could also be faster than in_array()

    function punctuation($string){
      if(strlen($string) > 5){
        // Check if the last character is a desired punctuation character
        if(!preg_match("/[.,?!]$/", $string)){ 
          // remove any trailing non-word characters, and add a period
          $string = preg_replace("/[\W]*$/", '.', $string);
        } 
      }
      return $string;
    }
    

    To get more detailed information on the run time of your code, including which functions are taking a long time and how often they are called, you should look into the Xdebug and xhprof extensions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大