Sujet : Re: C23 thoughts and opinions
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 26. May 2024, 22:52:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v30auq$3kcu9$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Mozilla Thunderbird
On 26/05/2024 17:35, Michael S wrote:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argz, char** argv)
{
if (argz > 1) {
FILE* fp = fopen(argv[1], "wb");
if (fp) {
char buf[2048];
_Bool look_for_comma = 0;
for (;;) {
if (fgets(buf, sizeof(buf), stdin) != buf)
break;
char* p = buf;
for (;;) {
char c = *p;
if (isgraph(c)) {
if (look_for_comma) {
if (c == ',') {
look_for_comma = 0;
++p;
} else {
goto done;
}
} else {
char* endp;
long val = strtol(p, &endp, 0);
if (endp==p) // not a number
goto done;
fputc((unsigned char)val, fp);
p = endp;
look_for_comma = 1;
}
} else {
if (c == 0)
break; // end of line
++p; // skip space or control character
}
}
}
done:
fclose(fp);
} else {
perror(argv[1]);
return 1;
}
}
return 0;
}
I tried this on my 600MB data like this:
C:\c>c fred.exe <data
C:\c>fred --version
clang version 18.1.0rc
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\c
Since those bytes represent the contents of the clang compiler, I was able to run it afterwards.
All versions across compilers/optimise levels seemed to give a constant time of 17-18 seconds. This is good compared with my initial 144 seconds (most compilers failed; you reported a similar test took several minutes).
However, what's involved with a compiler is much elaborate than such a program. There's syntax, type-checking, code-generation...
Still, I reported earlier an experimental change to my non-C compiler, which translated this same input to a program with that embedded binary (not just the binary itself) in under 6 seconds.
That's three times as fast as the above result:
C:\mapps>tm \mx2\mm -ext test2 # tm is timing tool
Compiling test2.m to test2.exe
TM: 5.86 # (timings vary)
C:\mapps>test2
data is 119571969 bytes
C:\mapps>type test2.m
[]byte data = (
include "data"
0)
proc main=
fprintln "data is # bytes", data.len
end