Size of printf

Programming, for all ages and all languages.
Post Reply
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Size of printf

Post by mark3094 »

Hi,

I have a few functions in my mini C library. These include printf, strlen, strcat, strncat, putchar and int/hex/oct to ASCII.

Most of these functions are very small, however, my printf has been built based on the C99 standard, and it took nearly 600 lines of code to complete.
Does this sound excessive?

When I compile to a static library, it is 47KB without optimisation.
Just wondering if this sounds normal, or too large? I imagine it will push up the size of my kernel, but maybe that's fine.

Any experience that anyone can add to this would be great.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Size of printf

Post by bluemoon »

I don't see any immediate need to optimize printf, in size or in speed; until your kernel is very mature.

FYI, my test application linked with newlib is 14764 bytes:

Code: Select all

#include <stdio.h>
int main ( int argc, char *argv[] ) {
    printf ("Hello from user application!\n");
    return 0xBADC0DE;
}
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Size of printf

Post by mark3094 »

Thankyou

No, my Kernel is non-existant. I've been spending my time building the supporting functions.
I won't worry about the size for now. At least not until it becomes an issue.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Size of printf

Post by Combuster »

my local printf backend is 567 lines without float support, so your number sounds about right. when summing the size of my strlen/str(n)cat + strto* + print (courtesy of pdclib) I end up with 17k of object files at -O0 and 15k at -O2, printf and dependencies being responsible for 8k/7k respectively so you might want to compare the designs due to the factor 3 difference observed.

Bluemoon's output seems to be in a similar league, with 14k of output code (which has a printf and its dependencies, plus a main, and probably including a runtime as well)

Bluemoon also mentioned the other important point: don't optimize prematurely. You are likely to introduce bugs that way.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Size of printf

Post by gerryg400 »

47k sounds large. My printf driver, which admittedly is not fully featured, is 2k.
If a trainstation is where trains stop, what is a workstation ?
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Size of printf

Post by mark3094 »

Is there any reasonable way to begin troubleshooting why the output file is so large?

The printf.obj file is 23KB, and the printf.asm file is 90KB (both of these created by the compiler).

The method I'm using for printf involves making a string in memory, based on the parameters passed to printf, then writing the string to screen with putchar.

Could that be the problem?


Or maybe I should create a cut down version of printf for Kernel use only, which doesn't support floating point, long long integers, etc?
Post Reply