source: doc/generic_types/evaluation/c-bench.c @ da1c772

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since da1c772 was 87c5f40, checked in by Aaron Moss <a3moss@…>, 7 years ago

Update benchmarks to include stack of pairs

  • Property mode set to 100644
File size: 2.0 KB
Line 
1#include <stdlib.h>
2#include "bench.h"
3#include "c-pair.h"
4#include "c-stack.h"
5
6_Bool* new_bool( _Bool b ) {
7        _Bool* q = malloc(sizeof(_Bool));
8        *q = b;
9        return q;
10}
11
12char* new_char( char c ) {
13        char* q = malloc(sizeof(char));
14        *q = c;
15        return q;
16}
17
18int* new_int( int i ) {
19        int* q = malloc(sizeof(int));
20        *q = i;
21        return q;
22}
23
24void* copy_bool( const void* p ) { return new_bool( *(const _Bool*)p ); }
25
26void* copy_char( const void* p ) { return new_char( *(const char*)p ); }
27
28void* copy_int( const void* p ) { return new_int( *(const int*)p ); }
29
30void* copy_pair_bool_char( const void* p ) { return copy_pair( p, copy_bool, copy_char ); }
31
32void free_pair_bool_char( void* p ) { free_pair( p, free, free ); }
33
34int cmp_bool( const void* a, const void* b ) {
35        return *(const _Bool*)a == *(const _Bool*)b ? 0 : *(const _Bool*)a < *(const _Bool*)b ? -1 : 1;
36}
37
38int cmp_char( const void* a, const void* b ) {
39        return *(const char*)a == *(const char*)b ? 0 : *(const char*)a < *(const char*)b ? -1 : 1;
40}
41
42int main(int argc, char** argv) {
43        srand(20171025);
44
45        struct stack s = new_stack();
46        REPEAT_TIMED( "push_int",
47                push_stack(&s, new_int( rand() ));
48        )
49
50        struct stack t;
51        TIMED( "copy_int",
52                copy_stack(&t, &s, copy_int);
53        )
54
55        TIMED( "clear_int",
56                clear_stack(&s, free);
57        )
58
59        int max = 0;
60        REPEAT_TIMED( "pop_int", 
61                int* x = pop_stack(&t);
62                if ( *x > max ) { max = *x; }
63                free(x);
64        )
65
66        struct stack s2 = new_stack();
67        REPEAT_TIMED( "push_bool_char", 
68                push_stack(&s2, new_pair( new_bool( rand() & 0x1 ), new_char( rand() & 0x7F ) ));
69        )
70
71        struct stack t2;
72        TIMED( "copy_bool_char", 
73                copy_stack(&t2, &s2, copy_pair_bool_char);
74        )
75
76        TIMED( "clear_bool_char", 
77                clear_stack(&s2, free_pair_bool_char);
78        )
79
80        struct pair* max2 = new_pair( new_bool(0), new_char('\0') );
81        REPEAT_TIMED( "pop_bool_char", 
82                struct pair* x = pop_stack(&t2);
83                if ( cmp_pair( x, max2, cmp_bool, cmp_char ) > 0 ) {
84                        free_pair_bool_char( max2 );
85                        max2 = x;
86                } else {
87                        free_pair_bool_char( x );
88                }
89        )
90        free_pair_bool_char( max2 );
91}
Note: See TracBrowser for help on using the repository browser.