【问题标题】:Reduce looping execution time减少循环执行时间
【发布时间】:2018-02-04 05:32:55
【问题描述】:

我有 35 秒的时间来执行此代码。如何减少执行时间?我应该在这个源代码中改变什么。

    $file_handle = fopen("WMLG2_2017_07_11.log", "r");
while (!feof($file_handle)) {
   $line = fgets($file_handle);
   if (strpos($line, 'root@CLA-0 [WMLG2]  >') !== false) {
        $namafileA = explode('> ', $line);
        $namafile = str_replace(' ', '_', $namafileA[1]);
        $filenameExtension = $namafile.".txt";
        $file = preg_replace('/[^A-Za-z0-9\-_.]/', '', $filenameExtension); // hapus special character kecuali "." dan "_"
    } else {
        $newfile = fopen("show_command_file_Tes2/$file", "a");
        fwrite($newfile, $line);
    }
}
fclose($file_handle);

【问题讨论】:

    标签: php loops while-loop file-handling execution-time


    【解决方案1】:

    我发现您对原始代码所做的一些错误可能会影响您的性能,但我不确定影响程度。

    如果我理解正确,您正在打开一个日志文件并将消息整理到单独的文件中。

    您尚未从日志文件中粘贴示例,但我假设您有重复的文件目标,并非日志文件的每一行都有单独的文件目标。

    您的代码打开,但从不关闭句柄,并且在脚本运行期间保持打开状态。垃圾收集器没有关闭文件句柄,您必须手动执行以释放资源。

    基于此,您应该存储文件指针(或至少关闭它们)并重新使用已打开的句柄。您在执行期间至少打开 X 行句柄,而不是关闭它/重用它,其中 X 是文件中的行数。

    我注意到的另一件事是,您的行可能很长,这是一种罕见的情况,php 的 strpos() 函数可能比匹配字符串正确位置的正则表达式慢。如果没有日志文件,我不能肯定地说,因为preg_match() 在简单/短字符串上是相当昂贵的函数(strpos() 更快。)

    如果它是一个日志文件,很可能以那个 "root@CLA"... 字符串开头,如果你可以用^(字符串的开头)或@987654326 指定字符串位置,你应该尝试匹配它@(字符串结尾)。

    <?php
    
    $file_handle = fopen("WMLG2_2017_07_11.log", "r");
    
    //you 'll store your handles here
    $targetHandles = [];
    
    while (!feof($file_handle))
    {
        $line = fgets($file_handle);
        if (strpos($line, 'root@CLA-0 [WMLG2]  >') !== false)
        {
            $namafileA = explode('> ', $line);
            $namafile = str_replace(' ', '_', $namafileA[1]);
            $filenameExtension = $namafile . ".txt";
            $file = preg_replace('/[^A-Za-z0-9\-_.]/', '', $filenameExtension); // hapus special character kecuali "." dan "_"
        }
        else
        {
            //no $file defined, most likely nothing to write yet
            if (empty($file))
            {
                continue;
            }
    
            //if its not open, we'll make them open
            if (empty($targetHandles[$file]))
            {
                $targetHandles[$file] = fopen("show_command_file_Tes2/$file", "a");
            }
            //writing the line to target
            fwrite($targetHandles[$file], $line);
        }
    }
    
    //you should close your handles every time
    foreach ($targetHandles as $handle)
    {
        fclose($handle);
    }
    
    fclose($file_handle);
    

    【讨论】:

    • 从您的代码中,变量 $file undefined 这里是日志文件,如果您想查看它dropbox.com/s/z8tvmegokghrjok/WMLG2_2017_07_11.log?dl=0@Fiber
    • 是的,你是对的,但我不知道你对那个变量的意图是什么,你在 $file = preg_replace('/[^A-Za-z0-9\-_.]/', '', $filenameExtension); // hapus special character kecuali "." dan "_" 中定义)` 但这可能是未定义的。让我编辑我的帖子并更正它。
    • $file = preg_replace('/[^A-Za-z0-9\-.]/', '', $filenameExtension) 我用它来删除所有空间,因为如果我没有定义它的文件名将是 ex: show_alarm_active .txt 在我放扩展名之前,字符串的最后一个有空格
    • 是的,但是 $file 作为一个以前从未定义过的变量,您正在使用该表达式进行定义。当$file 尚未定义时,我做了一些更改以跳过。
    • 哇它的工作,我刚刚定义 $file = "";及其运行
    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    相关资源
    最近更新 更多