fork(5) download
  1. #include <stdio.h> /* printf, scanf, NULL */
  2. #include <stdlib.h> /* calloc, exit, free */
  3. #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
  4.  
  5. const size_t c_array_size = 10000000;
  6.  
  7. /* Fields */
  8. enum T_field_enum { amount_of_money_e, gender_e, age_e, code_e, height_e, /*<<<<<- add fields here */ last_e };
  9.  
  10. /* Row */
  11. struct T_cash_account_row {
  12. unsigned code:20; // 0 - 1000000
  13. unsigned gender:1; // 0 - 1
  14. unsigned age:7; // 0 - 100
  15. unsigned amount_of_money:20;// 0 - 1000000
  16. unsigned height:9; // 0 – 300
  17. };
  18. /* ----------------------------------------------------------------------- */
  19.  
  20. /* Generate random data for the one row */
  21. static inline struct T_cash_account_row generate_row() {
  22. struct T_cash_account_row cash_account_row;
  23. cash_account_row.age = rand() % 100;
  24. cash_account_row.amount_of_money = (rand() % 1000)*(rand() % 1000);
  25. cash_account_row.code = (rand() % 1000)*(rand() % 1000);
  26. cash_account_row.gender = rand() % 2;
  27. cash_account_row.height = rand() % 300;
  28. return cash_account_row;
  29. }
  30. /* ----------------------------------------------------------------------- */
  31.  
  32. /* Filters */
  33. struct T_range_filters {
  34. struct T_cash_account_row begin, end;
  35. /* bytes array or bitset from https://g...content-available-to-author-only...b.com/jmbr/667605 */
  36. unsigned char use_filter[last_e];
  37. };
  38. /* ----------------------------------------------------------------------- */
  39.  
  40. /* Compare row with filters */
  41. static inline unsigned char test_predicate(struct T_cash_account_row const*const row,
  42. struct T_range_filters const*const range_filters)
  43. {
  44. return
  45. (!range_filters->use_filter[amount_of_money_e] ||
  46. (row->amount_of_money >= range_filters->begin.amount_of_money &&
  47. row->amount_of_money <= range_filters->end.amount_of_money)) &&
  48. (!range_filters->use_filter[gender_e] ||
  49. (row->gender >= range_filters->begin.gender && row->gender <= range_filters->end.gender)) &&
  50. (!range_filters->use_filter[age_e] ||
  51. (row->age >= range_filters->begin.age && row->age <= range_filters->end.age)) &&
  52. (!range_filters->use_filter[code_e] ||
  53. (row->code >= range_filters->begin.code && row->code <= range_filters->end.code)) &&
  54. (!range_filters->use_filter[height_e] ||
  55. (row->height >= range_filters->begin.height && row->height <= range_filters->end.height));
  56. }
  57. /* ----------------------------------------------------------------------- */
  58.  
  59. /* search */
  60. static inline size_t search(struct T_cash_account_row const*const array_ptr, const size_t c_array_size,
  61. struct T_cash_account_row *const result_ptr, struct T_range_filters const*const range_filters)
  62. {
  63. size_t result_size = 0;
  64. size_t i; /* loop index */
  65. for(i = 0; i < c_array_size; ++i) {
  66. if(test_predicate(array_ptr + i, range_filters))
  67. result_ptr[result_size] = array_ptr[i], ++result_size;
  68. }
  69. return result_size;
  70. }
  71. /* ----------------------------------------------------------------------- */
  72.  
  73. int main() {
  74.  
  75. size_t i; /* loop index */
  76. struct T_cash_account_row *const array_ptr = ( struct T_cash_account_row *)calloc(c_array_size, sizeof(struct T_cash_account_row));
  77. if (array_ptr == NULL) {
  78. printf ("calloc error\n");
  79. exit(1);
  80. }
  81.  
  82. /* initialize random seed: */
  83. /* srand (time(NULL)); */
  84.  
  85. /* Fill table random data */
  86. for(i = 0; i < c_array_size; ++i)
  87. array_ptr[i] = generate_row();
  88. printf ("Generated rows: %d \n", c_array_size);
  89.  
  90. struct T_range_filters range_filters = {};
  91.  
  92. range_filters.use_filter[amount_of_money_e] = rand()%1 + 0;
  93. range_filters.use_filter[gender_e] = rand()%1 + 0;
  94. range_filters.use_filter[height_e] = rand()%1 + 0;
  95.  
  96. range_filters.begin.age = rand() % 100;
  97. range_filters.end.age = range_filters.begin.age + 5;
  98. range_filters.use_filter[age_e] = rand()%1 + 1;
  99.  
  100. range_filters.begin.code = rand() % 30000;
  101. range_filters.end.code = range_filters.begin.code + 5;
  102. range_filters.use_filter[code_e] = rand()%1 + 1;
  103.  
  104. struct T_cash_account_row *const result_ptr = ( struct T_cash_account_row *)calloc(c_array_size, sizeof(struct T_cash_account_row));
  105.  
  106. size_t result_size;
  107. clock_t end, start;
  108.  
  109. printf ("C-Searching...\n");
  110.  
  111. start = clock();
  112. result_size = search(array_ptr, c_array_size, result_ptr, &range_filters);
  113. end = clock();
  114. float c_took_time = (float)(end - start);
  115. printf ("C-search took %f seconds.\n", c_took_time/CLOCKS_PER_SEC);
  116.  
  117. printf ("Found rows: %d \n", result_size);
  118.  
  119. /*for(i = 0; i < result_size; ++i) {
  120. printf ("%d, %d, %d, %d, %d \n",
  121. result_ptr[i].age, result_ptr[i].amount_of_money, result_ptr[i].code, result_ptr[i].gender, result_ptr[i].height);
  122. }*/
  123.  
  124. int qwerty;
  125. scanf ("%d",&qwerty);
  126. return 0;
  127. }
Success #stdin #stdout 1.98s 159168KB
stdin
Standard input is empty
stdout
Generated rows: 10000000 
C-Searching...
C-search took 0.040000 seconds.
Found rows: 38