Time elapse computation in milliseconds C#
c#, time, timer
Solution
using System.Diagnostics;
//...
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < N_ITER; i++) {
// cpu intensive sequence
}
stopwatch.Stop();
elapsed_time = stopwatch.ElapsedMilliseconds;
Problem
I need to time the execution of a code sequence written in C#. Using DateTime.Now I get incorrect values for the millisecond field. For example: ``` int start_time, elapsed_time; start_time = DateTime.Now.Millisecond; for(int i = 0; i < N_ITER; i++) { // cpu intensive sequence } elapsed_time = DateTime.Now.Millisecond - start_time; ``` elapsed_time gives negative values. How may I replace DateTime in order to obtain the actual value of the elapsed time?