I need help profiling this code, it needs to be quicker.
I need to capture the word that follows the first occurence of the string "hostname" in several thousand files. At first I tried a system call to grep, since grep is a C binary I thought it would be quickest, but apparently the system call overhead makes it slower.
This is what I have:
@cache = `cat filelist`;
foreach $path (@cache) {
open F, $path;
while (<F>) {
if (/hostname ([\-\w]+)/) {
$hostname = $1;
last;
}
}
print "$hostname\n";
}
Here is the Devel::SmallProf output
================ SmallProf version 0.9 ================
Profile of prof
+ Page 1
===============================================================
+==
count wall tm cpu time line
0 0.000000 0.000000 1:#!/opt/CSCOpx/bin/perl
0 0.000000 0.000000 2:
1 0.000004 0.000000 3:$RUNDIR = "/var/adm/links/newrun";
0 0.000000 0.000000 4:
0 0.000000 0.000000 5:
1 0.013885 0.010000 6:@cache = `cat $RUNDIR/filelist`;
0 0.000000 0.000000 7:
11600 0.059747 0.050000 8:foreach $path (@cache) {
11599 0.906185 0.080000 9: open F, $path;
284014 3.008497 1.210000 10: while (<F>) {
283652 3.195402 0.840000 11: if (/hostname ([-\w]+)/) {
11599 0.101677 0.060000 12: $hostname = $1;
11599 0.094969 0.040000 13: last;
0 0.000000 0.000000 14: }
0 0.000000 0.000000 15: }
11599 0.127013 0.050000 16: print $hostname." ";
0 0.000000 0.000000 17:}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.