All is in the title, and I wonder if it is a good practice or not :
[TestMethod]
public void Compute_Array_In_Less_1_Second()
{
Stopwatch watcher = new Stopwatch();
int[] A = Enumerable.Range(0, 50000).Select(i => GetRandomNumber(MIN_INT, MAX_INT)).ToArray();
watcher.Start();
int[] R = Program.MethodThatReturnsAnArray(A);
watcher.Stop();
if (watcher.ElapsedMilliseconds > 1000)
Assert.Fail("The method runs in more 1 second");
}
You should be using the appropriate mechanisms provided by your testing framework, like: http://nunit.org/index.php?p=timeout&r=2.5
But note that you don’t want to be doing this everywhere ( to measure performance) but to test that the unit is actually finishing in time or times out if it needs to do that.