Effects of very large functions in a C program?

Ignoring modularity and readability, what are the performance implications of having large functions relative to many split functions? (C-language in general).

+3


source to share


3 answers


A large function probably has a slight performance gain in many smaller functions due to fewer function calls. But my general rule of thumb is to let the compiler do the optimization and focus on functionality and security.



+3


source


Functions are an important part of organizing code in any programming language. Although the performance of having one large function would reduce the use of function calls and therefore reduce stack jumps and lead to more efficient code. But not every project has the luxury of not being modular and having code that is unreadable or worse than confusing or misleading. Over time, the cost of a project with large features will be much greater than a project with small features in terms of maintenance, refactoring, feature enhancements, etc. A function that is large is only important when analyzed in a specific context, and be in some situations where a large function cannot be broken down into smaller pieces and is entirely acceptable if well designed and simple.



Remember the first rule of writing functions do one thing and one thing well

.

+2


source


In C programming, a stack frame is created for each function call, so in the case of a single function, there will only be one stack, and there is no need for a stack jump, but in the case of using many subdivided functions, each function will have a separate stack and for each function call there will be stack jump, so performance may be degraded depending on compiler optimizations.

+1


source







All Articles