I’m still a derp, but much less of a derp about programming that I was. While I started trying to learn things serially, filling out a firm grounding in each area before proceeding to the next in an order determined by the author of the book I was reading, I’ve taken to reading and trying out whatever I see that takes my fancy, occasionally backtracking to where I started, to build my knowledge along a branching-tree-like fashion.
While regex is largely unrelated to what I’m working on now I began reading up on how it works with C# and was immediately confused as to why an instance was being created for something that is going to do one simple task, after several hours of continued reading spaced out over a few weeks, I came across MSDN’s Best Practices for Regular Expressions which highlights that, as:
- Each time the regex engine is invoked, it goes through all the creation gubbins, converts the regular expression to binary that can be compared to the string you’re running it against.
- If you were to repeatedly evoke this, say, in a loop, you would be wasting oh so much performance as it goes through this for each input string.
In short, something that seems to the user to be simple (Not that being able to count every sentence in a book in a few seconds is easy.) have a considerable overhead in comparison to their useful work.
Aaand then I learned of ways to make things much more efficient, broken down into Static Instance and Pre-compiled DLL.
As the regex needs to load itself into memory before working but it has no variables in it’s class, you can create a static instance and each time it is called, it will be as unchanged as when it was first created. This means that, after the first time the instance is called, it can be re-used without any of the initial overhead.
Secondarily, if you have a regex you know you will be using a lot, you can shift it’s creation to pre-compiled binary code in DLL form, so the overhead is no longer wasted each time the program runs, but has already been created and is loaded into memory in a form that is ready to run straight away.
So, while regex doesn’t relate to my project, it is a good example of when a static instance of a fuction is actually useful and opens me up to a small area of the possibilities of compiling functions as binary ahead of time, which wouldn’t make much sense for most instances. Most of all, I was engaged with what I learned.
Now all I have to do is study for a year, only to come back, read this, and have no idea what I was talking about. That’s the learning process, people.