ใช้คลาสนาฬิกาจับเวลาเพื่อวัดเวลาในการดำเนินการของเมธอดใน .NET -
Stopwatch s = Stopwatch.StartNew();
ตอนนี้ตั้งค่าฟังก์ชันและใช้คุณสมบัติ ElapsedMilliseconds เพื่อรับเวลาดำเนินการในหน่วยมิลลิวินาที -
s.ElapsedMilliseconds
ให้เราดูรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.IO;
using System.Diagnostics;
public class Demo {
public static void Main(string[] args) {
Stopwatch s = Stopwatch.StartNew();
display();
for (int index = 0; index < 5; index++) {
Console.WriteLine("Time taken: " + s.ElapsedMilliseconds + "ms");
}
s.Stop();
}
public static void display() {
Console.WriteLine("Demo function!");
}
} ผลลัพธ์
Demo function! Time taken: 15ms Time taken: 16ms Time taken: 16ms Time taken: 16ms Time taken: 16ms