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
- Output Data to CSV – old code for writing to CSV, but I still use it occasionally
- Working with CSV Files Part 1 – Parsing to a Class
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.
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 Rows | File Size | 1* Write | 1* Read | 2* Write | 2* Read |
---|---|---|---|---|---|
100 | 8KB | 4 | 3 | 0 | 0 |
750 | 55KB | 8 | 4 | 2 | 4 |
1,000 | 73KB | 8 | 4 | 3 | 6 |
4,300 | 340KB | 14 | 10 | 14 | 23 |
10,000 | 759KB | 23 | 30 | 27 | 54 |
100,000 | 7.8MB | 173 | 322 | 243 | 655 |
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.