Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it true that write all your C code in a single source file will make the program run faster? [duplicate]

I just noticed that at the beginning of the sqlite 3 source code file, it says:

/******************************************************************************
** This file is an amalgamation of many separate C source files from SQLite
** version 3.27.1.  By combining all the individual C code files into this
** single large file, the entire code can be compiled as a single translation
** unit.  This allows many compilers to do optimizations that would not be
** possible if the files were compiled separately.  Performance improvements
** of 5% or more are commonly seen when SQLite is compiled as a single
** translation unit.

I don't know this before and can't find an authoritative source to back it up. Is this true? What about C++?

like image 599
Just a learner Avatar asked Dec 02 '25 13:12

Just a learner


1 Answers

It will not automatically make it faster, but there is some truth to it. Having everything in a single file allows the compiler to do optimizations that would be impossible otherwise. For instance, a function cannot be inlined unless it belongs to the same translation unit in which it is called.

Inlining a function basically means that the function call is replaced with the function body. A benefit of this is that you get to skip the jump to the function code and the return jump. But if the function is in another translation unit, then the compiler will only know the prototype of the function and not the function body, which in turn means that it has to do a jump.

With that being said, I would strongly advice not to use that approach. If you really need that final tweak, then use some kind of script that can create a single c-file from your source tree.

like image 61
klutt Avatar answered Dec 05 '25 03:12

klutt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!