r/C_Programming 1d ago

i made lib that beets fmt lib

Enable HLS to view with audio, or disable this notification

i made logging in C that does not need formater% as printf instead it has auto-type detect via _Generic ,like fmt it uses {} as placeholder

i started it just for fun but later i notice that it is insanely fast it beets fmt lib and rust print 4.5x faster

it uses linux syscall write i might map syscall for windows latter

this is very interesting because i did not optimize the lib like convert from type to another use poor impl + i write in the buffer many times

write(1, logtag, strlen(logtag));

write(1, filename, strlen(filename)) ;

write(1, "->", 2);

write(1, function, strlen(function)) ;

write(1, " ", 1);

write(1, buffer, out);

so i guess after optimizing it will be 2x faster

all respect for fmt devs i inspired the {} from them btw

2 Upvotes

5 comments sorted by

5

u/aocregacc 1d ago

idk about rustc but you didn't turn the optimizations on for your C and C++ compilations. It's pretty meaningless to compare the speed without optimizations on.

3

u/Realistic_Extent3487 1d ago

this is with -o3 btw

// Time: 0.269424 seconds ester

// Time: 1.159421 seconds fmt

2

u/DreamingElectrons 1d ago

If you now find a way to somehow capture the variables from the outer scope and then out the variables into the curly brackets in your string you basically have python's F-strings, would be cool but probably needs quite a lot of trickery to get the values...

1

u/[deleted] 1d ago

[deleted]

1

u/DreamingElectrons 1d ago

It is with some heavy trickery and use of extensions, but there is a reason why most people don't actually touch those.

2

u/Realistic_Extent3487 1d ago edited 1d ago

Well I did already but without {} because C macros can't has {} so I used ESTER_FPRINT(logger,x) Latter it detect x and print it

Note: it can print the var without strings like

ESTER_LOG(logger,x) But Not Strings as ESTER_LOG(logger,"my age is {x})