diff --git a/Makefile b/Makefile index ddc8340..9893d08 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,15 @@ SHELL := /bin/bash -CFLAGS := -w -O3 -pg -#CFLAGS := -w -g -pg -DDEBUG +#CFLAGS := -w -O3 -pg -DDEBUG -DDEBUG_BEST +CFLAGS := -w -O3 -DDEBUG -DDEBUG_BEST +# specific DEBUG flags: +# timer: -DDEBUG_TIMER +# best control: -DDEBUG_BEST + #CFLAGS := -w -g -pg -DDEBUG TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" export PATH := .:$(PATH) -TARGETS=lceb tree oper stack eval best +TARGETS=lceb tree oper stack eval best timer INCLUDES=lceb.h OBJS=$(TARGETS:=.o) @@ -20,6 +24,8 @@ tree: tree.c $(CC) $(CFLAGS) -DSTANDALONE -o $@ $? oper: oper.c $(CC) $(CFLAGS) -DSTANDALONE -o $@ $? +timer: timer.c + $(CC) $(CFLAGS) -DSTANDALONE -o $@ $? lceb.o tree.o oper.o eval.o stack.o best.o: lceb.h tree oper: lceb.h diff --git a/best.c b/best.c index f96bb72..8a6027f 100644 --- a/best.c +++ b/best.c @@ -13,19 +13,19 @@ static int bestops=MAXINT; static BEST bests[1024*10]; /* TODO: should be dynamic */ static int nbests=0; -static int sigint=0; +int sigint_received=0; #define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a)) void stopall() { printf("SIGINT RECEIVED: aborting eval\n"); - sigint=1; + sigint_received=1; } int stopped() { - return sigint; + return sigint_received; } void set_target(n) @@ -45,17 +45,10 @@ int check_best(res, nops, node, values, ops) int diff, found=0, i=0; diff=DIFF(target, res); -# ifdef DEBUG1 +# ifdef DEBUG_BEST1 printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops); # endif if (diff < bestdiff || (diff == bestdiff && nops < bestops)) { -# ifdef DEBUG - // printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops); - printf("diff=%d nops=%d %d=", diff, nops, res); - print_node(node, TREE_TOP, 0, 4); - putchar('\n'); - //printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops); -# endif //best=res; // clear old bests for (i=0; itype == TREE_LEAF) { + leaves_calc++; node->val=*vals; res=*vals; vals++; *ncalcs=0; //printf("leaf=%d\n", res); } else { + nodes_calc++; op=*ops; node->op=*ops; ops++; @@ -80,7 +85,7 @@ int eval_node(node, depth, pvals, pops, ncalcs) if (!check_best(res, *ncalcs, node, val_zero, ops_zero)) res=-1; } - if (stopped()) { + if (sigint_received) { print_bests(); exit(1); } @@ -91,3 +96,12 @@ int eval_node(node, depth, pvals, pops, ncalcs) # endif return res; } + +int get_totnodes() +{ + return nodes_calc; +} +int get_totleaves() +{ + return leaves_calc; +} diff --git a/lceb.c b/lceb.c index d78d449..c8c1b87 100644 --- a/lceb.c +++ b/lceb.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "lceb.h" int main(ac, av) @@ -20,19 +21,20 @@ int main(ac, av) { unsigned target; STACK inputstack, *stack; - int i, j, k, stacksize, val, res; - //char *ops="+-*/", *opscomb; - //int len_ops=strlen(ops), ncombs, nops; + int i, j, k, stacksize, val; int ncombs, nstacks, ntrees, nops; TREE *tree; int intarray[1024]; int eval; char *comb; + struct timespec end; + setlocale(LC_ALL, ""); /* to use "%'d" in printf */ if (ac < 4 || ac > 2+MAXINPUT) { fprintf(stderr, "usage: %s target n1 n2 [...n%d]\n", *av, MAXINPUT); exit(1); } + start_timer(); target=atoi(av[1]); stacksize=2*(ac-2)-1; nops=ac-2-1; @@ -110,5 +112,8 @@ int main(ac, av) } printf("\n"); print_bests(); + set_timer(&end); + printf("Total time elapsed: %.5f secs, nodes/leaves evaluated:%'d/%'d\n", + get_timer(end), get_totnodes(), get_totleaves()); exit(0); } diff --git a/lceb.h b/lceb.h index d51471f..7421cfe 100644 --- a/lceb.h +++ b/lceb.h @@ -1,3 +1,4 @@ +#include #define MAXINPUT 6 /* max numbers as input */ #define ALLOCSIZE 1024 /* # of elements to alloc when needed */ @@ -55,6 +56,7 @@ typedef struct best { char *oper; NODE *root; int *values; + struct timespec timer; } BEST; /* tree.c */ @@ -103,6 +105,8 @@ extern char *nth_comb(int n); //extern int eval_stack(STACK *stack); /* tree version */ extern int eval_node(NODE *node, int depth, int *pvals, char *pops, int *ncalcs); +extern int get_totnodes(); +extern int get_totleaves(); /* best.c */ extern int stopped(); @@ -110,3 +114,9 @@ extern void set_target (int n); extern int check_best(int res, int nops, NODE *node, int *values, char *ops); extern void print_best(NODE *node, int *values, char *pops, int depth); extern void print_bests(); + +/* timer.c */ +extern int sigint_received; +extern void start_timer(); +extern void set_timer(struct timespec *timer); +extern double get_timer(struct timespec timer); diff --git a/timer.c b/timer.c new file mode 100644 index 0000000..6a931f7 --- /dev/null +++ b/timer.c @@ -0,0 +1,62 @@ +#include +#include + +#define NANOSEC 1000000000.0 +#define MILLISEC 1000 + +static struct timespec start; + +void start_timer() +{ + printf("timer started...\n"); + clock_gettime(CLOCK_MONOTONIC, &start); +# ifdef DEBUG_TIMER + printf("sec=%d nsec=%d\n", start.tv_sec, start.tv_nsec); +# endif +} + +void set_timer(timer) + struct timespec *timer; +{ + clock_gettime(CLOCK_MONOTONIC, timer); +# ifdef DEBUG_TIMER + printf("timer: sec=%d nsec=%d orig: sec=%d nsec=%d\n", + timer->tv_sec, timer->tv_nsec, start.tv_sec, start.tv_nsec); +# endif +} + +double get_timer(timer) + struct timespec timer; +{ + double diff; + diff=(timer.tv_sec - start.tv_sec) + (timer.tv_nsec - start.tv_nsec) / NANOSEC; +# ifdef DEBUG_TIMER + printf("timer sec: %.5f\n", diff); +# endif + return diff; +} + +#ifdef STANDALONE +main(ac, av) + int ac; + char **av; +{ + int delay; + struct timespec end, tsdelay; + + if (ac != 2) { + fprintf(stderr, "usage: %s msecs\n", *av); + exit (1); + } + delay=atoi(*(av+1)); + start_timer(); + tsdelay.tv_sec=delay/MILLISEC; + tsdelay.tv_nsec=delay%MILLISEC; + printf("delay %d=%d:%d\n", delay, tsdelay.tv_sec, tsdelay.tv_nsec); + nanosleep(&tsdelay, NULL); + set_timer(&end); + printf("Time elapsed: %.5f seconds\n", get_timer(end)); + + return 0; +} +#endif