Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed of finite state machines - OO vs procedural

Hey all,
I am designing a program that will accept from input a series of tokens and feed them to a finite state machine which I have designed. I have designed a test finite state machine in object-oriented style, with structs for the machine itself and transitions, etc. but the application I am writing is one where speed is very important.

So far, using the machine, adding new states and the like, has proven to be easy and not very complicated. It is easy to comprehend and leaving for a month and coming back to the code will not be very disorienting. However, I am not sure what the speed trade off is with the current OO approach. Will the allocation of the objects, storage of data, etc. take away much of the speed that would be had by using a bunch of labels and goto statements?

like image 236
SuperSoaper Avatar asked Dec 28 '22 06:12

SuperSoaper


2 Answers

Rather than thinking about it in terms of OO being slower than functional or procedural programming think about it in terms of operations. Calling a function, branching, fetching, storing etc... all take some time and to get an idea of the performance of each solution you'd need to tally up how much of each of these you'll need to do.

The best way to do this is to use your OO test solution and see if it's fast enough. If not then profile it, work out which branches or stores are costing you the most and see if you can avoid or streamline them. Slowly re-architect your solution until it meets the performance goals you're after. It may be that in some cases you'll have to adopt a more functional or procedural style.

Lastly if you do code up a single function that consists of hundreds of stack variables and goto you've done it wrong. Code must always be readable and maintainable.

Extra thought: Can you just spend another $5,000 on better hardware to avoid $50,000 in development costs optimising?

like image 97
Daniel Avatar answered Dec 31 '22 11:12

Daniel


I would prefer well structured organized, maintainable, comprehendable (even after leaving for a month, thats quite a quality) code instead of a "bunch of labels and goto statements." Being said that, I would also run a profiler on the code to detect the bottlenecks when doing a speed analysis.

like image 22
Arun Avatar answered Dec 31 '22 12:12

Arun