Reputation: 1144
If I measure the running time of a method, shouldn't it be same if I give the same input data? I used the Stopwatch:
Stopwatch sw = new Stopwatch();
sw.Start();
//code here
sw.Stop();
label3.Text = "Running Time:"+sw.Elapsed.TotalMilliseconds;
Upvotes: 2
Views: 441
Reputation: 6619
No, because other programs on your computer are also using the CPU. So even if its the same code ,the environment on your computer could change. It could be a lot of network traffic, some background tasks and so on.
Upvotes: 2
Reputation: 861
No, because there are several other occurences running on your computer which are influencing your stopwatch time.
Just a small list:
Such a performance comparision is usually called with the same method, same parameters over several times in a loop because of all those influences.
Upvotes: 1
Reputation: 39049
It's hard to tell without the code.
You could see differences due to several reasons - this is by no means a complete list:
Upvotes: 7
Reputation: 12884
It depends on the number of Processes currently running on your processor. If you have a heavy load on processor then i may take some high time. But there wont be much difference.
Upvotes: 1
Reputation: 81724
No, not at all. Your program is running on a complex system -- a computer -- which has many other processes running, interrupting your program to borrow the CPU at odd moments. And even within your program, there are other threads which may steal cycles here and there. Benchmarks always must be statistical -- measure many times, and take the average.
Upvotes: 4
Reputation: 5408
So much is going on both within the code and on the computer. You will be lucky to get the same results every time.
Upvotes: 1
Reputation: 81700
No.
There are so many factors involving the time taken:
Upvotes: 2
Reputation: 62265
It never could be exactly the same. There will be always variations. On your computer runs not only your program, there is OS and other programs, services too.
Important is do not see too big difference between times. That is.
Upvotes: 1