开发者

Time required for a process to complete

I am new to C# world. I am attempting to calculate time taken by a algorithum for the purpose of comparison. Following code measures the elapsed time from when a subroutine is called until the subroutine returns to the main program.This example is taken from "Data structures through C#" by Michael McMillan. After running this program the output is Time=0, which is incorrect. The program appears to be logically correct. Can anybody help me. Following is the code

 using System;
us开发者_如何学编程ing System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chap1
{
    class chap1
    {
        static void Main()
        {
            int[] nums = new int[100000];
            BuildArray(nums);
            Timing tObj = new Timing();
            tObj.startTime();
            DisplayNums(nums);
            tObj.stopTime();
            Console.WriteLine("Time: " + tObj.result().TotalSeconds);
            Console.WriteLine("Start Time: " + tObj.startTime().TotalSeconds);
            Console.WriteLine("Duration : " + tObj.result().TotalSeconds);
            Console.ReadKey();
        }
        static void BuildArray(int[] arr)
        {
            for (int i = 0; i <= 99999; i++)
                arr[i] = i;
        }
        static void DisplayNums(int[] arr)
        {
            for (int i = 0; i <= arr.GetUpperBound(0); i++)
                Console.WriteLine(arr[i]);
        }
    }
class Timing
    {
        TimeSpan StartTiming;
        TimeSpan duration;
        public Timing()
        {
            StartTiming = new TimeSpan(0);
            duration = new TimeSpan(0);
        }
        public TimeSpan startTime()
        {
            GC.Collect();


     GC.WaitForPendingFinalizers();
            StartTiming = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
            return StartTiming;
        }
        public void stopTime()
        {
            duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(StartTiming);

        }
        public TimeSpan result()
        {
            return duration;
        }
    }
}


The Stopwatch class is designed for this.

UserProcessorTime doesn't begin to have the resolution necessary to measure counting to 100000 in a for loop. Your WriteLine calls won't be included in user time as they are I/O time. Your code might not be running on thread 0. User time isn't updated except at context switches. When you print startTime, you're changing the stored value. There are probably some other things that can go wrong I haven't thought of.

I strongly suggest you use the Stopwatch class which takes advantage of the CPU's performance counters.


You don't use the Timing class anywhere in your main function and I don't see where you print the time either. Is this the EXACT code you're running?

Update per new code:

Don't run it in debug mode... build your release version and then run the executable manually: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/3f10a46a-ba03-4f5a-9d1f-272a348d660c/

I tested your code and it worked fine when running the release version, but when I was running it in the debugger it was not working properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜