1

Even though it is good to check performance of code in terms of algorithmic analysis and Big-Oh! notation i wanted to see how much it takes for the code to execute in my PC. I had initialized a List to 9999count and removed even elements out from the them. Sadly the timespan to execute this seems to be 0:0:0. Surprised by the result there must be something wrong in the way i time the execution. Could someone help me time the code correct?

        IList<int> source = new List<int>(100);
        for (int i = 0; i < 9999; i++)
        {
            source.Add(i);
        }

        TimeSpan startTime, duration;
        startTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;

        RemoveEven(ref source);
        duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(startTime);

        Console.WriteLine(duration.Milliseconds);
        Console.Read();

3 Answers 3

7

The most appropriate thing to use there would be Stopwatch - anything involving TimeSpan has nowhere near enough precision for this:

var watch = Stopwatch.StartNew();
// something to time
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);

However, a modern CPU is very fast, and it would not surprise me if it can remove them in that time. Normally, for timing, you need to repeat an operation a large number of times to get a reasonable measurement.

Aside: the ref in RemoveEven(ref source) is almost certainly not needed.

Sign up to request clarification or add additional context in comments.

2 Comments

So my code was correct but the tool used for measurement was wrong? So are classes passed always by reference in c#?
@Deeptechtons any variable/parameter/field to a class instance is a reference, so you are passing a reference. By adding a ref, you are passing a reference, by-reference - which is only useful if you reassign the list in the method.
4

In .Net 2.0 you can use the Stopwatch class

IList<int> source = new List<int>(100);
for (int i = 0; i < 9999; i++)
{
    source.Add(i);
}

Stopwatch watch = new Stopwatch();

watch.Start();
RemoveEven(ref source);
//watch.ElapsedMilliseconds contains the execution time in ms
watch.Stop()

1 Comment

the shorthand of var stopwatch = new Stopwatch(); stopwatch.Start(); is var stopwatch = Stopwatch.StartNew();
1

Adding to previous answers:

var sw = Stopwatch.StartNew();

// instructions to time

sw.Stop();

sw.ElapsedMilliseconds returns a long and has a resolution of:

1 millisecond = 1000000 nanoseconds

sw.Elapsed.TotalMilliseconds returns a double and has a resolution equal to the inverse of Stopwatch.Frequency. On my PC for example Stopwatch.Frequency has a value of 2939541 ticks per second, that gives sw.Elapsed.TotalMilliseconds a resolution of:

1/2939541 seconds = 3,401891655874165e-7 seconds = 340 nanoseconds

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.