An esteemed monk (who shall remain nameless) posted recently that he had benchmarked Text::xSV and Text::CSV_XS and found them to be comparable in speed. He did not include his benchmark tests. While I have great respect for Tilly's Text::xSV, I found it hard to believe that a pure perl module would be as fast as an XS module at CSV tasks. Running the benchmarks below, I find that Text::CSV_XS is approximately three times faster than Text::xSV at both writing and reading CSV files. The results on my 800mhz pentium III running debian sarge were as follows:
  Compare file creation with Text::xSV and Text::CSV_XS
        Rate  xSV  CSV
  xSV 11.6/s   -- -62%
  CSV 30.3/s 161%   --

  Compare file reading with Text::xSV and Text::CSV_XS
        Rate  xSV  CSV
  xSV 3.49/s   -- -73%
  CSV 12.8/s 266%   --
But my benchmark fu is not very strong. I would appreciate it if others could look at the benchmark and suggest any improvements, or, if I've made some horrible mistake, to please point it out.

Disclaimer : I'm currently the maintainer of Text::CSV_XS, but it was written by Jochen Wiedmann and all credit goes to him. And again, this isn't a dispargement of the excellent Text::xSV, it has a nicer interface and the features of the two modules overlap in the sense that each does things the other doesn't. Both can use alternate record separators and can handle embedded newlines and commas.
#!perl -w use strict; use Text::CSV_XS; use Text::xSV; use IO::File; use Benchmark qw(:all); my($cols,$data,$rows) = ( ['Name','City','Num'], [], 999 ); for my $num(0..$rows) { push @$data, ["myself\nme","Portland,Oregon",$num]; } print "\nCompare file creation with Text::xSV and Text::CSV_XS\n"; cmpthese( 50, { xSV => sub { create_xSV('test.xSV',$cols,$data) } , CSV => sub { create_CSV('test.CSV',$cols,$data) } } ); create_xSV('test.xSV',$cols,$data,'keep'); create_CSV('test.CSV',$cols,$data,'keep'); print "\nCompare file reading with Text::xSV and Text::CSV_XS\n"; cmpthese( 50, { xSV => sub { read_xSV('test.xSV') } , CSV => sub { read_CSV('test.CSV') } } ); sub create_xSV { my($fname,$cols,$data,$keep) = @_; my $fh = IO::File->new(">$fname") or die $!; my $csv = Text::xSV->new( fh=>$fh, header=>$cols); $csv->print_header(); $csv->print_row(@$_) for @$data; $fh->close; if (!$keep) { unlink $fname or die $!; } } sub create_CSV { my($fname,$cols,$data,$keep) = @_; my $csv = Text::CSV_XS->new({ binary => 1 }); my $fh = IO::File->new(">$fname") or die $!; $csv->print($fh,$cols); $fh->print("\n"); for (@$data){ $csv->print($fh,$_); $fh->print("\n"); } $fh->close; if (!$keep) { unlink $fname or die $!; } } sub read_xSV { my $fname = shift; my $fh = IO::File->new("$fname") or die $!; my $csv = Text::xSV->new( fh=>$fh, header=>$cols); my $count; $csv->read_header(); while ($csv->get_row()) { my @row = $csv->extract(qw(Name City Num)); die 'Bad Read' unless "@row" eq "@{$data->[$count++]}"; } die "Bad number of rows '$count'" unless $count == $rows+1; $fh->close; } sub read_CSV { my $fname = shift; my $fh = IO::File->new("$fname") or die $!; my $csv = Text::CSV_XS->new({binary=>1}); my $header = $csv->getline($fh); my $count=0; while ( my $columns = $csv->getline($fh) ) { last if !defined $columns->[0]; die 'Bad Read' unless "@$columns" eq "@{$data->[$count++]}"; } die "Bad number of rows '$count'" unless $count == $rows+1; $fh->close; } __END__

In reply to Benchmark comparison of Text::xSV and Text::CSV_XS by jZed

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.