Skip to content
HammerheadShark666 edited this page Dec 2, 2022 · 2 revisions

This project demonstrates Benchmark .NET.

A benchmark is a measurement or set of measurements relating to the execution of some code. Benchmarks allow you to compare the relative performance of code as you begin making efforts to improve performance.

The process being benchmarked involves organisation records in a csv file being read and then saved to a database.

Process Details

The process reads records from 1 or more csv files and then saves them to a database table in batches.

The number of files and number of records saved in a batch can be changed to test different quantities of data and optimising the saving process.

Three different loops are benchmarked.

  • Standard for loop - for (int i = 0; i < files.Count; i++)

  • Parallel for loop - Parallel.ForEach

  • Parallel for async loop - Parallel.ForEachAsync

Results

10 files x 10000 records - saving 1 at a time

Method Mean Error
ForLoop 116.68 s NA
ParallelForLoop 40.12 s NA
ParallelForLoopAsync 52.17 s NA

10 files x 10000 records - saving 100 at a time

Method Mean Error
ForLoop 17.73 s NA
ParallelForLoop 10.25 s NA
ParallelForLoopAsync 10.91 s NA

10 files x 10000 records - saving 250 at a time

Method Mean Error
ForLoop 15.338 s NA
ParallelForLoop** 9.831 s NA
ParallelForLoopAsync 10.785 s NA

10 files x 10000 records - saving 500 at a time

Method Mean Error
ForLoop 15.39 s NA
ParallelForLoop 10.39 s NA
ParallelForLoopAsync 10.34 s NA

10 files x 10000 records - saving 1000 at a time

Method Mean Error
ForLoop 15.76 s NA
ParallelForLoop 12.29 s NA
ParallelForLoopAsync 10.94 s NA

100 files x 10000 records - saving 250 at a time

Method Mean Error
ForLoop 126.33 s NA
ParallelForLoop 104.19 s NA
ParallelForLoopAsync*** 79.09 s NA

Conclusion

**When processing 10 files x 10000 records the optimal process is to use the Parallel.ForEach loop and save 250 records at a time.

***When processing 100 files x 10000 records the optimal process is to use the Parallel.ForEachAsync loop and save 250 records at a time.