简体   繁体   English

Perl写入文件的效率

[英]Perl efficiency in writing in file

I am creating a database with some informations of files. 我正在创建一个包含一些文件信息的数据库。
eg: file_name | 例如: file_name | size | 尺寸 modify_date ... Modify_date ...

I was thinking what is more efficient in this situation: 我在想在这种情况下更有效的方法:

1) For each file get the info and print them in my file 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
}

2) Store the info for every file in a hash and print all the hash at once 2) 将每个文件的信息存储在哈希中,并一次打印所有哈希

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 

You are wrong to consider efficiency before you have even got your program working 在程序尚未运行之前就考虑效率是错误的

You should write your code as clearly as possible and debug it. 您应该尽可能清楚地编写代码并进行调试。 Only then, if it is not running fast enough for your purpose, you should put your code through a profiler to discover the bottlenecks that are taking the most time 只有这样,如果它的运行速度不足以达到您的目的,则应将代码放入探查器中,以找出花费最多时间的瓶颈

The two options you show will probably not be very different unless your files are enormous 除非文件很大,否则显示的两个选项可能不会有很大不同

Doing a benchmark test on the two options i got those results ( which, if I increase the information size for each file, will lead to even bigger differencies between the two). 对这两个选项进行基准测试,我得到了这些结果(如果我增加每个文件的信息大小,将会导致两者之间的差异更大)。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM