From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9c86eb13dd395066 X-Google-Attributes: gid103376,public From: ohk@edeber.tfdt-o.nta.no (Ole-Hjalmar Kristensen FOU.TD/DELAB) Subject: Re: CRC in Ada? Date: 1997/03/07 Message-ID: #1/1 X-Deja-AN: 223727971 References: <1997Mar2.220652@nova.wright.edu> Organization: Telenor Online Public Access Newsgroups: comp.lang.ada Date: 1997-03-07T00:00:00+00:00 List-Id: In article dewar@merv.cs.nyu.edu (Robert Dewar) writes: Fergus asks <<>In my experience it is generally C programmers who make this mistake. Why is it a mistake?>> Tell you what Fergus, do a whole lot of timing tests comparing reading character by character and reading large blocks, and come back and tell us if it did not tell you the answer to that questin :-) OK, I'll bite: The following two simple programs have been tested on a 24443068 byte file, both compiled wit -O7 on the Centerline C compiler: #include main() { setvbuf(stdin,0,_IOFBF,4096); int c,total=0; while ((c = getc(stdin)) != EOF) total++; printf("total %d\n",total); } Average results obtained with time: 2.28u 0.28s 0:02.66 96.2% char buf[4096]; main() { int c,n,total=0; while ((n = read(0,buf,sizeof(buf))) > 0) { int i; for (i = 0; i < n; i++) { c = buf[i]; total++; } } printf("total %d\n",total); } Average results obtained with time: 0.12u 0.29s 0:00.41 100.0% As you can see, the time used by the OS is pretty much the same in both cases. It is somewhat surprising that the difference between the user CPU times is so large, but I'm more surprised by the speed of the second version than by the slowness of the first. Depending on what your program is really going to do with the 24443068 bytes, the 2.5 seconds spent in the C standard IO library may or may not be significant....