Article View: comp.lang.c
Article #20Re: Taming The Switch-Statement
From: bart
Date: Wed, 07 Oct 2026 10:32
Date: Wed, 07 Oct 2026 10:32
196 lines
9190 bytes
9190 bytes
On 07/10/2026 05:19, Waldek Hebisch wrote:
> bart <bc@freeuk.com> wrote:
>> On 07/10/2026 00:14, Waldek Hebisch wrote:
>>> bart <bc@freeuk.com> wrote:
>>
>>>> What program can gcc compile in 10ms, Hello World?
>>>
>>> On my machine the following non-canonical version of Hello World:
>>>
>>> #include <unistd.h>
>>> int
>>> main(void) {
>>> write(1, "Hello\n", 6);
>>> return 0;
>>> }
>>>
>>> takes about 20-22 ms. More realistically, compilation of a lead
>>> C program (66 lines) and linking of several .o files (many
>>> produced by a non-C compiler) + libc needs 23 ms producing
>>> 66 kB executable (of which 20 kB is code and the rest is mainly
>>> debug info). The same, but linking statically to libc needs 56 ms
>>> and produces 550 kB executable (of which 413 kB is code).
>>> Note: the last two are for Risc-V. That is compiler runs on my PC,
>>> but executables only run on Risc-V machine. And actually
>>> transferring the executable takes more time than C compilation.
>>
>> So, on your machine, what's the throughput of gcc in lines per second?
>>
>> I mean something more accurate than the 300lps of your 6-line example
>> which will be mostly due to overheads.
>
> Above you also have 66 line file. gcc compile time depends very
> much on source file. One case is 647 lines (17 kB) file, strightforward
> code without attempt to cram too much into each line. Plain 'gcc -Wall'
> (so no optimization) needs 42 ms. This code is actually doing
> possibly large work,
What does that mean, work done at runtime? That shouldn't affect it. The
compiler will only see a bunch of tokens related to the size of the source.
so more realistic is 'gcc -O -Wall' which
> needs 67 ms. 'gcc -O2 -Wall' takes 114 ms and 'gcc -O3 -Wall' takes
> 119 ms (but are unlikely to produce significant speedup). Another
> case is 847 lines file (24 kB) including 669 line (44 kB) header with
> macros and 22 line header declaring functions. This is heavily macroized
> code which macroexpandd to about 900 kB.
Recent discussions about headers suggested even huge headers were a
non-issue, according to some here.
I have an example of a 32KB file that expands to 4MB after
preprocessing. Here is compile-speed:
Time EXE size
gcc -O0 16 secs 3.0 MB
gcc -O2 42 secs 1.5 MB
tcc 0.37 secs 3.6 MB
I'm not arguing against never using gcc or never using optimisations.
I'm saying that for my development patterns (compile 100s of times a
day), it is just too slow and unresponsive to use routinely.
It can be used from time to time for its static analysis/linting part,
where it can give opinions about your work. While optimisation may only
be needed for production code, or again every so often to test there
will be no problems.
But you might need optimising more often if the speed-up is significant,
and you rely on it (or it takes annoyingly long - longer than the extra
compile-time - to run your tests!).
>> c:\cx>tim bcc sql
>> Compiling sql.c to sql.exe
>> Time: 0.228
>> Amazing isn't it that I can build a working SQL program in about the
>> time it takes gcc/clang to build Hello, World! bcc is not even optimised.
>
> Such compile speed is nice. Concerning "amazing", relative to
> machine speed such compile speeds were available in the past. But
> most of old language tools vanished and surviving ones got relatively
> slower. Apparently, people care much more about speed and size of
> resulting code than about compiler speed.
This one of my points: they never seems to care about the speed of the
compiler!
> Consider silly example,
> but small enough to give it here in full (targeting Risc-V):
>
> long f6(void) {
> return 6;
> }
>
> 'tcc -c' gives the following code (after disassembly):
>
> 0000000000000000 <f6>:
> 0: ff010113 add sp,sp,-16
> 4: 00113423 sd ra,8(sp)
> 8: 00813023 sd s0,0(sp)
> c: 01010413 add s0,sp,16
> 10: 00000013 nop
> 14: 0060051b addw a0,zero,6
> 18: 00813083 ld ra,8(sp)
> 1c: 00013403 ld s0,0(sp)
> 20: 01010113 add sp,sp,16
> 24: 00008067 ret
>
> that is 10 instructions, 40 bytes. 'gcc -c' (so no optimization)
> gives:
>
> 0000000000000000 <f6>:
> 0: 1141 add sp,sp,-16
> 2: e422 sd s0,8(sp)
> 4: 0800 add s0,sp,16
> 6: 4799 li a5,6
> 8: 853e mv a0,a5
> a: 6422 ld s0,8(sp)
> c: 0141 add sp,sp,16
> e: 8082 ret
>
> so 8 instructions, 16 bytes. 'gcc -O -c' gives:
What does '-O' do? On my gcc it gives same results as -O0.
> 0000000000000000 <f6>:
> 0: 4519 li a0,6
> 2: 8082 ret
>
> so 2 instructions, 4 bytes.
My bcc product gives better code than tcc while nearly as fast
compiling. For your example, bcc generates this for x64:
f6:
mov eax, 6
ret
These are EXE sizes for Lua, for x64/WinABI
gcc -O0 465 KB
gcc -O2 414 KB
gcc -O3 466 KB
gcc -Os 328 KB
tcc 384 KB
bcc 237 KB
> One things is that apparently tcc does not
> support Risc-V compressed instructions, which leads to considerably
> bigger code. Another thing is that tcc inserts a 'nop' for no good
> reason. And even without optimization gcc realizes that nothing
> clobbers link register so avoids storing and restoring it. gcc
> produces constant in "wrong" register, so has to move it, but in the
> balance looks slightly better than tcc.
My point above is that you can do minor 'optimisations' (generating
sensible code, plus peep-hole stuff) with very little cost.
(My bcc product has an extra step, which allocates some locals to
registers. Plus a peep-hole optimiser stage which doesn't make it any
faster, but makes the code a little smaller.
This has zero-cost, so it is enabled by default. It is zero-cost because
the extra time to do it (only 10% anyway) if offset by having less code
to deal with in the later stages.
If I disable it, then lua.exe ends up at 289KB - still smaller than gcc.)
> 'gcc -O' clearly gives much better code, on low end machines it is
> likely to be 5 times faster.
If that means -O2 or -O3, then on my programs, it is more typically 2x
as fast, for C code. But more like 1.5 x as fast for the C code
transpiled from my language using my old transpiler, for language projects.
Current transpiled C is low level and may affect how well it can
optimise larger programs. But if applied to 'bcc sql', then timing
reduces to just below 0.2 seconds; about 15% faster.
>>> And large part of it is likely to
>>> be gcc startup time, so you can make program larger whithout large
>>> increase of compile time.
>>
>> Yes, it is big and slow and they haven't provided a fast path (like a
>> secretly bundled version of tcc) when you need an instant, streamlined
>> result.
>>
>>
>
Technical Details
Message-ID:
<11a574n$2pano$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <119rau4$35ugq$1@dont-email.me> <119uo2c$3kc0t$1@paganini.bofh.team> <119uqnv$d6j2$1@dont-email.me> <20261005143559.157@kylheku.com> <11a1arj$1e8ks$1@dont-email.me> <11a295r$1lqal$3@dont-email.me> <11a2e2m$ef4o$2@dont-email.me> <11a2j83$1qu9n$1@dont-email.me> <11a2maf$1soc7$1@dont-email.me> <11a2vrn$20jj3$1@dont-email.me> <EI7xS.906$aNSb.21@fx20.iad> <11a37gv$23ruj$1@dont-email.me> <11a3vcj$88e7$1@paganini.bofh.team> <11a43hl$2e5k2$1@dont-email.me> <11a4h8v$eeb8$1@paganini.bofh.team>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Wed, 7 Oct 2026 11:32:56 +0100 Organization: A noiseless patient Spider Lines: 196 Message-ID: <11a574n$2pano$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <119rau4$35ugq$1@dont-email.me> <119uo2c$3kc0t$1@paganini.bofh.team> <119uqnv$d6j2$1@dont-email.me> <20261005143559.157@kylheku.com> <11a1arj$1e8ks$1@dont-email.me> <11a295r$1lqal$3@dont-email.me> <11a2e2m$ef4o$2@dont-email.me> <11a2j83$1qu9n$1@dont-email.me> <11a2maf$1soc7$1@dont-email.me> <11a2vrn$20jj3$1@dont-email.me> <EI7xS.906$aNSb.21@fx20.iad> <11a37gv$23ruj$1@dont-email.me> <11a3vcj$88e7$1@paganini.bofh.team> <11a43hl$2e5k2$1@dont-email.me> <11a4h8v$eeb8$1@paganini.bofh.team> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 07 Oct 2026 10:32:56 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2927352"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18brLHTTCR/OV+Z8+3GJKWL"; posting-host="ddea08e9999a8df42a3f6de53c7cc773" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:1gdLJ+cR0ubvp2iEQ3sI1g/qhC0= sha256:WvC9YXciLrLFIJ0U5Wp4IRx3TGeQg9/2LV2iJjyzIM0= sha1:TPxh8y1VZ3r5fWnn8w7FDKTPUZ4= sha256:+ZTAXQ+wcOq0eVElujTVG2pSHhkOP7KJM+zlE5OTBIo= In-Reply-To: <11a4h8v$eeb8$1@paganini.bofh.team> Content-Language: en-GB