modernc.org/ccgo/v3@v3.16.14/lib/testdata/CompCert-3.6/test/c/binarytrees.c (about) 1 /* The Computer Language Shootout Benchmarks 2 http://shootout.alioth.debian.org/ 3 4 contributed by Kevin Carson 5 compilation: 6 gcc -O3 -fomit-frame-pointer -funroll-loops -static binary-trees.c -lm 7 icc -O3 -ip -unroll -static binary-trees.c -lm 8 */ 9 10 #include <math.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 14 15 typedef struct tn { 16 struct tn* left; 17 struct tn* right; 18 long item; 19 } treeNode; 20 21 22 treeNode* NewTreeNode(treeNode* left, treeNode* right, long item) 23 { 24 treeNode* new; 25 26 new = (treeNode*)malloc(sizeof(treeNode)); 27 28 new->left = left; 29 new->right = right; 30 new->item = item; 31 32 return new; 33 } /* NewTreeNode() */ 34 35 36 long ItemCheck(treeNode* tree) 37 { 38 if (tree->left == NULL) 39 return tree->item; 40 else 41 return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right); 42 } /* ItemCheck() */ 43 44 45 treeNode* BottomUpTree(long item, unsigned depth) 46 { 47 if (depth > 0) 48 return NewTreeNode 49 ( 50 BottomUpTree(2 * item - 1, depth - 1), 51 BottomUpTree(2 * item, depth - 1), 52 item 53 ); 54 else 55 return NewTreeNode(NULL, NULL, item); 56 } /* BottomUpTree() */ 57 58 59 void DeleteTree(treeNode* tree) 60 { 61 if (tree->left != NULL) 62 { 63 DeleteTree(tree->left); 64 DeleteTree(tree->right); 65 } 66 67 free(tree); 68 } /* DeleteTree() */ 69 70 71 int main(int argc, char* argv[]) 72 { 73 unsigned N, depth, minDepth, maxDepth, stretchDepth; 74 treeNode *stretchTree, *longLivedTree, *tempTree; 75 76 N = argc < 2 ? 12 : atol(argv[1]); 77 78 minDepth = 4; 79 80 if ((minDepth + 2) > N) 81 maxDepth = minDepth + 2; 82 else 83 maxDepth = N; 84 85 stretchDepth = maxDepth + 1; 86 87 stretchTree = BottomUpTree(0, stretchDepth); 88 printf 89 ( 90 "stretch tree of depth %u\t check: %li\n", 91 stretchDepth, 92 ItemCheck(stretchTree) 93 ); 94 95 DeleteTree(stretchTree); 96 97 longLivedTree = BottomUpTree(0, maxDepth); 98 99 for (depth = minDepth; depth <= maxDepth; depth += 2) 100 { 101 long i, iterations, check; 102 103 iterations = pow(2, maxDepth - depth + minDepth); 104 105 check = 0; 106 107 for (i = 1; i <= iterations; i++) 108 { 109 tempTree = BottomUpTree(i, depth); 110 check += ItemCheck(tempTree); 111 DeleteTree(tempTree); 112 113 tempTree = BottomUpTree(-i, depth); 114 check += ItemCheck(tempTree); 115 DeleteTree(tempTree); 116 } /* for(i = 1...) */ 117 118 printf 119 ( 120 "%li\t trees of depth %u\t check: %li\n", 121 iterations * 2, 122 depth, 123 check 124 ); 125 } /* for(depth = minDepth...) */ 126 127 printf 128 ( 129 "long lived tree of depth %u\t check: %li\n", 130 maxDepth, 131 ItemCheck(longLivedTree) 132 ); 133 134 return 0; 135 } /* main() */ 136 137 /****** 138 build & benchmark results 139 140 BUILD COMMANDS FOR: binarytrees.gcc 141 142 Thu Sep 14 00:25:13 PDT 2006 143 144 /usr/bin/gcc -pipe -Wall -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 -lm binarytrees.c -o binarytrees.gcc_run 145 146 ================================================================= 147 COMMAND LINE (%A is single numeric argument): 148 149 binarytrees.gcc_run %A 150 N=16 151 152 PROGRAM OUTPUT 153 ============== 154 stretch tree of depth 17 check: -1 155 131072 trees of depth 4 check: -131072 156 32768 trees of depth 6 check: -32768 157 8192 trees of depth 8 check: -8192 158 2048 trees of depth 10 check: -2048 159 512 trees of depth 12 check: -512 160 128 trees of depth 14 check: -128 161 32 trees of depth 16 check: -32 162 long lived tree of depth 16 check: -1 163 skinny tree of depth 17 check: 17 164 *********/