Performance Perl在文件写入中的效率

Performance Perl在文件写入中的效率,performance,perl,Performance,Perl,我正在创建一个包含一些文件信息的数据库。 e、 g:文件名|大小|修改日期… 我在想,在这种情况下,什么更有效: 1) 获取每个文件的信息并将其打印到我的文件中 foreach my $file ( @listOfFiles) { my %temporary_hash = get_info_for_file($file); //store in a tempoarary hash

我正在创建一个包含一些文件信息的数据库。
e、 g:文件名|大小|修改日期

我在想,在这种情况下,什么更有效:

1) 获取每个文件的信息并将其打印到我的文件中

foreach my $file ( @listOfFiles) {                 
    my %temporary_hash = get_info_for_file($file); //store in a tempoarary hash
                                                     the informations for current file
    print_info(%temporary_hash, $output_file);     // print the  information in my output file
}
foreach my $file( @listOfFiles){
    store_info_in_hash( get_info_for_file($file), %hash); // for each file, store the 
                                                             information in a global hash

   }
print_all_info(%hash, $output_file);                       //after i have informations for each file
                                                             print the whole hash in my output file 
2) 将每个文件的信息存储在哈希中,并一次打印所有哈希

foreach my $file ( @listOfFiles) {                 
    my %temporary_hash = get_info_for_file($file); //store in a tempoarary hash
                                                     the informations for current file
    print_info(%temporary_hash, $output_file);     // print the  information in my output file
}
foreach my $file( @listOfFiles){
    store_info_in_hash( get_info_for_file($file), %hash); // for each file, store the 
                                                             information in a global hash

   }
print_all_info(%hash, $output_file);                       //after i have informations for each file
                                                             print the whole hash in my output file 

在你的程序工作

之前,考虑效率是错误的。 您应该尽可能清晰地编写代码并进行调试。只有这样,如果它的运行速度不足以满足您的需要,您才应该通过探查器来发现占用时间最多的瓶颈


您显示的两个选项可能不会有很大的不同,除非您的文件非常庞大

对我得到的两个选项进行基准测试(如果我增加每个文件的信息大小,这将导致两个选项之间更大的差异)


您的程序有四个功能,我不知道它们是做什么的。如果你能解释一下,这可能有助于我们正确理解你的问题。你为什么这么关心效率?我想我会为@listOfFiles编写
print\u info(get\u info\u for_file($),$output\u file),相当于您的第一个选项。但请不要混合使用camelCase和snake_case。我认为我对函数名有暗示作用,我添加了一些comments@Borodin谢谢你的建议!!我很感激!您可能希望以这样的方式编写,即如果1000个文件变成10000个文件,它仍然可以在不重写的情况下工作(即边写文件边写),但这实际上取决于您需要为每个文件存储多少信息,以确定您是否会受到可用内存的限制,10%的差异不值得费心,我不相信基准测试结果,因为我没有看到基准测试。太容易出错了。例如,您是否确实保留了愚蠢的
print\u all\u info(%hash,$output\u file)
调用约定?你的数据有多大?等