.NET में किसी विधि के निष्पादन समय को मापने के लिए Stopwatch क्लास का उपयोग करें:
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