我们来看到C#要如何计算程序码的执行时间呢 ~
有两种方法分别是 Stopwatch DateTime ~
学习目标: 计算程序码执行时间的实务
学习难度: ☆☆☆
Stopwatch (StartNew and Stop)
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class MainProgram
{
static void Main()
{
var stopWatch = Stopwatch.StartNew(); //启动Stopwatch
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("123"+"\n");
}
stopWatch.Stop(); //停止Stopwatch
Console.WriteLine(stopWatch.ElapsedMilliseconds); //印出执行时间(毫秒)
}
}
}
Stopwatch (GetTimestamp)
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class MainProgram
{
static void Main()
{
var start = Stopwatch.GetTimestamp(); //纪录时间(起点)
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("123"+"\n");
}
var end = Stopwatch.GetTimestamp(); //纪录时间(终点)
Console.WriteLine(end-start); //印出执行时间(毫秒)
}
}
}
DateTime
using System;
namespace ConsoleApp1
{
class MainProgram
{
static void Main()
{
DateTime start = DateTime.Now; //纪录时间(起点)
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("123"+"\n");
}
DateTime end = DateTime.Now; //纪录时间(终点)
TimeSpan timeSpan = (end - start);
Console.WriteLine(timeSpan.TotalMilliseconds); //印出执行时间(毫秒)
}
}
}
参考资料:
https://www.geeksforgeeks.org/how-to-calculate-the-code-execution-time-in-c-sharp/
>>: 【C#】Creational Patterns Simple Factory Mode
葛瑞部落格欢迎光顾 Windows Server 2019 语系更新手札 Windows设定 时间与...
今天结膜炎,看完医生整个白天都躺在床上眼睛痒得要死动不了,晚餐後好一点,写一点昨天缺漏的小东西 讲个...
既然可以将音档转为文字回传了,接下来我们可以尝试看看将收到的 Text message 转为音档去进...
今天来复习 cache 相关的知识! 首先可以参考一下这篇:Day.8 Cache 的基本原理 ca...
杂凑表(Hash Table)建立的方法 hash: 杂凑函式 add: 新增资料 search: ...