Article View: comp.lang.c
Article #8Re: Taming The Switch-Statement
From: Waldek Hebisch
Date: Wed, 07 Oct 2026 04:19
Date: Wed, 07 Oct 2026 04:19
150 lines
7179 bytes
7179 bytes
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, 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. This code is performance
critical and benefits from vectorization, so normally it would be
compiled using 'gcc -O3 -c', which takes 10.2s. 'gcc -O2 -c' needs
8.2s, 'gcc -O -c' take 4.8s, 'gcc -c' takes '1.45s'.
First file gives you a bit more than 15000 lines per second without
optimization and about 9600 lines per second at '-O'. Second
gives you about 1000 lines per second without optimization and
about 148 at '-O3'. But lines coming from macros are compiled
multiple times and without using macros the file would be much
bigger. Considering size in characters after expansion non-macro
version probably would need 20000 lines.
> My compilers for Windows tend to work at up 0.5Mlps, and between 1-2
> magnitudes faster than gcc.
>
>
>> 150 ms is still not too long.
>
> I use Windows where it is 250ms (a bit lower now), and yet my own
> product works fine on it; this is my C compiler building sqlite3:
>
> c:\cx>tim bcc sql
> Compiling sql.c to sql.exe
> Time: 0.228
>
> And this is gcc 14 building hello.c:
>
> c:\cx>tim gcc hello.c
> Time: 0.203
>
> And gcc 10:
>
> c:\cx>tim \tdm10\bin\gcc hello.c
> Time: 0.244
>
> And clang:
>
> c:\cx>tim clang hello.c
> Time: 0.263
>
> 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. 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:
0000000000000000 <f6>:
0: 4519 li a0,6
2: 8082 ret
so 2 instructions, 4 bytes. 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.
'gcc -O' clearly gives much better code, on low end machines it is
likely to be 5 times 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.
>
>
--
Waldek Hebisch
Technical Details
Message-ID:
<11a4h8v$eeb8$1@paganini.bofh.team>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!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>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 7 Oct 2026 04:19:45 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11a4h8v$eeb8$1@paganini.bofh.team>
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>
X-Complaints-To: abuse@bofh.team
Injection-Date: Wed, 7 Oct 2026 04:19:45 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="473448"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6";
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.09.1-rc3