Q:

Speed Tradeoff: Frequently Reading from file vs storing it using dynamic memory

I am writing a C program which involves reading a image file and reading each pixel of image just once. So should i read file once using fread() and store it in some dynamic variable(heap variable) or frequeently use fread() for each pixel??
Image will be of size 1000*1000 to 5000*5000.
 I will be extending the same program in MPI and CUDA. I would appreciate any other suggestions.
Thank you.

A:

Even a 12-bit colour ARGB image would need about 150 MB for a 5,000 * 5,000 pixel resolution, which is well within the capabilities of all current PCs and even many GPU cards. If you have that kind of memory available, you should read it once in a dynamically allocated array, or something along those lines. It would allow you to read the whole image in big I/O blocks, which is faster, and use direct memory operations (img[1234][4321][RED] = 34), rather than complicate your code with I/O functions.
If you do not have that kind of memory available, look at mmap() or whatever equivalent exists for your OS to map the file into virtual memory. You still have the advantage of using direct memory operations, without necessarily loading the whole thing in memory, although it would be computationally more expensive.
That said, modern OS perform extensive caching and prefetching of data, therefore using fread() may not be that much slower. Moreover, on current Linux systems with glibc-2.3 or later, it is optionally possible to use mmap() for file access, even when the application performs I/O with standard stdio functions.

