C#中文件写的性能测试

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43145361/article/details/100581242

测试目的

用于测试C#中几种代码写的文件写入的性能。

测试机器

机器2:桌面 i5 9600K(6T6C),32GB DDR4 2166,512GB NVME,更多请参见机器详细信息

测试方式

目前只测试了三种方式:

  • 方式1:调用File.WriteBytes(byte[]) 直接调用这个函数将数据写入。
  • 方式2:FileStream.Write(byte[]) 直接将所有数据写入文件。
  • 方式3:FileStream.Write(byte[]) 分块将数据分块后写入文件。

测试代码

参见Gitee 代码地址:https://gitee.com/hwaust/CSharpIOPerformanceTest

测试方法

每种情况每运行2次,然后开始记录数据,共执行20次后,取20次结果的中位值(排序后取第10位的值)。

初测结果

100MB数据写入本地文件测试
方式1:1.15 GB/s
方式2:1.30 GB/s
方式3:4.66 GB/s

结果分析

根据以上结果,发现数据分块后能够显著提高写入性能。于是追加了一个测试,数据如下所示。

  数据量  速度
  50 MB  4.29 GB/s.
 100 MB  4.67 GB/s.
 150 MB  4.28 GB/s.
 200 MB  1.60 GB/s.
 300 MB  1.59 GB/s.
 500 MB  1.51 GB/s.
1000 MB  0.57 GB/s.

由上可见,在200MB以下的文件写入的速度可达4GB/s以上,性能非常可观。

附:原始数据

第1次测试数据

C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 100 -a 0
Test Algorithm: FileWriteTest
....................
Writing: 88.7623 ms. Speed: 1.15 GB/s.
Writing 90.757  90.729  88.763  85.771  85.770  87.765  88.763  87.765  85.771  88.763  88.762  87.765  89.760  87.766  88.762  90.758  88.762  94.747  88.762  86.768

C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 100 -a 1
Test Algorithm: FileStreamWriteTest
....................
Writing: 78.7892 ms. Speed: 1.30 GB/s.
Writing 81.781  99.733  79.787  80.784  79.787  79.787  78.789  76.795  76.795  76.795  77.792  76.795  79.818  78.758  79.786  77.792  78.789  78.789  76.795  79.786
C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 100 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 21.9682 ms. Speed: 4.66 GB/s.
Writing 23.931  21.942  21.914  21.941  21.969  22.911  21.968  20.918  22.967  21.913  21.968  21.914  23.964  20.944  20.943  22.966  21.915  21.968  22.938  22.938

C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 200 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 77.7915 ms. Speed: 2.63 GB/s.
Writing 46.878  44.877  46.851  44.880  45.904  44.880  49.840  44.907  45.877  45.880  77.792  129.624  127.685 129.626 127.686 128.653 127.634 128.683 126.634 128.656

第2次追加测试数据

用于对FileStream在不同数据量下进行测试。

C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 50 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 11.9412 ms. Speed: 4.29 GB/s.
Writing 13.933  10.971  10.942  11.995  12.938  11.000  10.942  11.967  11.996  12.965  10.971  11.941   11.995  10.970  10.972  11.970  10.970  10.943  12.966  11.150

C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 100 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 21.944 ms. Speed: 4.67 GB/s.
Writing 23.905  22.966  20.917  21.969  22.912  20.971  20.946  20.943  21.914  20.972  19.976  20.947   22.934  22.912  21.969  20.944  21.941  22.939  22.936  21.944

C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 150 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 35.903 ms. Speed: 4.28 GB/s.
Writing 47.380  37.871  40.891  33.908  33.910  34.934  34.904  35.905  32.915  33.941  32.905  34.880   32.912  35.903  33.909  60.838  97.738  97.739  372.094 610.278
C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 200 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 127.6579 ms. Speed: 1.60 GB/s.
Writing 46.902  45.875  47.874  46.875  47.844  45.876  50.866  45.905  46.874  101.395 129.653 128.240 131.649 128.654 129.217 129.227 130.063 127.658 132.645 127.658
C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 300 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 193.1021 ms. Speed: 1.59 GB/s.
Writing 97.738  94.746  78.791  78.788  71.809  135.636 191.488 195.477 203.785 193.102 202.166 195.085 197.472 199.466 199.467 190.490 191.145 192.081 193.826 196.686
C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 500 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 338.9543 ms. Speed: 1.51 GB/s.
Writing 127.631 123.670 415.887 329.191 328.777 332.516 329.119 323.163 2555.571        872.774 338.954 316.669 632.393 883.176 1035.962        835.116 906.575 449.639 328.664 334.106

C:\VSProjects\IOPerformanceTest\bin\Debug>IOPerformanceTest.exe -t 20 -m 1000 -a 2
Test Algorithm: FileStreamWriteTestSmall
....................
Writing: 1783.2289 ms. Speed: 0.57 GB/s.
Writing 339.065 642.281 650.260 663.225 674.196 668.219 1606.695        1785.227        2045.531        1799.182        1808.167        1836.091        1796.190        1783.229        1782.232        1785.224        1774.253        1810.156       1776.260        1801.169

猜你喜欢

转载自blog.csdn.net/weixin_43145361/article/details/100581242