Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Illustrates the use of xbt_swag_offset
[simgrid.git] / testsuite / xbt / heap_bench.c
1 /* A few tests for the xbt_heap module                                      */
2
3 /* Authors: Arnaud Legrand                                                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6    under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <sys/time.h>
11 #include "xbt/heap.h"
12
13 #define MAX_TEST 1000000
14
15 /* Pour le bench */
16 long us_time(void);
17 long us_time(void)
18 {
19   struct timeval start;
20   gettimeofday(&start, NULL);
21
22   return (start.tv_sec * 1000000 + start.tv_usec);
23 }
24
25 int compare_xbt_heap_float_t(const void *a, const void *b);
26 void test_heap_validity(int size);
27 void test_heap_mean_operation(int size);
28
29 int compare_xbt_heap_float_t(const void *a, const void *b)
30 {
31   xbt_heap_float_t pa, pb;
32
33   pa = *((xbt_heap_float_t *) a);
34   pb = *((xbt_heap_float_t *) b);
35
36   if (pa > pb)
37     return 1;
38   if (pa == pb)
39     return 0;
40   return -1;
41 }
42
43 void test_heap_validity(int size)
44 {
45   xbt_heap_t heap = xbt_heap_new(size, NULL);
46   xbt_heap_float_t *tab = calloc(size, sizeof(xbt_heap_float_t));
47   int i;
48
49   for (i = 0; i < size; i++) {
50     tab[i] = (10.0 * rand() / (RAND_MAX + 1.0));
51     xbt_heap_push(heap, NULL, tab[i]);
52   }
53
54   qsort(tab, size, sizeof(xbt_heap_float_t), compare_xbt_heap_float_t);
55
56   for (i = 0; i < size; i++) {
57     /*     printf(XBT_HEAP_FLOAT_T " ", xbt_heap_maxkey(heap)); */
58     if (xbt_heap_maxkey(heap) != tab[i]) {
59       fprintf(stderr, "Problem !\n");
60       exit(1);
61     }
62     xbt_heap_pop(heap);
63   }
64   xbt_heap_free(heap);
65   free(tab);
66   printf("Validity test complete!\n");
67 }
68
69 void test_heap_mean_operation(int size)
70 {
71   xbt_heap_t heap = xbt_heap_new(size, NULL);
72   xbt_heap_float_t val;
73   long date = 0;
74   int i, j;
75
76   date = us_time();
77   for (i = 0; i < size; i++)
78     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
79   date = us_time() - date;
80   printf("Creation time  %d size heap : %g\n", size, 0.0 + date);
81
82   date = us_time();
83   for (j = 0; j < MAX_TEST; j++) {
84     val = xbt_heap_maxkey(heap);
85     xbt_heap_pop(heap);
86     xbt_heap_push(heap, NULL, 3.0 * val);
87   }
88   date = us_time() - date;
89   printf("Mean access time for a %d size heap : %g\n", size,
90          date * 1.0 / (MAX_TEST + 0.0));
91
92   xbt_heap_free(heap);
93 }
94
95 int main(int argc, char **argv)
96 {
97   int size;
98   for (size = 100; size < 10000; size *= 10) {
99     test_heap_validity(size);
100     test_heap_mean_operation(size);
101   }
102   return 0;
103 }