cost of fprintf

后端 未结 4 434
-上瘾入骨i
-上瘾入骨i 2021-01-15 05:31

I am developing an embedded application in C++ for a platform with limited code/data RAM, but rather unlimited RAM for filesystem usage.

While looking for reducing t

4条回答
  •  旧时难觅i
    2021-01-15 06:10

    I can think of three scenarios:

    1. Each time you remove a fprintf line the code size drops slightly, and when you remove the very last fprint it also drops slightly.
    2. When you remove the very last fprint the code size drops significantly.
    3. Every time you remove one instance of fprint the code size drops significantly.

    In scenario 1, it isn't fprintf that is the culprit, but rather the string literals that you are passing to it. You need to get these strings out of your code, either by using very short, terse messages, or by storing the strings in a file and referencing them by some form of ID within the code (although this incurs performance hits)

    In scenario 2, fprintf is (probably) the main culprit. It is quite a complex function that is capable of formatting all kinds of data types in all kinds of ways - so it takes quite a bit of code space. When you remove the last use of it the linker will eliminate it from the final binaries, making them smaller. Try using std::ofstream instead. If you only ever insert (for example) ints and strings to your output file, then only the code for handling ints and strings is linked in.

    Scenario 3 is very unlikely - and would probably indicate that fprintf is being inlined wherever you use it.

    Hope this helps

提交回复
热议问题