How do I print the average time taken to open a file for reading?
I am new to Perl and I have to do some automation at my work. I have to take a path to a directory containing multiple files and print the average time taken to read 开发者_如何学Goor open a particular file. Any help is very much appreciated.
Use Time::HiRes to be able to time things more precisely.
Why is it that you want to print an average time? That's going to be really hard to do in any meaningful way; caching is going to make reads after the first much faster.
Perl has fabulous benchmarking and timing tools.
While Time::HiRes can time accurately, you may wish to consider more complete benchmarking tools to see if the opening, reading or something else reading is the bottleneck. You will also need to write the code to time the relevant bits if Time::HiRes is the only tool in your bag.
For statement level benchmarking, I use Devel::NYTProf. It is fairly simple to use, and has a beautiful HTML output that will show what is going on under the hood of your program.
For very fast, simple subroutine level benchmarking I use the core module Benchmark. Randal Schwartz has an article about using Benchmark in the Unix Review here. Benchmark can be used to time a statement or subroutine literally in seconds. It is best combined with Time::HiRes for more accurate timing. Benchmark is a core distribution; Time::HiRes needs to be installed from CPAN.
The easiest from the get-go is to install Time::HiRes and then use Benchmark for a simple table on the time of the relevant statements or subroutines. If there seems to be more to dig into, use the more complex Devel::NYTProf.
You can use time() function to get the time at start and end of the file open Then you can calculate the difference between them. Then you can average out. You can do the same with reading files too
精彩评论