Working with CSV Files Part 2 – CsvHelper

Because I have previously written 2 articles with my own code for working with CSV files, I thought it was time I did a comparison between my own simple code and the most common Nuget package, CsvHelper.

The articles I have previously written are

What is CsvHelper

CsvHelper is a .NET library for reading and writing CSV files. Extremely fast, flexible, and easy to use.

It is an Open Source project with 87 contributors (at time of writing).

I chose CsvHelper simply because the Nuget package has been download some 113 million times, and I’ve seen it referenced in Stack Overflow comments.

CsvHelper as seen in Nuget Package Manager

Comparison

I not doing anything fancy here just creating CSV files with differing amounts of rows a few hundred times, and outputting the average time in milliseconds.

Results

Number of RowsFile Size1* Write1* Read2* Write2* Read
1008KB4300
75055KB8424
1,00073KB8436
4,300340KB14101423
10,000759KB23302754
100,0007.8MB173322243655
Results from comparison: * 1 = CsvHelper, 2 = my code

Analysis

I believe the biggest difference between the two solutions is the method used to read the files. The method used by my code is a lot faster for smaller files, but as you can see above, does slow down after 340KB for writing, and only 55KB for reading.

I did try with different numbers of iterations, thinking that maybe CsvHelper was taking a little longer to release the file, but that was not the case. On average, there was little difference from 200 iterations to 1,000 iterations.

Recommendations

In terms of best practice I would recommend reusing an existing library over writing your own, therefore I recommend CsvHelper.

In terms of speed, I would argue that CsvHelper is still the best option as it would take a large number of smaller files for it to really make a difference, where as a single large file can have a significant impact.

The comparison was only like for like, CsvHelper has more features, a good one being that it removes double quotes from string fields wrapped in them.

So, unless you have a need to include the code in your solution (like for auditing purposes), I recommend CsvHelper over using my code, and your code also.

CsvHelper Icon

Leave a Reply

Your email address will not be published. Required fields are marked *