Binary I/O Benchmark

So, I have been wanting to learn how to save data  into a binary file so that I can load massive amounts of data extremely quick. I figured that the best way to go about this would be creating an example program that could do exactly that. So I started with this program originally to get binary reading and writing working correctly… The most confusing part at first was how to store the dynamic array of integers I was using. I didnt know whether or not we could write the whole block of memory into the binary file or if we had to read through each record and store it individually (which ended up being the case.) After a little playing around, I was able to figure out that this was an extremely easy task because I could set up the template for reading/writing. I had the program create a struct like the following:

	typedef struct
	{
		int * data;
		int size;
	}OBJECT;

So in order to store this struct with ’size’ amount of records, we need to come up with a template like this:

FILE *fp;
OBJECT p;
fp = fopen("data.bin", "wb");

if(fp != NULL)
{
	fwrite(p.size, (size_t)sizeof(int), 1, fp);

	for(int x = 0; x /*less than*/ p.size; x++)
	{
		fwrite(p.data[x], (size_t)sizeof(int), 1, fp);
	}
}

fclose(fp);

When we want to read the file in, we need to read it in the same way, like this:

FILE *fp;

OBJECT p = (OBJECT*)malloc(sizeof(OBJECT));
p.size = 0;
p.data = (int*)realloc(p.data, p.size * sizeof(int));

fp = fopen("data.bin", "rb");

if(fp != NULL)
{
	fread(p.size, (size_t)sizeof(int), 1, fp);
	p.data = (int*)realloc(p.data, p.size * sizeof(int));

	for(int x = 0; x /*less than*/ p.size; x++)
	{
		fread(p.data[x], (size_t)sizeof(int), 1, fp);
	}
}

fclose(fp);
free(p);

Long story short, I came up with this program to run a benchmark to determine how effective reading/writing a binary file is in comparison to reading/writing a normal text file. The more records you have the program generate, the more obvious the difference between the two is. On a sample benchmark using 10,000,000 records, I got the following:

With this result, the Binary Load performed 3.35x better than the Text Load. That’s AMAZING considering its 10,000,000 records. Anyways, you can download the program using the links below if you’d like to test it for yourself by running the  benchmark on your PC.

Download Here: Binary I/O