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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀