Pregunta

I'm currently finishing my A2 Computing coursework (UK Sixth Form), and as part of it I have to do a range of testing processes on my software.

As part of this, I need to be-able to measure efficiency, and while primitive and probably not useful in the real world, I was hoping to be able to measure how long a procedure takes to execute.

I have searched Google and haven't found any method that can do this so far.

How could I go about this?

Thanks,
C.

No hay solución correcta

Otros consejos

A very simple way to do this is to take a timestamp before you execute your method, and then see how long it has taken to complete. For example:

DateTime scriptStart = DateTime.Now;

SomeMethod();

DateTime scriptEnd = DateTime.Now;
TimeSpan scriptTime = scriptEnd - scriptStart;
int seconds = scriptTime.TotalSeconds;

You can use Visual Studio Profiling Tools to get performance metrics of your code. The profiler allows you to get an execution time and count calls of any methods without changing your source code. This article can serve as a starting point to explore capabilities of the profiler.

If you use C# and you need to get the execution time of one or few methods then you can use the Stopwatch class. See below an example of its use:

var stopwatch = Stopwatch.StartNew();
SomeMethod();
Console.WriteLine("Call SomeMethod {0} ms", stopwatch.ElapsedMilliseconds);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top