When it comes to increasing performance, you want to make sure that what you intend to improve is actually the cause of the bottleneck. I would start by profiling the performance of the various pieces in my code and finding which piece introduces the greatest bottleneck.
At first blush, I see three main components of this process that are worth profiling:
- Loop through the entire contents of the file (this may or may not include File IO considerations, which you may wish to test separately).
- Check
String.Contains()
for each line in the file.
- "Do Stuff" when there is a match
While there are great commercial profilers out there, you can start by using a simple timer (like System.Diagnostics.Stopwatch
) in your code, or, if the process is really long, simply use watch to measure time.
Measure the time of each of the following.
- Measure the time it takes simply to look through the entire file without doing anything else. This isolates your looping and IO code so you can see how well it performs.
- Next, add only the string comparison (
String.Contains
) and measure the total time it takes with this added.
- Lastly, add in the "do stuff" code and measure the total time with this added.
You can now compare the cost of adding in each component to figure out how expensive that component is and whether or not you need to improve it. For example, let's say you recorded the following times:
Test Total Time Cost (Difference)
=============================================
Do Nothing 0s 0s
Loop Only 100s 100s
Add Comparison 105s 5s
Add Do Stuff 130s 25s
Looking at those (fake) numbers, the most expensive part of the process is the loop and IO code, so that's where I would begin to try to increase performance. Since the "Do Stuff" part added 25 seconds to the overall execution time, I would take a look at that code next to see if there was anything I could improve. Lastly, I would look at string comparison.
Of course, the time measurements I provided are fictitious, but you can apply the same steps to your performance profiling in your application. The key is to identify the greatest costs and try to reduce those first.