I\'m trying to determine which approach to removing a string is the fastest.
I simply get the start and end time a
There's a lot of activity going on under the hood of the OS, which is very likely to mess up your time measurements. In order to improve the accuracy of the measured times you should execute the tests multiple times and remove the lowest and the greatest times from the final results. This approach will exclude most of the external factors that may influence your code's execution time and will serve you well in most cases. The Stopwatch class is used to measure time as it is much more accurate than using DateTime.
You can easily make a test class to automate this. I have published one such class in my blog. It can compare the performance of two C# code fragments/algorithms. All you have to do is to override the Method1 and Method2 methods putting there the code fragments you want to test and use the test class like this:
Test test = new CustomTest();
Console.WriteLine("\n==============================");
Console.WriteLine(test.Execute());
Console.WriteLine("==============================\n");
Three simple notes:
Use System.Diagnostics.Stopwatch.
Don't profile your code on the same input one million times. Try to find your expected distribution of inputs and profile on that. That is profile on real-world input, not laboratory input.
Run the Clean
method once before entering the profiling loop to eliminate JITting time. Sometimes this is important.
Of these, notes 1. and 2. are by far the most important.
Your profiling results are meaningless if you are not using a high-resolution timer. Note that we don't time Usain Bolt using a water clock.
Your profiling results are meaningless if you are not testing on real-world input. Note that crash tests crash cars head on into other cars at 35 MPH, not into walls made of marshmellows at 5 MPH.
Thus:
// expectedInput is string[1000000]
// populate expectedInput with real-world input
Clean(expectedInput[0]);
Stopwatch sw = new Stopwatch();
sw.Restart(); //So you dont have to call sw.Reset()
for (int i = 0; i < 1000000; i++) {
string t = Clean(expectedInput[i]);
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
One complex note:
If you really need to do profiling, get a profiler like ANTS.
If you're only worried about testing it in your own code...use a System.Diagnostics.Stopwatch
I usually prefer breaking this kind of thing out of my code and using a true Profiler like RedGate's Performance Profiler
You can also use log4net, simply add log.Info("Test") where you wanna track stuff and you will find all your info and the time that line was executed in a txt file, for example: 2018-12-13 10:02:16,704 [9] INFO Hms.Domain.Concrete.RoomsRepository [(null)] - Test
Read more about it here: https://www.infoworld.com/article/3120909/application-development/how-to-work-with-log4net-in-c.html
You can use the Stopwatch class.
The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time.
var sw = new Stopwatch();
sw.Start();
// ...
sw.Stop();
You may have to apply some statistical techniques here to iron out the variance. Try running the same piece of code 1000 times and then take the average time, and compare that. Simulations usually employ some sort of methods to 'clean up' the numbers, and this is one of those.