What is actually slower? Checking each whitespace on a line for both conditions or parsing the entire line a third time? I guess it depends on how long the line is compared to how many whitespaces there are. If you had a standard text file where each line started at the margin and ended with a CR/LF or just LF I would say that parsing that line a third time would be slower then checking each whitespace against both conditions but this is just off the hip. I have no benchmarks to prove it and am really just asking in the first place.
Edit:
OK well it appears that japhy's suggestion is considerably faster. I would not have thought that it would make that much of a difference but it does appear to be considerable.
Time taken on brian 42 wallclock secs (41.70 usr 0.23 sys + 0.00 cusr 0.00 csys = 41.93 CPU) seconds
Time taken on japhy 17 wallclock secs (16.52 usr 0.21 sys + 0.00 cusr 0.00 csys = 16.73 CPU) seconds
Here is the code I used to benchmark both these methods.
#!/usr/bin/perl -w
use strict;
use Benchmark;
my $file = shift;
my ($start, $end);
$start = new Benchmark;
brian($file);
$end = new Benchmark;
calc($start, $end, 'brian');
$start = new Benchmark;
japhy($file);
$end = new Benchmark;
calc($start, $end, 'japhy');
sub calc {
my ($start, $end, $test) = @_;
my $diff = timediff($end, $start);
print "Time taken on ", $test, " ",timestr($diff, 'all'), " seconds
+\n";
}
sub brian {
my $file = shift;
open(my $in, '<', $file) or die "error: open $file: $!";
for (1..1000) {
seek($in, 0, 0);
my @lines = grep length(), map { s/#.*//; s/^\s+|\s+$//g; $_ } <
+$in>;
}
close($in);
}
sub japhy {
my $file = shift;
open(my $in, '<', $file) or die "error: open $file: $!";
for (1..1000) {
seek($in, 0, 0);
my @lines = grep length(), map { s/#.*//; s/^\s+//g; s/\s$//g; $
+_ } <$in>;
}
close($in);
}
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.