Obviously if you're gonna use a 512 byte buffer you're gonna run into issues. Now try something more realistic, like for example 1-4MiB. Also there's no async io here which would close the gap even more.Dann Corbit wrote: ↑Mon Nov 29, 2021 10:51 pm Re: "And that buffering is precisely what you want to AVOID for files that are meant to be read once."
Code: Select all
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> char string[32767]; char *getsafe(char *buffer, int count) { char *result = buffer, *np; if ((buffer == NULL) || (count < 1)) result = NULL; else if (count == 1) *result = '\0'; else if ((result = fgets(buffer, count, stdin)) != NULL) if (np = strchr(buffer, '\n')) *np = '\0'; return result; } #include <stdio.h> char buffer[512]= {0}; int main(int argc, char **argv) { FILE *pFile; clock_t start=0,end=0; int buftype =0; float seconds =0; pFile = fopen("C:\\lichess\\lichess_gm_2020-09.pgn","r"); if (argc >1) { buftype = atoi(argv[1]); if (buftype <0) buftype = 0; if (buftype >2) buftype = 2; } if (buftype == 0) /* full buffering */ { setvbuf ( pFile, NULL, _IOFBF, 32767); puts("Full buffering with 32 K"); } else if (buftype == 1) /* line buffering */ { setvbuf ( pFile, NULL, _IOLBF, 512); puts("line buffering with 0.5 K"); } else /* no buffering */ { setvbuf ( pFile, NULL, _IONBF, 0); puts("no buffering"); } // File operations here if (pFile) { start = clock(); while (fread(buffer, 1, sizeof buffer, pFile) > 0 ) { } end = clock(); fclose (pFile); } seconds = (float)(end - start) / CLOCKS_PER_SEC; printf("Elapsed time is %g seconds.\n", seconds); return 0; } /* G:\cc>gcc buftest.c G:\cc>a 0 Full buffering with 32 K Elapsed time is 1.027 seconds. G:\cc>a 1 line buffering with 0.5 K Elapsed time is 3.34 seconds. G:\cc>a 2 no buffering Elapsed time is 5.363 seconds. */
11/03/2021 05:39 PM 441,396,530 lichess_gm_2020-09.pgn
And as a side note. Looks like I'm mistaken about `setvbuf`, it does not affect caching by OS, that might not be possible to remove in a portable manner.