Why is fgets som much slower than fread? I am don’t really know; I haven’t looked into the code so I really don’t know what it does, but I guess it reads a buffer, scans through the text until it finds new line character or end of file or comes to the end of buffer and puts null character to terminate the string and returns pointer to buffer to user. Does it save buffer in case when strings are shorter, or does it discard the rest of buffer after new line?
I tried to write a little test where I am doing above mention but on buffer that I read with fread instead. Even if I am processing through the buffer, I am still getting consistently much better results if I am using fread instead of fgets.
For the fun I have also included test with sse2 optimized fgets, which I stumbled upon. It actually beatsfread. I don’t get 4x speedups as claimed in Readme file, but it still does better than fread:
With fread: duration 0: 0.957747 duration 1: 0.869212 duration 2: 0.842793 Mean value: 0.889917 With fgets: duration 0: 1.258209 duration 1: 1.258632 duration 2: 1.258991 Mean value: 1.258611 With sse2 optimizied fgets: duration 0: 0.661175 duration 1: 0.661901 duration 2: 0.661526 Mean value: 0.661534
Above are my results on 100M big ASCII test with random generated characters. Everything is measured 3 time and mean value is calculated. Time repported is in seconds. I did some “warm up” on file so it was in cache before measuring was done. Cpu is 2.4 ghz i7, 16 gig ram, compilers was VS 2012 with optimization and sse turned on. Test code can be seen. I have also modified slightly code for sse2 optimized fgets (included in zip), to compile with VS, and you will also need code for high precision timer from Intel’s site.
Post a Comment