Changeset 87c5f40 for doc/generic_types/evaluation/c-bench.c
- Timestamp:
- Apr 11, 2017, 5:11:55 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 4f57930
- Parents:
- e6dceef
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/generic_types/evaluation/c-bench.c
re6dceef r87c5f40 1 1 #include <stdlib.h> 2 2 #include "bench.h" 3 #include "c-pair.h" 3 4 #include "c-stack.h" 4 5 5 void* copy_int( void* p ) { 6 _Bool* new_bool( _Bool b ) { 7 _Bool* q = malloc(sizeof(_Bool)); 8 *q = b; 9 return q; 10 } 11 12 char* new_char( char c ) { 13 char* q = malloc(sizeof(char)); 14 *q = c; 15 return q; 16 } 17 18 int* new_int( int i ) { 6 19 int* q = malloc(sizeof(int)); 7 *q = *(int*)p;20 *q = i; 8 21 return q; 22 } 23 24 void* copy_bool( const void* p ) { return new_bool( *(const _Bool*)p ); } 25 26 void* copy_char( const void* p ) { return new_char( *(const char*)p ); } 27 28 void* copy_int( const void* p ) { return new_int( *(const int*)p ); } 29 30 void* copy_pair_bool_char( const void* p ) { return copy_pair( p, copy_bool, copy_char ); } 31 32 void free_pair_bool_char( void* p ) { free_pair( p, free, free ); } 33 34 int 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 38 int 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; 9 40 } 10 41 … … 14 45 struct stack s = new_stack(); 15 46 REPEAT_TIMED( "push_int", 16 int* x = malloc(sizeof(int)); 17 *x = rand(); 18 push_stack(&s, x); 47 push_stack(&s, new_int( rand() )); 19 48 ) 20 49 … … 25 54 26 55 TIMED( "clear_int", 27 clear_stack(&s );56 clear_stack(&s, free); 28 57 ) 29 58 30 int sum;59 int max = 0; 31 60 REPEAT_TIMED( "pop_int", 32 61 int* x = pop_stack(&t); 33 sum += *x;62 if ( *x > max ) { max = *x; } 34 63 free(x); 35 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 ); 36 91 }
Note: See TracChangeset
for help on using the changeset viewer.