Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <time.h>
- class TestC {
- public:
- TestC*child1;
- TestC*child2;
- TestC*child3;
- TestC(int depth) {
- if (depth>0) {
- depth-=1;
- child1=new TestC(depth);
- child2=new TestC(depth);
- child3=new TestC(depth);
- } else {
- child1=NULL;
- child2=NULL;
- child3=NULL;
- }
- }
- ~TestC() {
- if (child1!=NULL) delete child1;
- if (child2!=NULL) delete child2;
- if (child3!=NULL) delete child3;
- }
- int Count() {
- int count=1;
- if (child1!=NULL) count+=child1->Count();
- if (child2!=NULL) count+=child2->Count();
- if (child3!=NULL) count+=child3->Count();
- return count;
- }
- };
- int main(int argc, char*argv[]) {
- if (argc<3) {
- printf("TestC <depth> <reps>\n");
- return 0;
- }
- int depth=atoi(argv[1]);
- int reps=atoi(argv[2]);
- printf("TestC started depth=%d reps=%d\n",depth,reps);
- float min_time = LONG_MAX;
- float max_time = LONG_MIN;
- float avg_time = 0;
- int count = 0;
- for(int rep=0; rep<reps; rep++) {
- timespec start;
- clock_gettime(CLOCK_REALTIME, &start);
- TestC*t=new TestC(depth);
- count=t->Count();
- delete t;
- timespec stop;
- clock_gettime(CLOCK_REALTIME, &stop);
- float elapse = (stop.tv_sec-start.tv_sec) + (float)(stop.tv_nsec-start.tv_nsec)/1000000000;
- //printf("ELAPSE=%f\n", elapse);
- if (min_time>elapse) min_time=elapse;
- if (max_time<elapse) max_time=elapse;
- avg_time += elapse;
- }
- avg_time /= reps;
- printf("TestC count=%d min=%f max=%f avg=%f\n", count, min_time, max_time, avg_time);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement