Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 2. Reading and writing binary data is much faster than reading and writing

ID: 3822892 • Letter: P

Question

Part 2.  Reading and writing binary data is much faster than reading and writing text data.

Here is a program which does the following:

a. creates a char array 64 MB long (like 8-bit pixels, for example).
b. initializes it to random values between -128 and 127
c. writes the values to a data file as (text) integer values (like we have done with our PGM images)

Run this program and time how long it takes to run. (If it takes less than a few seconds, double or quadruple the size of the array.)

Then, write functions to write out and read back in the same data in binary format.  Run your program and time how long it takes to run.  Calculate approximately how much faster it is to read and write binary, and what the difference in size is between the ascii file and the binary file.  Add a few lines to your binary_input_output_notes.txt file giving the comparisons.  Then estimate how long it would take to read a 10 GB ascii PGM, and how long it would take to read the equivalent 2.5 GB binary PGM.

HereÔøs my solution, if you get stuck.

Name your output binary_timing.cpp

HERE IS THE PROGRAM MENTIONED ABOVE:

Explanation / Answer

/* file: binary_timing.cpp
lab 10 exercise
program to demonstrate to the student how much faster it is to write (and read)
a data file in binary than in ascii.
  
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <time.h>

using namespace std;

const int SIZE=64*1048576; // 64 * 1024*1024, 64 MegBytes
void initialize_dat(char dat[], int size);
void write_ascii_data(const char dat[], int size);
void read_ascii_data(char dat[], int size);
/*
void write_binary_data(const char dat[], int size);
void read_binary_data(char dat[], int size);
*/

int main( )
{
char *dat = new char[SIZE];
   clock_t start, end;
   double cpu_time_used;
if (! dat) {cerr << "cannot alloc dat array"<<endl; return 1;}
  
initialize_dat(dat, SIZE); // fill it with random numbers

// open a file for writing and write 64 MB of data into it
start = clock();
cout << "writing 64 MB of 8-bit integer data in ascii"<<endl;
write_ascii_data(dat, SIZE);
cout << "reading 64 MB of 8-bit integer data in ascii"<<endl;
read_ascii_data(dat, SIZE);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

cout<<"time taken to read and write in ASCII Format"<< cpu_time_used<<endl;
/* Now write and read data in binary format.
// you need to write the functions
// write_binary_data( ) and read_binary_data(...)
*/
start = clock();   
cout << "writing 64 MB of 8-bit integer data in binary"<<endl;
write_binary_data(dat, SIZE);
cout << "reading 64 MB of 8-bit integer data in binary"<<endl;
read_binary_data(dat, SIZE);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
   cout<<"time taken to read and write in BINARY Format"<< cpu_time_used<<endl;
#ifdef WIN32
system("pause");
#endif
return 0;
}

void initialize_dat(char dat[], int size)
{
for (int i=0;i<size; i++)
dat[i] = rand()%256 - 128; // get random # between -128 and 127.
}
void write_ascii_data(const char *dat, int size)
{
ofstream ofs("ascii.txt");
for (int i=0;i<size;i++) // output dat as integer values (like pixels?)
{
ofs << setw(4) << static_cast<int>(dat[i]);
if (i%16==15) ofs << endl;
else ofs << ' ';
if(i%(1024*1024)==0) {cout << " " << i/1024/1024<<" MB text written"; cout.flush();} // write something every MB so we know something is happening
// cout.flush( ) flushes the output buffer, so the output appears on the screen immediately.
}
cout << "done writing text"<<endl;
}

void read_ascii_data(char *dat, int size)
{
ifstream ifs("ascii.txt");
for (int i=0;i<size;i++)
{
int val;
ifs >> val; // read number into an integer
dat[i] = static_cast<char>(val); // convert it to a char.
if(i%(1024*1024)==0) {cout << " " << i/1024/1024<<" MB text read in"; cout.flush();} // write something every MB so we know something is happening
}
cout << "done reading text"<<endl;
}

/* finish these two functions*/

void write_binary_data(const char *dat, int size)
{
ofstream ofb("binary.txt");
ofb.write( dat,size);
cout << "done writing text"<<endl;
}

void read_binary_data(char *dat, int size)
{
ifstream ifb("binary.txt");
   ifb.read( dat, sixe );
cout << "done reading 64 MB binary";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote