Click here to Skip to main content
11,920,641 members (54,784 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Stopwatch
Why the access time of class members is high?

I created a simple class like this:
class A {
   public SimpleClass()
   public void doSth()

And by using Stopwatch class(in System.Diagnostics) I measured the
time of accessing doSth():


and I was shocked by the result!

although the method does nothing,the Access time was around: 0.04 millisecond(s)!
While measuring the time interval of much more calculations (hundreds of multiplication of float numbers) takes about 0.002 ms!

1.Is there any way to reduce this time?
2.Have I made any mistakes?
(I would be happy to know further info. on this topic Smile | :) )
Posted 24-Sep-12 20:08pm
Edited 1-Oct-12 14:12pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Do your test 1000 times and recheck your results, you should see a difference.

A single call delay can be attributed to startup time of the CLR, JIT compilation etc.
CyrusT 25-Sep-12 1:58am
@Mehdi: I did what you said 100,000 times( in a for loop)
and yet the result had no difference :(

besides I tested a Queue's Enqueue() and Dequeue() methods too,and I noticed that the running time of these methods are even worse! (about 0.2 ms! ,again in a 100,000 times loop )
Mehdi Gholam 25-Sep-12 3:05am
The following executes in 2ms on my machine (0.02ns per call):
DateTime dt = DateTime.Now;
pp p = new pp();
for (int i = 0; i < 100000; i++)
MessageBox.Show("" + DateTime.Now.Subtract(dt).TotalMilliseconds);
OK, I explained how to fix the timing the way excluding the extra time for JIT compilation, which otherwise distorts the timing results.
Considering that, the code you show above is also not quite correct.
Please see my solution.
[OP commented:]

You're right!
I made mistake at my test!
when running the test for 100,000 times,I didnt reset the StopWatch timer in order to calculate each "for step" calculation time.
My 5, but I would elaborate on JIT -- this is the main contribution of the number of timed iteration is not big enough, and way too many developers don't understand it.
Mehdi Gholam 2-Oct-12 0:43am
Cheers Sergey!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Not resetting StopWatch is not the only mistake.

One much more general thing is: you should move JIT time out of the equation. I also should note that your repetition time is very low, so the JIT compilation of the method will be a serious factor. The method is usually JIT-compiled before you call it for the first time; and the compilation time is way greater then the call itself.

This is the correct way:

class A {
   // no need in "public SimpleClass() {}"
   internal void doSth() {   } // no need in public: don't provide more access than really required

A myInstance = new A();
myInstance.doSth(); // when this has executed, the method is certainly JIT-compiled, but does not have to be compiled sooner

// now, time it!

That's it. Now it will work correctly.

Espen Harlinn 1-Oct-12 18:12pm
Excellent :-D
Thank you very much, Espen.
Mehdi Gholam 2-Oct-12 0:43am
Thank you, Mehdi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 415
1 phil.o 170
2 Dave Kreskowiak 155
3 KrunalRohit 134
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,815
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100