2

I will give the top of the method code since the whole code is long and is not needed in this case.

static int tickCountLength = 1000;
        int[] tickCounts = new int[tickCountLength];
        int numberOfTicks = 0;
        TimeSpan dtn;

        void DoCaptureRenderTarget(Device device, string hook)
        {
            //Environment.TickCount-->array

            tickCounts[numberOfTicks] = Environment.TickCount;
            numberOfTicks++;

            DateTime start1 = DateTime.Now;
            DateTime end1 = DateTime.Now;
            this.DebugMessage("Capture Time Testing " + (end1 - start1).ToString());
            dtn = end1 - start1;
            if (dtn.Seconds > 0)
            {
                string fffff = "fgf";
            }
            if (numberOfTicks >= tickCountLength)
            {

                StreamWriter w = new StreamWriter(@"c:\Milliseconds\TickCount.txt", true);
                foreach (int tick in tickCounts)
                {
                    w.WriteLine("TickCount === > " + tick.ToString());

                }
                w.Close();
                numberOfTicks = 0;
            }

The method is called each time every 1ms or 20ms not sure the real time. So i added this part trying to calculate:

DateTime start1 = DateTime.Now;
            DateTime end1 = DateTime.Now;
            this.DebugMessage("Capture Time Testing " + (end1 - start1).ToString());
            dtn = end1 - start1;
            if (dtn.Seconds > 0)
            {
                string fffff = "fgf";
            }

But this is wrong for sure the time between end1 and start1 is 00:00:00:00 How can i calculate by milliseconds the time the methos icalling each time ?

Maybe : Using DateTime.Now.Millisecond each time and manually subtracting from the previous one (by looking at it) ? Not sure how to do it.

user1196715
  • 583
  • 2
  • 6
  • 14
  • 6
    Use [`StopWatch`](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.100%29.aspx) to measure elapsed times. – Tim Schmelter Apr 28 '15 at 09:11
  • Try to use StopWatch https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx – csharpwinphonexaml Apr 28 '15 at 09:11
  • possible duplicate of [Exact time measurement for performance testing](http://stackoverflow.com/questions/969290/exact-time-measurement-for-performance-testing) – Anthony Apr 28 '15 at 14:05

5 Answers5

3

Use StopWatch

Example:

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);// or whatever code you want to calculate elapsed time.
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

// Format and display the TimeSpan value. 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63
2

Stopwatch is what you need. Here a simple example:

Stopwatch sw= new Stopwatch();
sw.Start();
// your methods to be measured
sw.Stop();
TimeSpan ts = stopWatch.Elapsed;

//Format 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);

Be aware though that stopwatch may have issues with a multi-threaded async application, since it can only measure the speed on one core. This issue may cause some desynchronization and give you some incorrect timings. Depends on your application, you may be better off with DateTime.UtcNow but it is not as precise as Stopwatch on a single thread. (from what I know)

Some further reading:

Exact time measurement

Multithreaded stopwatch?

Elapsed ticks msdn

Measuring with stopwatch

Community
  • 1
  • 1
Jose Luis
  • 3,307
  • 3
  • 36
  • 53
2

The problem is you have no calls and no logic between the

DateTime start1 = DateTime.Now;

and

DateTime end1 = DateTime.Now;

So there is noo surprise that elapsed time is quite small, hard to detect. Put the

DateTime start1 = DateTime.Now;

before the logic.

static int tickCountLength = 1000;
    int[] tickCounts = new int[tickCountLength];
    int numberOfTicks = 0;
    TimeSpan dtn;

    void DoCaptureRenderTarget(Device device, string hook)
    {
        //Environment.TickCount-->array
        DateTime start1 = DateTime.Now;
        tickCounts[numberOfTicks] = Environment.TickCount;
        numberOfTicks++;

        DateTime end1 = DateTime.Now;
        this.DebugMessage("Capture Time Testing " + (end1 - start1).ToString());
        dtn = end1 - start1;
        if (dtn.TotalMilliseconds > 0)
        {
            string fffff = "fgf";
        }
        if (numberOfTicks >= tickCountLength)
        {

            StreamWriter w = new StreamWriter(@"c:\Milliseconds\TickCount.txt", true);
            foreach (int tick in tickCounts)
            {
                w.WriteLine("TickCount === > " + tick.ToString());

            }
            w.Close();
            numberOfTicks = 0;
        }

I don't know exactly what logic inside your method you want to be measured in time.

Eldar
  • 104
  • 6
1

You can use the stopwatch like this:

var watch = Stopwatch.StartNew();
// your code for which you want to measure time comes here
watch.Stop();
var elapsedMillisecond = watch.ElapsedMilliseconds;

You can also use the Environment.TickCount Property

Here is a good read given by John Chapman's blog post about DateTime.Now precision if you want to know why DateTime.Now is not recommended and why.

Also read this interesting article: The Darkness Behind DateTime.Now

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Okay, first of all, you are better off using Environment.TickCount rather than DateTime.Now, especially when measuring intervals.

Secondly, when calling DateTime.Now (or Environment.TickCount for that matter) you actually get the DateTime object for when you called the Now get method. So, the results you'll get if you're placing these methods one after the other is the same.

I didn't really understand what you're trying to measure, but a correct way to measure time in C# would probably be

int start = Environment.TickCount;
FunctionToMeasure();
int end = Environment.TickCount;
int diffMS = end - start; //This is the measured time of the function.

If you're measuring the time it takes for the actual DateTime.Now function, it is 0ms.

Hopefully I helped :)!

B.W.
  • 92
  • 10