Re: Very slow
- From: George Mpouras <nospam.gravitalsun@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 12 Jan 2012 22:07:54 +0200
Thanks Ben,
What the code actually is doing is to count the fields while respecting the number of \ escaped commas.
A 2k+1 number of slashes \ \\\ \\\\\ ... indicating a non separator
A 2k number of slashed \\ \\\\ ... is a valid comma separator
So your code does not give always correct results.
Have a look at the update I post. At my suprise the codes
while ( ... ) { ... } or
until ( ... ) { ... } or
for ( ... ) { ... }
are killing Perl speed. I can not explaing why but the aproach of
do
{
}
while ( ... )
is much faster. Time from 7 minutes decreased to 20 seconds (the same as your speed). I am not very happy with that, because my C collegue is loughing because perl cannot "for"
Στις 12/1/2012 4:21 μμ, ο/η Ben Morrow έγραψε:
Quoth "George Mpouras"<nospam.gravitalsun@xxxxxxxxxxxxxxxxxx>:Create a test file with 20000000 same lines of 50 commas (it will be<snip>
1020000000 bytes)
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Now I have a perl and C program running almost the same code. Perl needs
about 6 minutes to finish while the C version finishes at 20 seconds.
The difference is huge. What can I do for a faster perl version
Following the two programs
--== C ==--while (getline(&line,&len, stdin) != -1) {^^^
n = 1;
0, otherwise you get an off-by-one error
<snip>#!/usr/bin/perl
my $NC = 1;
open DATA, '<', '/work/test.txt' or die "$^E\n";
while (<DATA>)
{
my ($i, $n, $Length) = (0, 1, length $_);
while ( $i++< $Length )
{
my $c = substr $_, $i, 1;
if ( $c eq ',' ) { $n++ } elsif ($c eq '\\') {$i++}
}
$NC = $n if $n> $NC
}
close DATA;
print "Fields $NC\n"
Well, try not writing C in Perl :).
~% time ./ccommas<commas
50
0m3.29s real 0m3.08s user 0m0.20s system
~% time perl plcommas
Fields 50
6m37.54s real 6m35.89s user 0m1.61s system
~% time perl -nE'
BEGIN { $NC = 1 }
s/\\,//g; $c = tr/,//;
$c> $NC and $NC = $c;
END { say "Fields $NC" }'
Fields 50
0m16.74s real 0m15.48s user 0m1.25s system
~%
So, not quite as fast as the C version, but not too bad.
Ben
.
- Follow-Ups:
- Re: Very slow
- From: Peter J. Holzer
- Re: Very slow
- References:
- Very slow
- From: George Mpouras
- Re: Very slow
- From: Ben Morrow
- Very slow
- Prev by Date: Re: Very slow
- Next by Date: Re: Very slow
- Previous by thread: Re: Very slow
- Next by thread: Re: Very slow
- Index(es):
Relevant Pages
|