0

I was solving a practice problem on a site which states that

The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime.

Also how do I optimize input/output routines other than printf and scanf?

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
nishant agrawal
  • 471
  • 2
  • 7

1 Answers1

2

It is operating system specific (because the C standard only knows about <stdio.h>). With Linux consider using low-level syscalls for efficiency, like open(2), mmap(2), read(2), pread(2), write(2). You might also want to use readahead(2). Don't forget to make I/O in rather large blocks (e.g. 128Kbytes), page aligned if possible. Read the Advanced Linux Programming book.

If restricted to standard C99 functions, use fread(3) on rather big chunks. Consider also increasing the internal buffer with setvbuf(3)

And 2.5Mbyte/sec is not very impressive. Probably, the bottleneck is the hardware, but you should be able to get perhaps 20 or 50Mbytes/sec on a standard desktop hardware. Using SSD would help a big lot.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Yes, unbuffered I/O with paged-sized (or slightly less), page-aligned chunks that are also a multiple of the disk sector size is a recipe for speed. You have to make it worthwhile by processing that data efficiently. – paddy Oct 26 '12 at 20:11
  • i am new to c programming and don't know much so unable to get all technical terms – nishant agrawal Oct 26 '12 at 20:15
  • Try to code something. If on Linux, use `gcc -Wall -g` to compile it. Debug it with a debugger (e.g. `gdb`). Run it, and test it on large (megabyte sized) files. – Basile Starynkevitch Oct 26 '12 at 20:17
  • i am working on a windows system can you tell me how to find the runtime of my program so that i can compare the performances – nishant agrawal Oct 26 '12 at 20:18