source: doc/generic_types/evaluation/c-bench.c @ 0d10090

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 0d10090 was 0d10090, checked in by Aaron Moss <a3moss@…>, 7 years ago

Add printing code to benchmark

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