Find Execution time of a Method

后端 未结 5 1118
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 03:19

I am making an Image Steganography project for my college. I have finished the project and have kept several different algorithms for hiding data in images.

What I want

5条回答
  •  既然无缘
    2021-02-06 04:03

    This is a useful extension method for Stopwatch:

    public static class StopwatchExt
    {
        public static string GetTimeString(this Stopwatch stopwatch, int numberofDigits = 1)
        {
            double time = stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
            if (time > 1)
                return Math.Round(time, numberofDigits) + " s";
            if (time > 1e-3)
                return Math.Round(1e3 * time, numberofDigits) + " ms";
            if (time > 1e-6)
                return Math.Round(1e6 * time, numberofDigits) + " µs";
            if (time > 1e-9)
                return Math.Round(1e9 * time, numberofDigits) + " ns";
            return stopwatch.ElapsedTicks + " ticks";
        }
    }
    

    Use it like this:

    Stopwatch stopwatch = Stopwatch.StartNew();
    //Call your method here
    stopwatch.Stop();
    Console.WriteLine(stopwatch.GetTimeString());
    

提交回复
热议问题