Thanks, Jim. I never even thought of benchmarking. So I put together a quick test using Benchmark and here are the results:
# ./read_test_perl.pl Benchmark: timing 10000 iterations of Grep, Perl... Grep: 75 wallclock secs ( 0.19 usr 6.82 sys + 13.27 cusr 54.30 csys = 74.58 CPU) @ 1426.53/s (n=10000) Perl: 1 wallclock secs ( 0.39 usr + 0.05 sys = 0.44 CPU) @ 22727.27/s (n=10000) It looks like Perl is much, much faster running this process than calling out to grep. I did have a pipe to awk in with the grep, so I just removed that altogether just to make sure it wasn't awk that was causing it to take so much longer: # ./read_test_perl.pl Benchmark: timing 10000 iterations of Grep, Perl... Grep: 43 wallclock secs ( 0.20 usr 7.04 sys + 8.77 cusr 27.05 csys = 43.06 CPU) @ 1381.22/s (n=10000) Perl: 1 wallclock secs ( 0.40 usr + 0.06 sys = 0.46 CPU) @ 21739.13/s (n=10000) So it looks like awk was accounting for less than half of total time. Keeping it in the house and running things through perl definitely seems like the way to go here. Thanks again! On Thu, Jul 19, 2012 at 8:21 PM, Jim Gibson <jimsgib...@gmail.com> wrote: > > On Jul 19, 2012, at 5:10 PM, Sheppy R wrote: > > > I'm wondering what is the least resource intensive way to read just one > > item of a file. I'm looking at one of two options (I'm assuming there > are > > more that I'm just not familiar with). The first idea is to run grep > > through a system command. The second is to open the file for reading > then > > scan each line for the matching text. > > > > my $thing = `grep "text" file`; > > > > my $file = 'file'; > > my $FH; > > open($FH, '<', $file); > > for <FH> > > { > > if m/(thing)/ > > { $thing = $1 }}; > > > > I know its basic code with a lot of stuff missing, but I think you get > the > > idea. Is there another method that might be even cheaper than these two? > > Opening and reading the file from within your program will be more > efficient than forking a child process to run grep and return its output. > Creating a process has some overhead, and grep will still have to open and > read the entire file. > > You can make your program even more efficient if you stop reading the file > once you have found your item. > > However, unless you have very large files, the difference in efficiency in > any of these approaches is likely to be negligible from a practical > standpoint. > > When in doubt, benchmark! > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >