Reputation: 15
I have a project in which I've written code to calculate a correlation coefficient using two different formulas.
I'm supposed to calculate the time each formula takes to produce the output, and then compare those times to determine which is faster.
How can I do that? And can I do it in the same project, or do I have to separate the formulas each in different project?
Upvotes: 0
Views: 2088
Reputation: 30728
You can use StopWatch class.
Performance Tests: Precise Run Time Measurements with System.Diagnostics.Stopwatch
Upvotes: 0
Reputation: 30543
At the simplest level you could just output the current time to the immediate window in Visual Studio at the beginning and end of each of your functions and then compare those values.
If you add this before your function starts:
Debug.WriteLine("Function X started at: " + DateTime.Now.ToLongTimeString());
And this after your function completes:
Debug.WriteLine("Function X ended at: " + DateTime.Now.ToLongTimeString());
That will write to the immediate window. I'm assuming you're just running this in Visual Studio in debug mode rather than creating a separate executable and running that?
The stopwatch approaches described in the other answers is a nicer and more precise approach, the approach above is just a quick and dirty way of outputting the data at debug time.
This is an example of using the StopWatch class from the Microsoft MSDN documentation:
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
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);
}
}
You would just have to adjust the code above to run both functions and measure the time for each one to run.
The MSDN documentation mentioned above notes the following:
On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the Hardware Abstraction Layer (HAL), you can get different timing results on different processors. To specify processor affinity for a thread, use the ProcessThread.ProcessorAffinity method.
This article provides an example of how to see those issues and potential methods to deal with the accuracy issues. It begins:
Everybody who does performance optimization stumbles sooner or later over the Stopwatch class in the System.Diagnostics namespace. And everybody has noticed that the measurements of the same function on the same computer can differ 25% -30% in run time. This article shows how single threaded test programs must be designed to get an accuracy of 0.1% - 0.2% out of the Stopwatch class. With this accuracy, algorithms can be tested and compared.
If you need a very precise measurement then it is important to understand the concepts discussed in that document related to isolating a thread for your function to use and incorporating a warm up phase in the test.
Those points apply no matter which programmatic method you are using to record the times.
Upvotes: 1
Reputation: 3620
If you want to analyze code to find some long lasting methods and function try dotTrace from Jetbrains or some another profiling tool.
In dotTrace you can very quickly find all hot spots or potential bottlenecks.
Upvotes: 1
Reputation: 56727
You can measure the time a method takes to execute using the Stopwatch
class.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
CalculateStuff();
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
Upvotes: 2
Reputation: 6538
You can measure the time elapsed in a function with Stopwatch
.
https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx
Exemple from dotnetperls:
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
// Create new stopwatch.
Stopwatch stopwatch = new Stopwatch();
// Begin timing.
stopwatch.Start();
// Do something.
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(1);
}
// Stop timing.
stopwatch.Stop();
// Write result.
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
}
Upvotes: 4