Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i calculate the speed the method is call using DateTime.Now?

Tags:

c#

.net

winforms

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.

like image 650
Daniel Lip Avatar asked Nov 30 '25 12:11

Daniel Lip


2 Answers

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);
like image 102
csharpwinphonexaml Avatar answered Dec 02 '25 02:12

csharpwinphonexaml


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.

like image 38
Eldar Avatar answered Dec 02 '25 00:12

Eldar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!