Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
heap bench project source file
[simgrid.git] / win32_testsuite / borland / builder6 / simulation / xbt / heap_bench / heap_bench.c
1 //---------------------------------------------------------------------------
2
3 #pragma hdrstop
4
5 /*#include "..\..\..\..\..\..\include\xbt\dict.h"*/
6
7 /* $Id$ */
8
9 /* A few tests for the xbt_heap module                                      */
10
11 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
12
13 /* This program is free software; you can redistribute it and/or modify it
14  * under the terms of the license (GNU LGPL) which comes with this package. */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include "xbt/heap.h"
20 #include "gras/virtu.h"         /* time manipulation in bench */
21
22 #define MAX_TEST 1000000
23
24 int _XBT_CALL compare_double(const void *a, const void *b);
25 void test_heap_validity(int size);
26 void test_heap_mean_operation(int size);
27 void test_reset_heap(xbt_heap_t heap,int size);
28
29
30 int _XBT_CALL compare_double(const void *a, const void *b)
31 {
32         double pa, pb;
33         
34         pa = *((double *) a);
35         pb = *((double *) b);
36         
37         if (pa > pb)
38                 return 1;
39         
40         if (pa == pb)
41                 return 0;
42         
43         return -1;
44 }
45
46 void test_heap_validity(int size)
47 {
48         xbt_heap_t heap = xbt_heap_new(size, NULL);
49         double *tab = calloc(size, sizeof(double));
50         int i;
51         
52         for (i = 0; i < size; i++) 
53         {
54                 tab[i] = (10.0 * rand() / (RAND_MAX + 1.0));
55                 xbt_heap_push(heap, NULL, tab[i]);
56         }
57         
58         qsort(tab, size, sizeof(double), compare_double);
59         
60         for (i = 0; i < size; i++) 
61         {
62                 /*     printf("%lg" " ", xbt_heap_maxkey(heap)); */
63                 if (xbt_heap_maxkey(heap) != tab[i]) 
64                 {
65                         fprintf(stderr, "Problem !\n");
66                         exit(1);
67                 }
68                 
69                 xbt_heap_pop(heap);
70         }
71         
72         xbt_heap_free(heap);
73         free(tab);
74         printf("Validity test complete!\n");
75 }
76
77 void test_heap_mean_operation(int size)
78 {
79         xbt_heap_t heap = xbt_heap_new(size, NULL);
80         double val;
81         double date = 0;
82         int i, j;
83         
84         date = gras_os_time() * 1000000;
85
86         for (i = 0; i < size; i++)
87                 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
88         
89         date = gras_os_time() * 1000000 - date;
90         
91         printf("Creation time  %d size heap : %g\n", size, date);
92         
93         date = gras_os_time() * 1000000;
94         
95         for (j = 0; j < MAX_TEST; j++) 
96         {
97         
98                 if(!(j%size) && j)
99                         test_reset_heap(heap,size);
100         
101                 val = xbt_heap_maxkey(heap);
102                 xbt_heap_pop(heap);
103                 xbt_heap_push(heap, NULL, 3.0 * val);
104         }
105         
106         date = gras_os_time() * 1000000 - date;
107         printf("Mean access time for a %d size heap : %g\n", size,date * 1.0 / (MAX_TEST + 0.0));
108         
109         xbt_heap_free(heap);
110 }
111
112 void test_reset_heap(xbt_heap_t heap,int size)
113 {
114         int i;
115         xbt_heap_free(heap);
116         heap = xbt_heap_new(size, NULL);
117         
118         for (i = 0; i < size; i++)
119         {
120                 xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
121         }
122 }
123
124
125 #pragma argsused
126
127 int main(int argc, char **argv)
128 {
129         int size;
130
131         for (size = 100; size < 10000; size *= 10)
132         {
133                 test_heap_validity(size);
134                 test_heap_mean_operation(size);
135         }
136         return 0;
137 }
138 //---------------------------------------------------------------------------