Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define LOOKUP_FILE_ROWS (400 * 1024 * 1024)
  7. #define LOOKUP_FILE_NAME "lookup_test.csv"
  8.  
  9. #define LOOKUP_LINE_SIZE 1024
  10.  
  11.  
  12. /**
  13. * This macro represents an inlined, identical portion of code used to time any function.
  14. * This is such that different abstractions of timings are not affected by function call overhead.
  15. */
  16.  
  17. #define TIMING_FUNCTION_CODE(fn, result) do { \
  18. clock_t start, end; \
  19. start = clock(); \
  20. (fn)(); \
  21. end = clock(); \
  22. (result) = ((double) (end - start)) / CLOCKS_PER_SEC; \
  23. } while(0)
  24.  
  25. static void make_csv_test(void)
  26. {
  27. FILE* fp;
  28. size_t i;
  29. fp = fopen(LOOKUP_FILE_NAME, "w");
  30. if(fp == NULL) {
  31. fprintf(stderr, "Cannot open file %s, exiting\n", LOOKUP_FILE_NAME);
  32. exit(2);
  33. }
  34. for(i = 0 ; i < LOOKUP_FILE_ROWS; i++) {
  35. fprintf(fp, "%d,%d,%d\n", 1333003, 1234280, 1122040);
  36. }
  37. fclose(fp);
  38. }
  39.  
  40. static void read_and_count_csv_rows(void)
  41. {
  42. FILE* fp;
  43. const char* line_reader;
  44. char* field_writer;
  45. size_t fields_counted;
  46. char line_buf[LOOKUP_LINE_SIZE];
  47. char field_buf[64];
  48. fields_counted = 0;
  49. // safe zero'd memory
  50. memset(line_buf, 0, sizeof(line_buf));
  51. memset(field_buf, 0, sizeof(field_buf));
  52. fp = fopen(LOOKUP_FILE_NAME, "r");
  53. if(fp == NULL) {
  54. fprintf(stderr, "Cannot open file %s, exiting\n", LOOKUP_FILE_NAME);
  55. exit(2);
  56. }
  57. while(!feof(fp)) {
  58. size_t field_written = 0;
  59. fgets(line_buf, LOOKUP_LINE_SIZE, fp);
  60. line_reader = line_buf;
  61. field_writer = field_buf;
  62. while((field_written < sizeof(field_buf)) && *line_reader != ',') {
  63. *field_writer++ = *line_reader++;
  64. field_written++;
  65. }
  66. fields_counted++;
  67. }
  68. fclose(fp);
  69. }
  70.  
  71. int main(void) {
  72. double time_taken = 0.0;
  73. puts("-- Creating csv lookup test file --");
  74. make_csv_test();
  75. puts("-- finished creating csv lookup test file --");
  76. printf(" -- the test will read %lu rows from %s\n", LOOKUP_FILE_ROWS, LOOKUP_FILE_NAME);
  77. puts("-- starting timed lookup test --");
  78. TIMING_FUNCTION_CODE(read_and_count_csv_rows, time_taken);
  79. printf("The lookup csv test took %f seconds\n", time_taken);
  80. remove(LOOKUP_FILE_NAME);
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement