DateTime时间转换引发的耗时问题
从本地文件读取数据,然后写入内存,查出的耗时问题,直接上代码:
static void Main(string[] args) { var datetime = DateTime.Parse("2020-10-10 05:05:05"); var listStr = new List<string>(); var listLong = new List<long>(); for (int i = 0; i < 10000 * 1000; i++) { listStr.Add(datetime.ToString("yyyy-MM-dd HH:mm:ss")); listLong.Add(datetime.ToBinary()); } Stopwatch sw = new Stopwatch(); sw.Start(); foreach (var item in listStr) { DateTime.Parse(item); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 1000d); sw.Restart(); foreach (var item in listLong) { DateTime.FromBinary(item); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 1000d); }
结果(单位秒):
1千万的数据转换耗时相差12秒,惊人,以后注意!