perf test for csv reading
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define LOOKUP_FILE_ROWS (400 * 1024 * 1024)
#define LOOKUP_FILE_NAME "lookup_test.csv"
#define LOOKUP_LINE_SIZE 1024
/**
* This macro represents an inlined, identical portion of code used to time any function.
* This is such that different abstractions of timings are not affected by function call overhead.
*/
#define TIMING_FUNCTION_CODE(fn, result) do { \
clock_t start, end; \
start = clock(); \
(fn)(); \
end = clock(); \
(result) = ((double) (end - start)) / CLOCKS_PER_SEC; \
} while(0)
static void make_csv_test(void)
{
FILE* fp;
size_t i;
fp = fopen(LOOKUP_FILE_NAME, "w");
if(fp == NULL) {
fprintf(stderr, "Cannot open file %s, exiting\n", LOOKUP_FILE_NAME);
exit(2);
}
for(i = 0 ; i < LOOKUP_FILE_ROWS; i++) {
fprintf(fp, "%d,%d,%d\n", 1333003, 1234280, 1122040);
}
fclose(fp);
}
static void read_and_count_csv_rows(void)
{
FILE* fp;
const char* line_reader;
char* field_writer;
size_t fields_counted;
char line_buf[LOOKUP_LINE_SIZE];
char field_buf[64];
fields_counted = 0;
// safe zero'd memory
memset(line_buf, 0, sizeof(line_buf));
memset(field_buf, 0, sizeof(field_buf));
fp = fopen(LOOKUP_FILE_NAME, "r");
if(fp == NULL) {
fprintf(stderr, "Cannot open file %s, exiting\n", LOOKUP_FILE_NAME);
exit(2);
}
while(!feof(fp)) {
size_t field_written = 0;
fgets(line_buf, LOOKUP_LINE_SIZE, fp);
line_reader = line_buf;
field_writer = field_buf;
while((field_written < sizeof(field_buf)) && *line_reader != ',') {
*field_writer++ = *line_reader++;
field_written++;
}
fields_counted++;
}
fclose(fp);
}
int main(void) {
double time_taken = 0.0;
puts("-- Creating csv lookup test file --");
make_csv_test();
puts("-- finished creating csv lookup test file --");
printf(" -- the test will read %lu rows from %s\n", LOOKUP_FILE_ROWS, LOOKUP_FILE_NAME);
puts("-- starting timed lookup test --");
TIMING_FUNCTION_CODE(read_and_count_csv_rows, time_taken);
printf("The lookup csv test took %f seconds\n", time_taken);
remove(LOOKUP_FILE_NAME);
return 0;
}