Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文件编写优化 编辑:优化结果在这个问题的结尾!_Php_Optimization_File_Pcre - Fatal编程技术网

PHP文件编写优化 编辑:优化结果在这个问题的结尾!

PHP文件编写优化 编辑:优化结果在这个问题的结尾!,php,optimization,file,pcre,Php,Optimization,File,Pcre,您好,我有一个下面的代码,首先扫描一个特定文件夹中的文件,然后逐行读取每个文件,然后在多次“if…else if”之后,将新修改的文件写入另一个文件夹,其名称与打开时相同 问题是逐行写入文件似乎非常缓慢。默认的60秒限制只能容纳25个左右的文件。文件大小从10k到350k不等 任何优化代码以使其运行更快的方法。逐行读取,将每行放入一个数组,然后将整个数组写入一个新的文本文件(而不是逐行读取/写入)是否更好。如果是,在实践中是如何做到的 提前谢谢 -----代码如下----- 我怀疑文件写入是这里

您好,我有一个下面的代码,首先扫描一个特定文件夹中的文件,然后逐行读取每个文件,然后在多次“if…else if”之后,将新修改的文件写入另一个文件夹,其名称与打开时相同

问题是逐行写入文件似乎非常缓慢。默认的60秒限制只能容纳25个左右的文件。文件大小从10k到350k不等

任何优化代码以使其运行更快的方法。逐行读取,将每行放入一个数组,然后将整个数组写入一个新的文本文件(而不是逐行读取/写入)是否更好。如果是,在实践中是如何做到的

提前谢谢 -----代码如下-----


我怀疑文件写入是这里的性能问题。你正在运行十个正则表达式


使用字符串方法(如查找子字符串)可能会加快速度。

如果可以将正则表达式更改为或类似的-stripos()(不区分大小写),则删除正则表达式将使性能得到最大的提高-您应该注意到速度的提高

测试需要
”!==false“
,因为找到的字符串可能位于位置0。例如,您的第一个测试用例可以是():


您还可能会发现使用fgets()而不是一次读取整个文件可能会提高性能(但这更像是内存问题)。正如其他人提到的,只写循环中的文件,不要打开和关闭它。

我认为这是因为你在循环中打开/关闭文件,尝试在foreach之前移动fopen(),在它之后移动fclose

  • file()
    切换到
    fgets()
    。这将一次只将一行加载到内存中,而不是文件中的每一行
  • 将调用更改为
    preg\u match()
    stripos()
    (如果适用)。应该快一点
  • $ourFileHandle
    的打开/关闭移动到外部循环中。这将大大减少对文件系统的stat调用的数量,并且应该会大大加快速度
  • 在这个庞大的if.。else中可能还有很多其他的优化,但我将把这些留给另一个SOer(或您)来完成


    那么它只会将每个文件的最后一行写入一个新文件,不是吗?我同意你肯定不想做那么多次I/O。只需将修改后的每一行附加到一个新字符串,然后将新字符串写出一次,可能会更快。只要您将fwrite()保留在循环中,它就会保持文件打开状态,并继续将每一行写入其中。如何将我的“preg_匹配('/\S+calls/i',$line)”替换为strps()?“\S+”是否有替代项?如果前9个匹配项中没有一个匹配项,则仅为10。考虑到这一点,按照概率的顺序重新测试if-elseifs可能会在某种程度上提高性能。我一直在想为什么它必须是“不假”而不是“真”?它提供了更好的性能或可靠性吗?@mika-strps()的PHP手册页突出地解释了这一点。@minka我添加了简短的解释,并将函数名链接到手册页。如果能看到非正则表达式匹配的性能增益与文件I/O减少的增益,那将很有趣。我将在返回“我的实验室”时发布一些结果.我的代码是基于MalphasWats的代码。从下面的链接中可以找到我最初的、非常混乱的问题。如果您查看它,只需要计算调用值。谢谢我会在这里根据你的代码重写我的代码,并保留大家在这里告诉我的其他想法。
    <?php
    
    function scandir_recursive($path)    {
    ...
    ...
    }
    
    
    
    $fileselection = scandir_recursive('HH_new');
    foreach ($fileselection as $extractedArray) {
    $tableName = basename($extractedArray); // Table name
    $fileLines=file($extractedArray);
        foreach ($fileLines as $line) {
                if(preg_match('/\(all-in\)/i' , $line)) {
                    $line = stristr($line, ' (all-in)', true) .', and is all in';
                    $allin = ', and is all in';
                }
                else {
                    $allin = '';
                }
                if(preg_match('/posts the small blind of \$[\d\.]+/i' , $line)) {
                    $player = stristr($line, ' posts ', true);
                    $betValue = substr(stristr($line, '$'), 1);
                    $bettingMatrix[$player]['betTotal'] = $betValue;
                }
                else if(preg_match('/posts the big blind of \$[\d\.]+/i' , $line)) {
                    $player = stristr($line, ' posts ', true);
                    $betValue = substr(stristr($line, '$'), 1);
                    $bettingMatrix[$player]['betTotal'] = $betValue;
                }
                else if(preg_match('/\S+ raises /i' , $line)) {
                    $player = stristr($line, ' raises ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $bettingMatrix[$player]['betTotal'] = $betValue; //total bet this hand (shortcut)
                }
                else if(preg_match('/\S+ bets /i' , $line)) {
                    $player = stristr($line, ' bets ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $bettingMatrix[$player]['betTotal'] = $betValue; //total bet this hand (shortcut)
                }
                else if(preg_match('/\S+ calls /i' , $line)) {
                    $player = stristr($line, ' calls ', true);
                    $betValue = substr(stristr($line, '$'), 1);
                    $callValue = $betValue - $bettingMatrix[$player]['betTotal']; //actual amount called
                    $bettingMatrix[$player]['betTotal'] = $betValue;
                    $line = stristr($line, '$', true)."\$".$callValue.$allin;
                    $allin = '';
                }
                else if(preg_match('/(\*\*\* (Flop|Turn|River))|(Full Tilt Poker)/i' , $line)) {
                    unset($bettingMatrix); //zero $betValue
                }
                else if(preg_match('/\*\*\* FLOP \*\*\*/i' , $line)) {
                    $flop = substr(stristr($line, '['), 0, -2);
                    $line = '*** FLOP *** '. $flop;
                }
                else if(preg_match('/\*\*\* TURN \*\*\*/i' , $line)) {
                    $turn = substr(stristr($line, '['), 0, -2);
                    $line = '*** TURN *** '. $flop .' '. $turn;
                }
                else if(preg_match('/\*\*\* RIVER \*\*\*/i' , $line)) {
                    $river = substr(stristr($line, '['), 0, -2);
                    $line = '*** RIVER *** '. substr($flop, 0, -1) .' '. substr($turn, 1) .' '. $river;
                }
                else {
                }
            $ourFileHandle = fopen("HH_newest/".$tableName.".txt", 'a') or die("can't open file");
            fwrite($ourFileHandle, $line);
            fclose($ourFileHandle);
        }
    }
    ?>
    
    $fileselection = scandir_recursive('HH_new');
    foreach ($fileselection as $extractedArray) {
        $tableName = basename($extractedArray); // Table name
        $handle         = fopen($extractedArray, 'r');
        $ourFileHandle  = fopen("HH_newest/".$tableName.".txt", 'a') or die("can't open file");
        while ($line = fgets($handle)) {
                if (FALSE !== strpos($line, '(all-in)')) {
                    $line = strstr($line, ' (all-in)', true) .", and is all in\r\n";
                    $allin = ', and is all in';
                } else {
                    $allin = '';
                }
                if (FALSE !== strpos($line, ' posts the small blind of $')) {
                    $player = strstr($line, ' posts ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $bettingMatrix[$player]['betTotal'] = $betValue;
                }
                else if (FALSE !== strpos($line, ' posts the big blind of $')) {
                    $player = strstr($line, ' posts ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $bettingMatrix[$player]['betTotal'] = $betValue;
                }
                else if (FALSE !== strpos($line, ' posts $')) {
                    $player = strstr($line, ' posts ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $bettingMatrix[$player]['betTotal'] += $betValue;
                }
                else if (FALSE !== strpos($line, ' raises to $')) {
                    $player = strstr($line, ' raises ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $betMade = $betValue - $bettingMatrix[$player]['betTotal']; //actual amount raised by
                    $bettingMatrix[$player]['betTotal'] = $betValue; //$line contains total bet this hand (shortcut)
                }
                else if (FALSE !== strpos($line, ' bets $')) {
                    $player = strstr($line, ' bets ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $betMade = $betValue - $bettingMatrix[$player]['betTotal']; //actual amount raised by
                    $bettingMatrix[$player]['betTotal'] = $betValue; //$line contains total bet this hand (shortcut)
                }
                else if (FALSE !== strpos($line, ' calls $')) {
                    $player = strstr($line, ' calls ', true);
                    $betValue = substr(strstr($line, '$'), 1);
                    $callValue = $betValue - $bettingMatrix[$player]['betTotal']; //actual amount called
                    $bettingMatrix[$player]['betTotal'] = $betValue;
                    $line = strstr($line, '$', true)."\$".$callValue.$allin. "\r\n";
                    $allin = '';
                }
                else if (FALSE !== strpos($line, '*** FLOP ***')) {
                    $flop = substr(strstr($line, '['), 0, -2);
                    unset($bettingMatrix); //zero $betValue
                }
                else if (FALSE !== strpos($line, '*** TURN ***')) {
                    $turn = substr(strstr($line, '['), 0, -2);
                    $line = '*** TURN *** '.$flop.' '.$turn."\r\n";
                    unset($bettingMatrix); //zero $betValue
                }
                else if (FALSE !== strpos($line, '*** RIVER ***')) {
                    $river = substr(strstr($line, '['), 0, -2);
                    $line = '*** RIVER *** '. substr($flop, 0, -1) .' '. substr($turn, 1) .' '. $river."\r\n";
                    unset($bettingMatrix); //zero $betValue
                }
                else if (FALSE !== strpos($line, 'Full Tilt Poker')) {
                    unset($bettingMatrix); //zero $betValue
                }
                else {
                }
            fwrite($ourFileHandle, $line);
        }
        fclose($handle);
        fclose($ourFileHandle);
    }
    
    if(stripos($line, '(all-in)') !== false) {
        //generate output
    }
    
    $fileselection = scandir_recursive('HH_new');
    foreach ($fileselection as $extractedArray)
    { 
      $tableName     = basename( $extractedArray ); // Table name
      $handle        = fopen( $extractedArray, 'r' );
      $ourFileHandle = fopen("HH_newest/".$tableName.".txt", 'a') or die("can't open file");
    
      while ( $line = fgets( $handle ) )
      {
        if ( false !== stripos( $line, '(all-in)' ) )
        {
          $line = stristr($line, ' (all-in)', true) .', and is all in';
          $allin = ', and is all in';
        } else {
          $allin = '';
        }
        if ( preg_match('/posts the small blind of \$[\d\.]+/i' , $line ) )
        {
                $player = stristr($line, ' posts ', true);
                $betValue = substr(stristr($line, '$'), 1);
                $bettingMatrix[$player]['betTotal'] = $betValue;
        }
        else if(preg_match('/posts the big blind of \$[\d\.]+/i' , $line)) {
                $player = stristr($line, ' posts ', true);
                $betValue = substr(stristr($line, '$'), 1);
                $bettingMatrix[$player]['betTotal'] = $betValue;
        }
        else if(preg_match('/\S+ raises /i' , $line)) {
                $player = stristr($line, ' raises ', true);
                $betValue = substr(strstr($line, '$'), 1);
                $bettingMatrix[$player]['betTotal'] = $betValue; //total bet this hand (shortcut)
        }
        else if(preg_match('/\S+ bets /i' , $line)) {
                $player = stristr($line, ' bets ', true);
                $betValue = substr(strstr($line, '$'), 1);
                $bettingMatrix[$player]['betTotal'] = $betValue; //total bet this hand (shortcut)
        }
        else if(preg_match('/\S+ calls /i' , $line)) {
                $player = stristr($line, ' calls ', true);
                $betValue = substr(stristr($line, '$'), 1);
                $callValue = $betValue - $bettingMatrix[$player]['betTotal']; //actual amount called
                $bettingMatrix[$player]['betTotal'] = $betValue;
                $line = stristr($line, '$', true)."\$".$callValue.$allin;
                $allin = '';
        }
        else if(preg_match('/(\*\*\* (Flop|Turn|River))|(Full Tilt Poker)/i' , $line)) {
                unset($bettingMatrix); //zero $betValue
        }
        else if ( FALSE !== stripos( $line, '*** FLOP ***' ) )
        {
                $flop = substr(stristr($line, '['), 0, -2);
                $line = '*** FLOP *** '. $flop;
        }
        else if ( FALSE !== stripos( $line, '*** TURN ***' ) )
        {
                $turn = substr(stristr($line, '['), 0, -2);
                $line = '*** TURN *** '. $flop .' '. $turn;
        }
        else if ( FALSE !== stripos( $line, '*** RIVER ***' ) )
        {
                $river = substr(stristr($line, '['), 0, -2);
                $line = '*** RIVER *** '. substr($flop, 0, -1) .' '. substr($turn, 1) .' '. $river;
        }
        else {
        }
        fwrite($ourFileHandle, $line);
      }
      fclose( $handle );
      fclose( $ourFileHandle );
    }