C#写单个较⼤⽂件与写多个较⼩⽂件的性能⽐较
我们对⽂件写操作:
1.写多个⽂件,每个⽂件较⼩
2.直接写⼀个较⼤的⽂件
⽐较这两种⽅式的性能。
⼀般来说,读写⽂件可以适当缓冲区⼤⼀点,尽量减少多次读写⽂件操作。
写⼀个⽐较⼤的⽂件 ⽐ 写多个⼩⽂件 花费的时间要少很多。
同理:TCP通信中,可以适当减少发送接收次数,⽽每次发送和接收的缓冲区可以适当⼤⼀些,来优化通信性能。
我们⽤实际程序测试【写⽂件】:
⼀个写⼊200个1KB的⽂件【累计⽂件⼤⼩为200KB】
直接写⼊⼀个2MB的⽂件【⽂件⼤⼩为2048KB,⼤约是200KB的⼗倍】
visual  studio 2019新建控制台应⽤程序WriteMultipleFileDemo,输⼊测试代码,如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WriteMultipleFileDemo
{
class Program
{
static void Main(string[] args)
{
//写多个⽂件 200个1KB⽂件写1个2MB所花费的时间⽐较
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 20; i++)
{
//52*20=1040字节,约1KB
sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
}
string content = sb.ToString();
Console.WriteLine("开始测试【写200个1KB⽂件】与【写⼀个2MB的⽂件】所花费的时间⽐较......");
Task taskMultiple = Task.Run(() =>
{
WriteFileUseTime(WriteMultipleFile, nameof(WriteMultipleFile), content);
});
Task taskSingle = Task.Run(() =>
{
WriteFileUseTime(WriteSingleFile, nameof(WriteSingleFile), content);
});
Task.WaitAll(taskMultiple, taskSingle);
Console.WriteLine("写⼀个⽐较⼤的⽂件⽐写多个⼩⽂件花费的时间要少很多。\n同理:TCP通信中,可以适当减少发送和接收次数,⽽每次发送和接收的缓            Console.ReadLine();
}
/// <summary>
/// 写⽂件⽤时
/// 写⽂件⽤时
/// </summary>
/// <param name="actionWriteFile"></param>
/// <param name="methodName"></param>
/// <param name="content"></param>
static void WriteFileUseTime(Action<string> actionWriteFile, string methodName, string content)
{
Stopwatch stopwatch = Stopwatch.StartNew();
actionWriteFile(content);
Console.WriteLine($"【{methodName.PadRight(18)}】,写⽂件结束,⽤时【{stopwatch.Elapsed.TotalMilliseconds}】ms");        }
/// <summary>
/// 写100个1KB的⽂件
/// </summary>
/// <param name="content"></param>
static void WriteMultipleFile(string content)
{
string pathDirectory = AppDomain.CurrentDomain.BaseDirectory + "TestMultiple\\";
if (!Directory.Exists(pathDirectory))
{
大文件发送
Directory.CreateDirectory(pathDirectory);
}
for (int i = 1; i <= 200; i++)
{
string path = pathDirectory + DateTime.Now.ToString("yyyyMMdd_") + $"{i.ToString("D3")}.txt";
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
{
streamWriter.WriteLine(content);
streamWriter.Close();
fileStream.Close();
}
}
}
}
/// <summary>
/// 写1024⾏1KB的单个⽂件,也就是1MB
/// </summary>
/// <param name="content"></param>
static void WriteSingleFile(string content)
{
string pathDirectory = AppDomain.CurrentDomain.BaseDirectory + "TestSingle\\";
if (!Directory.Exists(pathDirectory))
{
Directory.CreateDirectory(pathDirectory);
}
string path = pathDirectory + DateTime.Now.ToString("yyyyMMdd") + ".txt";
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
{
for (int i = 0; i < 2048; i++)
{
streamWriter.WriteLine(content);
}
streamWriter.Close();
fileStream.Close();
}
}
}
}
}
测试结果如图: