C# Why the running time of a method is not the same?

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

Answers (8)

Simon Edström
Simon Edström

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

Kelon
Kelon

Reputation: 861

No, because there are several other occurences running on your computer which are influencing your stopwatch time.

Just a small list:

  • Another process on your computer needs CPU time and your process/thread is less important then this process
  • Same with all hardware calls, like writing to your harddrive, painting graphics etc
  • You're calling this method/class the first time and the JIT-Compiler runs before executing it
  • Your heap was eaten up and your application calls for new memory or else, the garbage collector collects and frees all obsolete data
  • your processor started a power reducation and runs at a lower rate
  • and more more more...

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

zmbq
zmbq

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:

  1. Computer is busy - your computer is doing something else which means it gives your code less resources (CPU time).
  2. I/O - you do some sort of I/O that can take a little while or a long time.
  3. Jitting - the first time you run a method, the JIT compiler compiles it, and it takes time. The second time it takes a lot less.
  4. Cache hits and misses - if you measure the same code twice, the second time may be faster because the data used by the code is already in the cache.

Upvotes: 7

Kishore Kumar
Kishore Kumar

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

Ernest Friedman-Hill
Ernest Friedman-Hill

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

sealz
sealz

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

Aliostad
Aliostad

Reputation: 81700

No.

There are so many factors involving the time taken:

  • Other processes running
  • Resources available
  • Other threads running in your process fighting for same resources
  • If GC kicks in at the time of running the method
  • Hardware of your machine
  • ....

Upvotes: 2

Tigran
Tigran

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

Related Questions