4 ms·
C compilers are largely slow because they are written with the expectations of a lot of optimization and large projects rather than with much concern for compil
by vidarh 14y ago
C compilers are largely slow because they are written with the expectations of a lot of optimization and large projects rather than with much concern for compile times.
C compilers that are written to be fast rather than flexible can be extremely fast. E.g. see Fabrice Bellards "TCCBOOT", that he demonstrated compiling the Linux kernel at boot-time in 15 seconds on a 2.4GHz Pentium 4: http://bellard.org/tcc/tccboot_readme.html http://bellard.org/tcc/tccboot_readme.html
TCC can also be treated much like an interpreter like your go example.
- pjmlp 14y agoAny language with modules is able to run circles around C compilation speeds, regardless of what optimizations you might be doing. It all boils down to having the parse each include file every time it appears, because the preprocessor might change the meaning of the file in each inclusion.
- vidarh 14y agoThat's something TCCBOOT demonstrated quite clearly isn't a major problem of C itself but rather an issue with common C compiler implementations, given the size of the Linux kernel and the time spent on a CPU which is by now ancient history. Yes, you need to pre-process and potentially parse them again, but in practice most C include files use include guards and are low cost to process if done even remotely properly.
- pjmlp 14y agoBut the compiler does not know the guards are there, so it needs to read the files anyway. This is why Plan9 C compilers have the strict rules that header files don't include other header files and you as a user need to include them all. I need to look into TCCBOOT in more detail.
- vidarh 14y agoTechnically it needs to read the file once, and keep the data around (of course it can keep re-reading the same files, but there's no technical reason other than memory availability that forces it to). If the compiler implementer cares about speed, it can easily keep the data together with tokenized pre-processed blocks that can be used to reduce the guard lookups to a hash table lookup unless someone has done something weird (in which case the worst case is to revert to pre-processing the file again). It is also perfectly possible to cache token streams or even pre-parsed (with some caveats) versions of the headers specialised by the value of used pre-processor definitions, which in most cases will prevent re-parsing of most include files (only causing re-parses when the pre-processor definitions actually change). The pathological worst case for C code can be horribly nasty, but real-world code is generally fairly well behaved and easy to speed up processing of on modern systems that aren't terribly memory starved. Even on systems that are memory starved, there have been plenty of C compilers that can "pre-compile" headers to speed up compilation.
- e12e 14y ago> TCC can also be treated much like an interpreter I assume you are aware, but tcc can be treated exactly like an interpreter: #!/usr/bin/tcc -run -lgmp #include<stdlib.h> #include "gmp.h" /* all this in eg. fac.c */ void factorial(mpz_t r, int n) { unsigned int i; mpz_t tmp; mpz_init(tmp); mpz_set_ui(r,1); for (i=1;i<=n;i++) { mpz_set_ui(tmp,i); mpz_mul(r,r,tmp); } mpz_clear(tmp); } int main(int argc, char** argv) { mpz_t r; mpz_init(r); int n; if (argc == 2) { n = atoi(argv[1]); factorial(r, n); gmp_printf("%d!:%Zd\n", n, r); mpz_clear(r); } else { exit(1); } exit(0); } chmod a+x fac.c time ./fac.c 1000 1000!:4023872(... snip for formating ...)000 real 0m0.007s user 0m0.004s sys 0m0.000s edit: removed actual factorial, as hn formating choked on the long line. edit #2: indentation/block formatting of code. edit #3: missing "time" prefix...
- vidarh 14y agoYeah, I opted for a weaker claim as I've not actually tested how practical it is to use it that way in myself. I keep meaning to read through the tcc source..