Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <time.h>
  5.  
  6. #ifdef _WIN32
  7. #  define PRTsize "Iu"
  8. #else
  9. #  define PRTsize "zu"
  10. #endif
  11.  
  12. #define ARRAY_COUNT ((size_t) 10000000)
  13. #define SINGLE_ARRAY_SIZE ((size_t) 6)
  14.  
  15. static void quick_sort(size_t, int *);
  16. static void bubble_sort(size_t, int *);
  17. static void opt_bubble_sort(size_t, int *);
  18. static void insertion_sort(size_t, int *);
  19. static void optimized_sort(size_t, int *);
  20.  
  21. #define ADD_TEST(func) { .name=#func, .sort_func=func, .time_total=0.0, .time_per_sort=0.0 }
  22. typedef struct {
  23.   const char *name;
  24.   void (*sort_func)(size_t, int*);
  25.   double time_total;
  26.   double time_per_sort;
  27. } test_run_t;
  28.  
  29. test_run_t test_runs[] = {
  30.   ADD_TEST(quick_sort),
  31.   ADD_TEST(bubble_sort),
  32.   ADD_TEST(opt_bubble_sort),
  33.   ADD_TEST(insertion_sort),
  34.   ADD_TEST(optimized_sort)
  35. };
  36. const size_t test_run_count=sizeof test_runs / sizeof *test_runs;
  37.  
  38. void do_all_runs(void);
  39. void show_runs_stats(void);
  40.  
  41. int main(void)
  42. {
  43.   printf("test %" PRTsize " loops on %" PRTsize " int arrays\n", ARRAY_COUNT, SINGLE_ARRAY_SIZE);
  44.   do_all_runs();
  45.   show_runs_stats();
  46.  
  47.   return 0;
  48. }
  49.  
  50. void do_test_run(test_run_t *);
  51. int *create_test_arrays(size_t array_count, size_t array_size);
  52.  
  53. void do_all_runs(void)
  54. {
  55.   for(size_t i=0; i<test_run_count; ++i)
  56.     do_test_run(test_runs+i);
  57. }
  58.  
  59. int compare_runs(const void *pa, const void *pb)
  60. {
  61.   return ((const test_run_t *)pa)->time_total - ((const test_run_t *)pb)->time_total;
  62. }
  63.  
  64. void show_runs_stats(void)
  65. {
  66.   puts("\n---\n");
  67.   printf("%-20s   %-9s   %-5s\n", "function", "time", "ratio");
  68.   qsort(test_runs, test_run_count, sizeof *test_runs, compare_runs);
  69.   for(size_t i=0; i<test_run_count; ++i)
  70.     printf("%-20s %8.2lf ns   %.2lf\n", test_runs[i].name,
  71.                                     test_runs[i].time_per_sort,
  72.                                     test_runs[i].time_per_sort / test_runs[0].time_per_sort
  73.           );
  74. }
  75.  
  76. void do_test_run(test_run_t *test_run)
  77. {
  78.   int *test_array=create_test_arrays(ARRAY_COUNT, SINGLE_ARRAY_SIZE);
  79.   test_run->time_total=0.0;
  80.   for(size_t i=0; i<ARRAY_COUNT; ++i) {
  81.     if (i % (ARRAY_COUNT/100) == 0) {
  82.       printf("%-20s  %02d%%\r", test_run->name, (int) ((100.0*i) / ARRAY_COUNT) );
  83.       fflush(stdout);
  84.     }
  85.     clock_t start=clock();
  86.     test_run->sort_func(SINGLE_ARRAY_SIZE, test_array+SINGLE_ARRAY_SIZE*i);
  87.     test_run->time_total += clock()-start;
  88.   }
  89.   free(test_array);
  90.   test_run->time_per_sort = test_run->time_total / CLOCKS_PER_SEC / ARRAY_COUNT * 1E9;
  91.   printf("%20s 100%% %.2lfs\n", test_run->name, test_run->time_total/CLOCKS_PER_SEC);
  92. }
  93.  
  94. int *create_test_arrays(size_t array_count, size_t array_size)
  95. {
  96.   int *new=malloc(array_count*array_size*sizeof *new);
  97.   if (new==NULL) {
  98.     perror("malloc");
  99.     exit(EXIT_FAILURE);
  100.   }
  101.   srand(0);
  102.   for(size_t i=0; i<array_count*array_size; ++i) {
  103.     if (i % (array_count*array_size/20) == 0) {
  104.       printf("Initializing test arrays %02d%%\r", (int) ((100.0*i) / (array_count*array_size)));
  105.       fflush(stdout);
  106.     }
  107.     new[i]=rand();
  108.   }
  109.   puts("test arrays initialized     ");
  110.   return new;
  111. }
  112.  
  113. static int compare_int(const void *pa, const void *pb);
  114.  
  115. static void quick_sort(size_t size, int *array)
  116. {
  117.   (void) size;
  118.   qsort(array, SINGLE_ARRAY_SIZE, sizeof *array, compare_int);
  119. }
  120.  
  121. static int compare_int(const void *pa, const void *pb)
  122. {
  123.   return (* (const int *) pa) - (* (const int *) pb);
  124. }
  125.  
  126. static void bubble_sort(size_t size, int *array)
  127. {
  128.   (void) size;
  129.   for(size_t i=SINGLE_ARRAY_SIZE-1; i>0; --i)
  130.     for(size_t j=0; j<i; ++j)
  131.       if (array[j+1]<array[j]) {
  132.         int temp=array[j+1];
  133.         array[j+1]=array[j];
  134.         array[j]=temp;
  135.       }
  136. }
  137.  
  138. static void opt_bubble_sort(size_t size, int *array)
  139. {
  140.   (void) size;
  141.   bool sorted;
  142.   for(size_t i=SINGLE_ARRAY_SIZE-1; i>0; --i) {
  143.     sorted=true;
  144.     for(size_t j=0; j<i; ++j) {
  145.       if (array[j+1]<array[j]) {
  146.         int temp=array[j+1];
  147.         array[j+1]=array[j];
  148.         array[j]=temp;
  149.         sorted=false;
  150.       }
  151.       if (sorted)
  152.         return ;
  153.     }
  154.   }
  155. }
  156.  
  157. static void insertion_sort(size_t size, int *array)
  158. {
  159.   for(size_t i=1; i<size; ++i) {
  160.     int temp=array[i];
  161.     size_t j=i;
  162.     while ( (j>0) && (array[j-1]>temp) ) {
  163.       array[j]=array[j-1];
  164.       --j;
  165.     }
  166.     array[j]=temp;
  167.   }
  168. }
  169.  
  170. static inline void compare_and_swap(int *array, size_t first, size_t second);
  171.  
  172. static void optimized_sort(size_t size, int *array)
  173. {
  174.   (void) size;
  175.   compare_and_swap(array, 1, 2);
  176.   compare_and_swap(array, 4, 5);
  177.   compare_and_swap(array, 0, 2);
  178.   compare_and_swap(array, 3, 5);
  179.   compare_and_swap(array, 0, 1);
  180.   compare_and_swap(array, 3, 4);
  181.   compare_and_swap(array, 1, 4);
  182.   compare_and_swap(array, 0, 3);
  183.   compare_and_swap(array, 2, 5);
  184.   compare_and_swap(array, 1, 3);
  185.   compare_and_swap(array, 2, 4);
  186.   compare_and_swap(array, 2, 3);
  187. }
  188.  
  189. static inline void compare_and_swap(int *array, size_t f, size_t s)
  190. {
  191.   static int temp;
  192.   if (array[f]>array[s])
  193.   {
  194.     temp=array[f];
  195.     array[f]=array[s];
  196.     array[s]=temp;
  197.   }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement