Thread View: comp.lang.c
13 messages
13 total messages
Started by bart
Wed, 07 Oct 2026 00:25
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 07 Oct 2026 00:25
Date: Wed, 07 Oct 2026 00:25
70 lines
3781 bytes
3781 bytes
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.
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.
> 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.
Re: Taming The Switch-Statement
Author: 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
Re: Taming The Switch-Statement
Author: Lawrence D’Oliveiro
Date: Wed, 07 Oct 2026 06:28
Date: Wed, 07 Oct 2026 06:28
15 lines
2473 bytes
2473 bytes
On Wed, 7 Oct 2026 04:19:45 -0000 (UTC), Waldek Hebisch wrote: > 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. The Linux kernel is currently up to about 38 million lines of code, last I heard. How long do you think it would take to compile all that? On a reasonably grunty machine, of course, but *without* resorting to a RAM disk? Would you believe, a little over two minutes? Down to 15 seconds for a build with some reasonable configuration choices? <https://www.tomshardware.com/software/linux/linux-enthusiasts-see-10-second-kernel-compilation-times-on-the-horizon-ai-assisted-patches-cut-build-times-by-nearly-a-third-without-a-ramdisk>
Re: Taming The Switch-Statement
Author: David Brown
Date: Wed, 07 Oct 2026 07:07
Date: Wed, 07 Oct 2026 07:07
53 lines
3822 bytes
3822 bytes
On 07/10/2026 02:25, bart 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.
>
> My compilers for Windows tend to work at up 0.5Mlps, and between 1-2
> magnitudes faster than gcc.
>
I can't answer for other people, but when I write code, usually there is
a fair amount of difference between different lines. The time taken for
a compilation depends on the code!
You can do all the speed measurements you like using files made from
thousands of copies of the same pointless function. It means /nothing/.
"Lines per second" is a good way to rate machines for painting dashed
lines down the middle of a road - not a way to judge a compiler.
Your tools are useless to everyone except you. No one cares if they are
a million times faster at being useless, people need tools that do
something useful to them.
So for the love of $DEITY, give it a break! You write plenty of
interesting posts - your addiction to milliseconds is not part of that.
Re: Taming The Switch-Statement
Author: 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.
>>
>>
>
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 07 Oct 2026 10:39
Date: Wed, 07 Oct 2026 10:39
22 lines
2639 bytes
2639 bytes
On 07/10/2026 07:28, Lawrence D’Oliveiro wrote: > On Wed, 7 Oct 2026 04:19:45 -0000 (UTC), Waldek Hebisch wrote: > >> 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. > > The Linux kernel is currently up to about 38 million lines of code, > last I heard. How long do you think it would take to compile all that? > On a reasonably grunty machine, of course, but *without* resorting to > a RAM disk? On x64 that would equate to roughly a 400MB binary. That is quite a hefty 'kernel'! Or maybe only some of that code will be compiled for any particular target. I heard of a figure around 5% which would make it 2Mloc. I don't know if it still has to trawl through the other 95% to extract it, or it is organised more sensibly.
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 07 Oct 2026 10:49
Date: Wed, 07 Oct 2026 10:49
69 lines
4578 bytes
4578 bytes
On 07/10/2026 08:07, David Brown wrote:
> On 07/10/2026 02:25, bart 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.
>>
>> My compilers for Windows tend to work at up 0.5Mlps, and between 1-2
>> magnitudes faster than gcc.
>>
> I can't answer for other people, but when I write code, usually there is
> a fair amount of difference between different lines. The time taken for
> a compilation depends on the code!
>
> You can do all the speed measurements you like using files made from
> thousands of copies of the same pointless function. It means /nothing/.
I've given examples for real code.
But yes synthesised benchmarks can be used to spot bottlenecks. Maybe
other compiler developers can give it a go!
> "Lines per second" is a good way to rate machines for painting dashed
> lines down the middle of a road - not a way to judge a compiler.
>
> Your tools are useless to everyone except you. No one cares if they are
> a million times faster at being useless, people need tools that do
> something useful to them.
> So for the love of $DEITY, give it a break! You write plenty of
> interesting posts - your addiction to milliseconds is not part of that.
It is annoying though that no one wants to acknowledge that gcc IS slow.
I've given examples where you just want a compiler to do a mechanical
translation from source code to binary, just like you might translate
assembly to binary.
You DON'T CARE about detailed analysis. You might not even care about
the code quality for any number of reasons.
gcc does not give you an option where it is suddenly 10 or 20 times
faster (and it uses a fraction of the memory). We need -O-1 or -O-2.
Re: Taming The Switch-Statement
Author: David Brown
Date: Wed, 07 Oct 2026 10:57
Date: Wed, 07 Oct 2026 10:57
36 lines
3238 bytes
3238 bytes
On 07/10/2026 12:39, bart wrote: > On 07/10/2026 07:28, Lawrence D’Oliveiro wrote: >> On Wed, 7 Oct 2026 04:19:45 -0000 (UTC), Waldek Hebisch wrote: >> >>> 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. >> >> The Linux kernel is currently up to about 38 million lines of code, >> last I heard. How long do you think it would take to compile all that? >> On a reasonably grunty machine, of course, but *without* resorting to >> a RAM disk? > > On x64 that would equate to roughly a 400MB binary. That is quite a > hefty 'kernel'! > > Or maybe only some of that code will be compiled for any particular > target. I heard of a figure around 5% which would make it 2Mloc. > The kernel on my system is about 15 MB. Sizes vary by version and build details, of course, but that's roughly the size. Some of the code in the kernel is target specific and therefore not used on most targets. But a very large proportion is typically compiled to modules rather than linked directly in the kernel, and many of the modules are not even built in most cases. I have no idea of the exact proportions. > I don't know if it still has to trawl through the other 95% to extract > it, or it is organised more sensibly. > It is organised sensibly.
Re: Taming The Switch-Statement
Author: David Brown
Date: Wed, 07 Oct 2026 11:11
Date: Wed, 07 Oct 2026 11:11
46 lines
3933 bytes
3933 bytes
On 07/10/2026 12:49, bart wrote: > On 07/10/2026 08:07, David Brown wrote: >> So for the love of $DEITY, give it a break! You write plenty of >> interesting posts - your addiction to milliseconds is not part of that. > It is annoying though that no one wants to acknowledge that gcc IS slow. > People /do/ understand that gcc takes time, and people /would/ prefer if it did the same job, faster. What seems to totally pass you by is that there is a difference between "slow" and "too slow". No one is happy if there builds take 10 minutes. Equally, no one cares if their builds take 1 second or 0.1 seconds. If I have to compile 20 files, and each takes 1 second to compile, it would be annoying to do them one at a time - but if they all compile in parallel, it takes just 1 second of my time. No one - but you - is bothered as long as real-life compile times are fast enough that they don't get in the way of development. Your tools are useless to me - they could just as easily be "cat file.c > /dev/null". Your other favourite toy compilers, like tcc, are equally useless. Since they are useless, they are not applicable in a speed comparison. When I compare compile times with /real/ tools - things that do good optimisations and good static checking, with support for modern language standards and the extensions I want, and target the devices I need - I have no reason to call gcc "slow". clang, or Green Hills, or Metrowerks, or Keil might be a bit faster in some cases, maybe a bit slower in other cases, but I have not seen significant differences. So I will not call gcc "slow" until I see another tool that does approximately as good a job for my needs, and does so several times faster than gcc. And I will not call gcc "too slow" unless I find its speed a problem or inconvenience. Of course I will always be happier if gcc gets faster, but that is not the same as thinking it is slow. I am glad that the GCC developers put their limited time and resources into other things than the speed of compilation of C code.
Re: Taming The Switch-Statement
Author: Waldek Hebisch
Date: Wed, 07 Oct 2026 15:10
Date: Wed, 07 Oct 2026 15:10
232 lines
10777 bytes
10777 bytes
bart <bc@freeuk.com> wrote:
> 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.
Yes.
> The
> compiler will only see a bunch of tokens related to the size of the source.
You want useful result, that is why '-O'.
> 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.
Normal headers, yes. To give you an idea what is involved let me show
small part of header and the file. From 'arrpack.h':
#define PROC1N(N1, N2, TYPE, OP, ARGS...) /* 1 array normal */ \
int N1 ## A ## N2(ARGS TYPE *arr, int *spec) \
{ \
int err, off, d, nd; \
int *wk, *wp, *sps, *sp; \
TYPE *arrend; \
\
if (err = arrspec(spec, 1, 1, &off, &d, &nd, &sps, &wk)) \
return err; \
arr += off; \
\
while (1) { \
/* dimension 1 expanded */ \
for (arrend = arr+nd; arr < arrend; arr += d) \
{ OP } \
for (sp=sps, wp=wk; ; ) { /* loop over dims 2...N */ \
if (sp == wk) return 0; \
arr += *sp++; \
if (++(*wp) == *sp++) \
*wp++ = 0; \
else \
break; \
} \
} \
}
...
#define PROC1(N1, N2, TYPE, OP, ARGS...) \
PROC1N(N1, N2, TYPE, OP, ARGS) \
PROC1M(N1, N2, TYPE, OP, ARGS) \
PROC1I(N1, N2, TYPE, OP, ARGS)
From 'arrpack.c':
#include <math.h>
#include "arrpack.h"
/*
-- Single array -------------------------------------------------------
*/
#define N2 zero
#define OP *arr = 0;
PROC1(b, N2, unsigned char, OP, )
PROC1(i, N2, int, OP, )
PROC1(s, N2, float, OP, )
PROC1(d, N2, double, OP, )
PROC1(c, N2, __complex__ float, OP, )
PROC1(z, N2, __complex__ double, OP, )
#undef OP
#undef N2
As you can see this creates 18 not so small functions.
> 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.
OK
>> 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.
I meant '-O'. In gcc '-O' means simpler and faster optimizations,
which almost always lead to faster code. '-O2' means havier
optimizations, and '-O3' even more heavy. Also, optimizations
at '-O2' or '-O3' level may backfire: transformation that is
intended to increase speed may actually decrease it.
On high-end machines CPU frequently is quite powerful, but execution
speed is limited by memory access. In such case executing say twice
as many instructions as needed costs only a little. But on low end
machines executing useless instructions cases significant slowdown.
--
Waldek Hebisch
Re: Taming The Switch-Statement
Author: bart
Date: Wed, 07 Oct 2026 18:41
Date: Wed, 07 Oct 2026 18:41
89 lines
5092 bytes
5092 bytes
On 07/10/2026 16:10, Waldek Hebisch wrote:
> bart <bc@freeuk.com> wrote:
>> What does that mean, work done at runtime? That shouldn't affect it.
>
> Yes.
>
>> The
>> compiler will only see a bunch of tokens related to the size of the source.
>
> You want useful result, that is why '-O'.
Apparently, -O means the same as -O1.
>> Recent discussions about headers suggested even huge headers were a
>> non-issue, according to some here.
>
> Normal headers, yes. To give you an idea what is involved let me show
> small part of header and the file. From 'arrpack.h':
>
> #define PROC1N(N1, N2, TYPE, OP, ARGS...) /* 1 array normal */ \
> int N1 ## A ## N2(ARGS TYPE *arr, int *spec) \
> { \
...> }
> ...
> #define PROC1(N1, N2, TYPE, OP, ARGS...) \
> PROC1N(N1, N2, TYPE, OP, ARGS) \
> PROC1M(N1, N2, TYPE, OP, ARGS) \
> PROC1I(N1, N2, TYPE, OP, ARGS)
...> PROC1(b, N2, unsigned char, OP, )
> PROC1(i, N2, int, OP, )
> PROC1(s, N2, float, OP, )
> PROC1(d, N2, double, OP, )
> PROC1(c, N2, __complex__ float, OP, )
> PROC1(z, N2, __complex__ double, OP, )
> #undef OP
> #undef N2
>
> As you can see this creates 18 not so small functions.
...
This generates about 5KB of code when expanded. You say the full version
is nearly 1MB? So what is the problem: generating that 1MB, or having to
process it?
It's roughly 30Kloc so if it is processing all that code, then it would
be the same problem as dealing with those big headers. Less in fact
since presumably it is only processed once if it contain function defs.
>> gcc -O0 465 KB
>> gcc -O1 348 KB>> gcc -O2 414 KB
>> gcc -O3 466 KB
>> gcc -Os 328 KB
>> 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.
>
> I meant '-O'. In gcc '-O' means simpler and faster optimizations,
> which almost always lead to faster code. '-O2' means havier
> optimizations, and '-O3' even more heavy. Also, optimizations
> at '-O2' or '-O3' level may backfire: transformation that is
> intended to increase speed may actually decrease it.
OK, I've added -O1 to my table.
I rarely use it, because if I'm using gcc for its optimisations, then I
might as well get the best. In this case, Lua working on a Fibonacci
test gives these results:
gcc -O0 9.4 seconds
gcc -O1 3.1 seconds
gcc -O2 2.9 seconds
bcc 6.6 seconds
('bcc' is poor here partly because of how this project is written: full
of deeply nested macros and function calls. This seems to be typical of
interpreters written in C.
My own interpreter, written in plain code and running the same
benchmark, takes 1.2 seconds.)
> On high-end machines CPU frequently is quite powerful, but execution
> speed is limited by memory access. In such case executing say twice
> as many instructions as needed costs only a little. But on low end
> machines executing useless instructions cases significant slowdown.
>
Re: Taming The Switch-Statement
Author: Waldek Hebisch
Date: Thu, 08 Oct 2026 04:49
Date: Thu, 08 Oct 2026 04:49
119 lines
6273 bytes
6273 bytes
bart <bc@freeuk.com> wrote:
> On 07/10/2026 16:10, Waldek Hebisch wrote:
>> bart <bc@freeuk.com> wrote:
>
>>> What does that mean, work done at runtime? That shouldn't affect it.
>>
>> Yes.
>>
>>> The
>>> compiler will only see a bunch of tokens related to the size of the source.
>>
>> You want useful result, that is why '-O'.
>
> Apparently, -O means the same as -O1.
>
>>> Recent discussions about headers suggested even huge headers were a
>>> non-issue, according to some here.
>>
>> Normal headers, yes. To give you an idea what is involved let me show
>> small part of header and the file. From 'arrpack.h':
>>
>> #define PROC1N(N1, N2, TYPE, OP, ARGS...) /* 1 array normal */ \
>> int N1 ## A ## N2(ARGS TYPE *arr, int *spec) \
>> { \
> ...> }
>> ...
>> #define PROC1(N1, N2, TYPE, OP, ARGS...) \
>> PROC1N(N1, N2, TYPE, OP, ARGS) \
>> PROC1M(N1, N2, TYPE, OP, ARGS) \
>> PROC1I(N1, N2, TYPE, OP, ARGS)
> ...> PROC1(b, N2, unsigned char, OP, )
>> PROC1(i, N2, int, OP, )
>> PROC1(s, N2, float, OP, )
>> PROC1(d, N2, double, OP, )
>> PROC1(c, N2, __complex__ float, OP, )
>> PROC1(z, N2, __complex__ double, OP, )
>> #undef OP
>> #undef N2
>>
>> As you can see this creates 18 not so small functions.
> ...
>
> This generates about 5KB of code when expanded. You say the full version
> is nearly 1MB? So what is the problem: generating that 1MB, or having to
> process it?
Macro expansion is pretty fast. It is production of machine code that
takes time.
> It's roughly 30Kloc so if it is processing all that code, then it would
> be the same problem as dealing with those big headers. Less in fact
> since presumably it is only processed once if it contain function defs.
Sorry, you do not get it. Normal headers consist of declarations.
gcc is reasonable fast at parsing and handling declarations. But
above you have actual executable code and translating it to
machine code takes time.
>>> gcc -O0 465 KB
>>> gcc -O1 348 KB>> gcc -O2 414 KB
>>> gcc -O3 466 KB
>>> gcc -Os 328 KB
>
>>> 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.
>>
>> I meant '-O'. In gcc '-O' means simpler and faster optimizations,
>> which almost always lead to faster code. '-O2' means havier
>> optimizations, and '-O3' even more heavy. Also, optimizations
>> at '-O2' or '-O3' level may backfire: transformation that is
>> intended to increase speed may actually decrease it.
>
> OK, I've added -O1 to my table.
>
> I rarely use it, because if I'm using gcc for its optimisations, then I
> might as well get the best.
Sometimes '-O' is the best. Not frequently, but it happens.
> In this case, Lua working on a Fibonacci
> test gives these results:
>
> gcc -O0 9.4 seconds
> gcc -O1 3.1 seconds
> gcc -O2 2.9 seconds
> bcc 6.6 seconds
>
> ('bcc' is poor here partly because of how this project is written: full
> of deeply nested macros and function calls. This seems to be typical of
> interpreters written in C.
The question frequently is "who is doing optimization?". Original
C design was to have programmer do most optimization and use
relatively dumb compiler. But is is easier to write the code
assuming aggressive compiler optimization. Basically, you write
rather general code, which always works, but is specific cases
when translated anively performs useless operations. In the past
programmer would specialize such code by hand, leaving only what is
needed in each case. But now compiler optimizations can do most
of this work.
> My own interpreter, written in plain code and running the same
> benchmark, takes 1.2 seconds.)
Lua interpreter is recognized as rather slow. But you frequently
compare apples with oranges. People use interpreted languages
either for safety, that is runtime checks or for fancy language features
or both. Correct program will run fine and get significant speedup
when you remove safety checks, but during developement program is
rarely correct and safety checks are essential. Some language
features slow down all programs in given language, even ones that
do not use the feature. But usually programmes do not want to
deal with multiple languages and stick to one that has all
features that they need. So really, comparing interpreters for
different languages have limited use.
--
Waldek Hebisch
Re: Taming The Switch-Statement
Author: bart
Date: Thu, 08 Oct 2026 11:24
Date: Thu, 08 Oct 2026 11:24
149 lines
8093 bytes
8093 bytes
On 08/10/2026 05:49, Waldek Hebisch wrote:
> bart <bc@freeuk.com> wrote:
>> This generates about 5KB of code when expanded. You say the full version
>> is nearly 1MB? So what is the problem: generating that 1MB, or having to
>> process it?
>
> Macro expansion is pretty fast. It is production of machine code that
> takes time.
>
>> It's roughly 30Kloc so if it is processing all that code, then it would
>> be the same problem as dealing with those big headers. Less in fact
>> since presumably it is only processed once if it contain function defs.
>
> Sorry, you do not get it. Normal headers consist of declarations.
> gcc is reasonable fast at parsing and handling declarations. But
> above you have actual executable code and translating it to
> machine code takes time.
If I understand your point, it is that LPS throughput for C can be
problematic because source code can be a mix of executable code,
unexpanded macro invocations and declarations?
When producing benchmark figures, you try to avoid extreme cases of
excessive macro use and disproportionately large headers even containing
only declarations.
Such figures can still be useful when building fairly sane code-bases
with normal-sized headers and moderate use of macros.
Your example that expands to nearly 1MB, generates long expansions with
few line-breaks, so line-count is tricky anyway.
Now here I assumed the 42ms -O0 figure was for this program. But at an
average density of 20 or so characters/line, that would equate to around
a million lines per second throughput!
So I had to go back a few posts to read it more carefully. This program
takes 1.45s at -O0, so roughly 30Klps at that code density.
This is not bad for gcc.
>> ('bcc' is poor here partly because of how this project is written: full
>> of deeply nested macros and function calls. This seems to be typical of
>> interpreters written in C.
>
> The question frequently is "who is doing optimization?". Original
> C design was to have programmer do most optimization and use
> relatively dumb compiler. But is is easier to write the code
> assuming aggressive compiler optimization. Basically, you write
> rather general code, which always works, but is specific cases
> when translated anively performs useless operations. In the past
> programmer would specialize such code by hand, leaving only what is
> needed in each case. But now compiler optimizations can do most
> of this work.
My problem is that gcc/clang are slow even at -O0! So the question is
why, and could something have been done about that.
People here may say that even -O0 is doing static analysis etc. But
suppose you don't want that. My guess is the infrastructure needed to
enable optimisations gets in the way even for -O0.
>> My own interpreter, written in plain code and running the same
>> benchmark, takes 1.2 seconds.)
>
> Lua interpreter is recognized as rather slow.
Not any more. Lua 5.5 is quite nifty, and can match or beat my
interpreter on some tests, although not for Fibonacci!
> But you frequently
> compare apples with oranges.
I compare my pure interpreter for a dynamically typed language with some
one else's interpreter for their dynamically typed language, on a
program running the same algorithm.
Mine usually has the edge, even though it does everything wrong. Lua has
a small number of built-in types only; it uses a compact, tagged 64-bit
value for all objects; it uses an efficient register based VM.
I have a lot more types plus user-defined ones. I use a complex 128-bit
descriptor. I use a stack-based VM. Plus I use an interpreter that is
unoptimised code!
So, why am I still fast? Maybe this is a clue; in lua.c there is this
line (may be part of a switch, but you can't tell):
vmcase(OP_ADDK) {
op_arithK(L, l_addi, luai_numadd);
vmbreak;
}
That op_arithK() is actually a macro invocation that expands to this:
{TValue*v1=(&((base+(((void)0),((((int)((((i)>>((((0+7)+8)+1)))&(
(~((~(Instruction)0)<<(8)))<<(0))))))))))->val);TValue*v2=(k+(((void)
0),((((int)((((i)>>(((((0+7)+8)+1)+8)))&((~((~(Instruction)0)<<(8)))<<
(0)))))))));((void)0);{StkId ra=(base+(((int)((((i)>>((0+7)))&((~((~
(Instruction)0)<<(8)))<<(0)))))));if(((((v1))->tt_)==(((3)|((0)<<4))))
&&((((v2))->tt_)==(((3)|((0)<<4))))){lua_Integer i1=(((void)0),(((v1)
->value_).i));lua_Integer i2=(((void)0),(((v2)->value_).i));pc++;{
TValue*io=((&(ra)->val));((io)->value_).i=(((lua_Integer)(((
lua_Unsigned)(i1))+((lua_Unsigned)(i2)))));((io)->tt_=(((3)|((0)<<4)))
);};}else{lua_Number n1;lua_Number n2;if((((((v1))->tt_)==(((3)|((1)
<<4))))?((n1)=(((void)0),(((v1)->value_).n)),1):(((((v1))->tt_)==(((3)
|((0)<<4))))?((n1)=((lua_Number)(((((void)0),(((v1)->value_).i))))),1)
:0))&&(((((v2))->tt_)==(((3)|((1)<<4))))?((n2)=(((void)0),(((v2)->
value_).n)),1):(((((v2))->tt_)==(((3)|((0)<<4))))?((n2)=((lua_Number)
(((((void)0),(((v2)->value_).i))))),1):0))){pc++;{TValue*io=((&(ra)->
val));((io)->value_).n=(((n1)+(n2)));((io)->tt_=(((3)|((1)<<4))));};
}};};};
(I'm familiar with this as I had to debug my compiler on it.)
Here, my guess is it's relying on the compiler to clean up the mess.
While my equivalent is plain code.
(Note that in Lua 5.5, 'ADD' I believe is only defined for INT and FLOAT
types; it used to be just FLOAT. I have 'ADD' defined for 5 types.)
> People use interpreted languages
> either for safety, that is runtime checks or for fancy language features
> or both.
People use such languages because they are easier to use, they can be
more productive, and there is no annoying build-step.
(No static analysis either, and no warnings; how is that possible?)
Some may use them for prototyping or development before porting to a
compiled language.
> So really, comparing interpreters for
> different languages have limited use.
If you say so. But it sounds to me like making excuses for a slow product.
Endless efforts have been made over the last two decades in making
scripting languages run fast. Apart from approaches using type inference
or tracing-JIT, this also applies to pure interpreters.
Part of that involves benchmarking.
So can certainly compare language implementations.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads