Sujet : Re: macro for fir list?
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 30. Mar 2024, 12:05:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uu8rl0$v2o8$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 30/03/2024 09:56, fir wrote:
>
yet other example
//bytes container
char* bytes = NULL; int bytes_size = 0;
void bytes_add(char val) { (bytes=(char*)realloc(bytes,++bytes_size*sizeof(char)))[bytes_size-1]=val; }
void bytes_load(char* name) { FILE *f = fopen(name, "rb"); int c; while((c=getc(f))!=EOF) bytes_add(c); fclose(f); }
This is pretty inefficient. Loading an 8MB file this way takes 3 seconds, vs. 50ms to load it in one go.
Loading the same 90KB file 10,000 times took 120 seconds, vs. 0.8 seconds even using a scripting language.
80% of the inefficiency is growing the buffer one byte at a time. The other 20% is reading the file one byte at a time.