Skip to content

Instantly share code, notes, and snippets.

@benkirk
Last active December 28, 2015 06:09
Show Gist options
  • Save benkirk/7455182 to your computer and use it in GitHub Desktop.
Save benkirk/7455182 to your computer and use it in GitHub Desktop.
tests xdr & native C binary I/O performance for vector and element-wise access.
// g++ -O2 -o test_xdr test_xdr.C ; time ./test_xdr
#include <iostream>
#include <vector>
#include <rpc/rpc.h>
#include <ctime>
#include <sys/time.h>
int main (int argc, char **argv)
{
#define BUFSIZE 1024*1000*100
std::vector<double> data(BUFSIZE);
timeval tstart, tstop;
// write one big vector, C
{
FILE *fp = fopen("test0.dat", "w");
gettimeofday (&tstart, NULL);
fwrite (&data[0],
sizeof(double),
data.size(),
fp);
fflush (fp);
fclose (fp);
gettimeofday (&tstop, NULL);
const double elapsed_time = (static_cast<double>(tstop.tv_sec - tstart.tv_sec) +
static_cast<double>(tstop.tv_usec - tstart.tv_usec)*1.e-6);
std::cout << "C fwrite:\n";
std::cout << "Wrote " << BUFSIZE*sizeof(double)/1.e6
<< "MB in " << elapsed_time << " seconds ("
<< (BUFSIZE*sizeof(double)/1.e6/elapsed_time) << ") MB/sec.\n";
}
// write each element individually, XDR
{
FILE *fp = fopen("test1.dat", "w");
gettimeofday (&tstart, NULL);
for (unsigned int cnt=0; cnt<data.size(); cnt++)
{
fwrite (&data[cnt],
sizeof(double),
1,
fp);
}
fflush (fp);
fclose (fp);
gettimeofday (&tstop, NULL);
const double elapsed_time = (static_cast<double>(tstop.tv_sec - tstart.tv_sec) +
static_cast<double>(tstop.tv_usec - tstart.tv_usec)*1.e-6);
std::cout << "C fwrite:\n";
std::cout << "Wrote " << BUFSIZE*sizeof(double)/1.e6
<< "MB in " << elapsed_time << " seconds ("
<< (BUFSIZE*sizeof(double)/1.e6/elapsed_time) << ") MB/sec.\n";
}
// write one big vector, XDR
{
XDR *xdrs = new XDR;
FILE *fp = fopen("test1.xdr", "w");
xdrstdio_create (xdrs, fp, XDR_ENCODE);
gettimeofday (&tstart, NULL);
xdr_vector(xdrs,
(char*) &data[0],
data.size(),
sizeof(double),
(xdrproc_t) xdr_double);
xdr_destroy (xdrs);
delete xdrs;
fflush (fp);
fclose (fp);
gettimeofday (&tstop, NULL);
const double elapsed_time = (static_cast<double>(tstop.tv_sec - tstart.tv_sec) +
static_cast<double>(tstop.tv_usec - tstart.tv_usec)*1.e-6);
std::cout << "XDR write:\n";
std::cout << "Wrote " << BUFSIZE*sizeof(double)/1.e6
<< "MB in " << elapsed_time << " seconds ("
<< (BUFSIZE*sizeof(double)/1.e6/elapsed_time) << ") MB/sec.\n";
}
// write each element individually, XDR
{
XDR *xdrs = new XDR;
FILE *fp = fopen("test2.xdr", "w");
xdrstdio_create (xdrs, fp, XDR_ENCODE);
gettimeofday (&tstart, NULL);
for (unsigned int cnt=0; cnt<data.size(); cnt++)
{
xdr_vector(xdrs,
(char*) &data[cnt],
1,
sizeof(double),
(xdrproc_t) xdr_double);
}
xdr_destroy (xdrs);
delete xdrs;
fflush (fp);
fclose (fp);
gettimeofday (&tstop, NULL);
const double elapsed_time = (static_cast<double>(tstop.tv_sec - tstart.tv_sec) +
static_cast<double>(tstop.tv_usec - tstart.tv_usec)*1.e-6);
std::cout << "XDR write:\n";
std::cout << "Wrote " << BUFSIZE*sizeof(double)/1.e6
<< "MB in " << elapsed_time << " seconds ("
<< (BUFSIZE*sizeof(double)/1.e6/elapsed_time) << ") MB/sec.\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment