Note that there are some explanatory texts on larger screens.

plurals
  1. POFastest CSV Parser in Perl
    primarykey
    data
    text
    <p>I am creating a subroutine that:</p> <p>(1) Parses a CSV file;</p> <p>(2) And checks if all the rows in that file have the expected number of columns. It croaks if the number of columns is invalid.</p> <p>When the number of rows is ranging from thousands to millions, <strong>what do you think is the <em>most efficient</em> way to do it?</strong></p> <p>Right now, I'm trying out these implementations.</p> <p><strong>(1) Basic file parser</strong></p> <pre><code>open my $in_fh, '&lt;', $file or croak "Cannot open '$file': $OS_ERROR"; my $row_no = 0; while ( my $row = &lt;$in_fh&gt; ) { my @values = split (q{,}, $row); ++$row_no; if ( scalar @values &lt; $min_cols_no ) { croak "Invalid file format. File '$file' does not have '$min_cols_no' columns in line '$row_no'."; } } close $in_fh or croak "Cannot close '$file': $OS_ERROR"; </code></pre> <p><strong>(2) Using Text::CSV_XS (bind_columns and csv->getline)</strong></p> <pre><code>my $csv = Text::CSV_XS-&gt;new () or croak "Cannot use CSV: " . Text::CSV_XS-&gt;error_diag(); open my $in_fh, '&lt;', $file or croak "Cannot open '$file': $OS_ERROR"; my $row_no = 1; my @cols = @{$csv-&gt;getline($in_fh)}; my $row = {}; $csv-&gt;bind_columns (\@{$row}{@cols}); while ($csv-&gt;getline ($in_fh)) { ++$row_no; if ( scalar keys %$row &lt; $min_cols_no ) { croak "Invalid file format. File '$file' does not have '$min_cols_no' columns in line '$row_no'."; } } $csv-&gt;eof or $csv-&gt;error_diag(); close $in_fh or croak "Cannot close '$file': $OS_ERROR"; </code></pre> <p><strong>(3) Using Text::CSV_XS (csv->parse)</strong></p> <pre><code>my $csv = Text::CSV_XS-&gt;new() or croak "Cannot use CSV: " . Text::CSV_XS-&gt;error_diag(); open my $in_fh, '&lt;', $file or croak "Cannot open '$file': $OS_ERROR"; my $row_no = 0; while ( &lt;$in_fh&gt; ) { $csv-&gt;parse($_); ++$row_no; if ( scalar $csv-&gt;fields &lt; $min_cols_no ) { croak "Invalid file format. File '$file' does not have '$min_cols_no' columns in line '$row_no'."; } } $csv-&gt;eof or $csv-&gt;error_diag(); close $in_fh or croak "Cannot close '$file': $OS_ERROR"; </code></pre> <p><strong>(4) Using Parse::CSV</strong></p> <pre><code>use Parse::CSV; my $simple = Parse::CSV-&gt;new( file =&gt; $file ); my $row_no = 0; while ( my $array_ref = $simple-&gt;fetch ) { ++$row_no; if ( scalar @$array_ref &lt; $min_cols_no ) { croak "Invalid file format. File '$file' does not have '$min_cols_no' columns in line '$row_no'."; } } </code></pre> <p>I benchmark-ed them using the Benchmark module.</p> <pre><code>use Benchmark qw(timeit timestr timediff :hireswallclock); </code></pre> <p>And these are the <strong>numbers (in seconds)</strong> that I got:</p> <p><strong>1,000 lines of file:</strong></p> <blockquote> <p>Implementation 1: 0.0016</p> <p>Implementation 2: 0.0025</p> <p>Implementation 3: 0.0050</p> <p>Implementation 4: 0.0097</p> </blockquote> <p><strong>10,000 lines of file:</strong></p> <blockquote> <p>Implementation 1: 0.0204</p> <p>Implementation 2: 0.0244</p> <p>Implementation 3: 0.0523</p> <p>Implementation 4: 0.1050</p> </blockquote> <p><strong>1,500,000 lines of file:</strong></p> <blockquote> <p>Implementation 1: 1.8697</p> <p>Implementation 2: 3.1913</p> <p>Implementation 3: 7.8475</p> <p>Implementation 4: 15.6274</p> </blockquote> <p>Given these numbers, I would conclude that the simple parser is the fastest but from what I have read from different sources, Text::CSV_XS should be the fastest. </p> <p>Will someone enlighten me on this? Is there something wrong with how I used the modules? Thanks a lot for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload