source: doc/generic_types/evaluation/c-bench.c @ 2183e12

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

Minor cleanup, also filled in benchmark source appendix

  • 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 ); } /***/
27void* copy_char( const void* p ) { return new_char( *(const char*)p ); } /***/
28void* copy_int( const void* p ) { return new_int( *(const int*)p ); } /***/
29void* copy_pair_bool_char( const void* p ) { return copy_pair( p, copy_bool, copy_char ); } /***/
30void free_pair_bool_char( void* p ) { free_pair( p, free, free ); } /***/
31
32int cmp_bool( const void* a, const void* b ) { /***/
33        return *(const _Bool*)a == *(const _Bool*)b ? 0 : *(const _Bool*)a < *(const _Bool*)b ? -1 : 1; 
34}
35
36int cmp_char( const void* a, const void* b ) {
37        return *(const char*)a == *(const char*)b ? 0 : *(const char*)a < *(const char*)b ? -1 : 1; /***/
38}
39
40int main(int argc, char** argv) {
41        FILE* out = fopen("c-out.txt", "w");
42        int max = 0;
43        struct stack s = new_stack(), t;
44        REPEAT_TIMED( "push_int", push_stack(&s, new_int( _i )); )
45        TIMED( "copy_int",      copy_stack(&t, &s, copy_int); /***/ )
46        TIMED( "clear_int", clear_stack(&s, free); /***/ )
47        REPEAT_TIMED( "pop_int", 
48                int* x = pop_stack(&t); /***/
49                if ( *x > max ) { max = *x; }
50                free(x); )
51        REPEAT_N_TIMED( "print_int", N/2, print( out, "dsds", _i, ":", _i, "\n" ); /***/ )
52
53        struct pair* max2 = new_pair( new_bool(0), new_char('\0') );
54        struct stack s2 = new_stack(), t2;
55
56        REPEAT_TIMED( "push_bool_char", 
57                push_stack(&s2, new_pair( new_bool( _i & 0x1 ), new_char( _i & 0x7F ) )); )
58        TIMED( "copy_bool_char", copy_stack(&t2, &s2, copy_pair_bool_char); /***/ )
59        TIMED( "clear_bool_char", clear_stack(&s2, free_pair_bool_char); /***/ )
60        REPEAT_TIMED( "pop_bool_char", 
61                struct pair* x = pop_stack(&t2); /***/
62                if ( cmp_pair( x, max2, cmp_bool, cmp_char ) > 0 ) { /***/
63                        free_pair_bool_char( max2 ); /***/
64                        max2 = x;
65                } else {
66                        free_pair_bool_char( x ); /***/
67                } )
68        REPEAT_N_TIMED( "print_pair", N/2, 
69                struct pair p1 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) }); /***/
70                struct pair p2 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) }); /***/
71                print( out, "pbcspbcs", p1, ":", p2, "\n" ); /***/
72                free(p1.first); free(p1.second); /***/
73                free(p2.first); free(p2.second); /***/ )
74        free_pair_bool_char( max2 ); /***/
75        fclose(out);
76}
Note: See TracBrowser for help on using the repository browser.