Reputation: 301
I want to benchmark an algorithm which operates on a file. The algorithm iterates N rounds and in each round, it reads one data block, do some magic stuff and set the offset of the next block.
Here's the pseudo code:
int run_algorithm(int offset) {
char *fname = "database.dat";
fd = open(fname, O_RDONLY);
// read the desired block to memory
block_size = 1024 * 1024;
char *buf = malloc(block_size);
lseek(fd, offset, SEEK_SET);
read(fd, &buf, block_size);
int new_offset;
// do magic stuff with buf
// and set a new offset
close(fd);
return new_offset;
}
int main() {
int i;
//init offset
int offset = 0;
// iterate N times
for (i = 0; i < N; i++) {
offset = run_algorithm(offset);
}
return 0;
}
I know the operating system has warm cache and cold cache. I want to implement the cold cache case. In each run_algorithm() call, there should be no buffering when the file is open. In other words, I don't want part of the file to be stored somewhere in the memory by the operating system to speed up the open() and seek().
Is there a way to specifically set the open() and seek() without buffering?
Upvotes: 3
Views: 2848
Reputation: 13690
You can't disable alll caches in the harddrive, and in the operating system.
But if you accept that you benchmark everything that's outside your program then you can disable buffering in the C run-time library:
setvbuf(fd, NULL, _IONBF, 0);
You must call the function immediatly after the fopen operation. See details in the cpp reference page.
Upvotes: 3