mercredi 22 avril 2015

Long function containing complex algorithm for higher performance

There are many questions already on the topic of lenghty functions and it's almost a never-ending story that people always degrade a function when it does not fit into the currently used screen anymore.

Examples include:

When is a function too long?

http://ift.tt/1DNj7kj

http://ift.tt/1cZKAGT

I will try my best to explain why my question is not a duplicate of anything I could find and why the answer is important to me, so please consider this before immediate down-voting.

Currently, I am working on a larger program that includes a few fairly complex algorithms, written in C++, for a hardware system. I would like to mention a couple of important points:

  • Every bit of performance improvement I can get is absolutely essential. It could be possible, though very unlikely, to strip down the performance for readability later, but that will only be clear after long testing on the hardware AFTER the program is fully functioning.
  • I or a person working directly with me will be the only ones ever to work on the code itself.

Now, in order to process some data I call a function that takes not too many inputs, runs through a complex algorithm, to finally leave with an even smaller amount of output values. I realized that the whole scope of the algorithm can become very large (a few hundred lines of code). Splitting it up into sensible functions is obviously possible, however, that introduces other difficulties:

  • Creating small functions for very small subtasks will make the algorithm jump out of the large function many times, which is indeed a performance drawback.
  • Creating a bit larger functions for the standard subtasks will create functions that would actually only be called a single time within the larger function, making it (in my point of view) unnecessary.
  • Anything in-between makes no sense as the data processing is highly interconnected and would finally decrease readability

Now, in this situation I personally don't mind a very large function, which indeed only does one single thing with an obvious output (no matter how complex the processing was). My question is now: Despite the often cited conventions of readability, re-usability, and testability, are there any more draw-backs (performance or otherwise) that I might be missing or could not find yet? E.g. Does compiler optimization get less effective, if functions are too long? Anything else?

Any answer is appreciated, happily specific to C++ and GCC. If I missed a good topic to this already, I would of course be more than happy to have a link to that location.

Aucun commentaire:

Enregistrer un commentaire