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 to ask is that is there any way in C# through which I can find the execution/running time between two points in a program. For example
//Some Code
//Code to start recording the time.
hideDataUsingAlgorithm();
//Code to stop recording and get the time of execution of the above function.
I want to do this to show the difference between simple(less time consuming) and more efficient but time consuming algorithms (using same data and same image). I have around 10 different algorithms for Color and GrayScale Images.
There is no multithreading so that wont be a problem. Theres simply just one main Thread.
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());
System.Environment.TickCount and the System.Diagnostics.Stopwatch class are two that work well for finer resolution and straightforward usage.
See Also:
You can use StopWatch
class:
var timer = System.Diagnostics.StopWatch.StartNew();
hideDataUsingAlgorithm();
timer.Stop();
var elapsed = timer.ElapsedMilliseconds;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With