Skip to content

Instantly share code, notes, and snippets.

@tanakamura
Created January 19, 2017 15:31
Show Gist options
  • Save tanakamura/91209ae8e5275dff7331b2b8db474032 to your computer and use it in GitHub Desktop.
Save tanakamura/91209ae8e5275dff7331b2b8db474032 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
double
getsec(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec / 1000000000.0;
}
void test(int SZ)
{
char data[SZ];
double t0, t1;
FILE *a = fopen("a.txt", "wb");
fwrite(data, 1, SZ, a);
fclose(a);
int h = open("a.txt", O_RDWR);
for (int i=0; i<4; i++) {
int nloop = 4096;
t0 = getsec();
for (int li=0; li<nloop; li++) {
pread(h, data, SZ, 0);
}
t1 = getsec();
printf("rd %f[usec]\n", ((t1-t0)/nloop)*1000000.0);
}
for (int i=0; i<4; i++) {
int nloop = 4096;
t0 = getsec();
for (int li=0; li<nloop; li++) {
pwrite(h, data, SZ, 0);
}
t1 = getsec();
printf("wr %f[usec]\n", ((t1-t0)/nloop)*1000000.0);
}
close(h);
}
int main()
{
test(1);
test(1024);
test(1024*1024);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment