The Q6600
Benchmarks Game

binary-trees C++ g++ program

source code

/* The Computer Language Benchmarks Game
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
*
* Contributed by Jon Harrop
* Modified by Alex Mizrahi
* Modified by Andreas Schäfer
* Modified by aardsplat-guest
*  *reset*
*/

#include <iostream>
#include <algorithm>
#include <future>
#include <vector>

#include <boost/pool/object_pool.hpp>

constexpr int threads{16};

struct Node {
   Node *l,*r;
   Node() : l(0),r(0) {}
   Node(Node *l,Node *r) : l(l),r(r) {}
   int check() const
   {
      if (l)
         return l->check() + 1 + r->check();
      else return 1;
   }
};

using Node_pool = boost::object_pool<Node>;

Node *make(int d,Node_pool& pool)
{
   if (d==0)
      return pool.construct();
   return pool.construct(make(d-1,pool),make(d-1,pool));
}

int make_iteration(int from,int to,int d,bool thread)
{
   int c{0};
   if (thread) {
      std::vector<std::future<int>>futures{};
      for (int j=0; j<threads; ++j) {
         int span{(to-from+1)/threads};
         futures.emplace_back(std::async(std::launch::async,
            make_iteration,from+span*j,span+span*j,d,false));
      }
      for (auto& fti : futures) {
         c += fti.get();
      }
   }
   else {
      Node_pool pool;
      for (int i=from; i<=to; ++i) {
         Node *a = make(d,pool);
         c += a->check();
      }
   }
   return c;
}

int main(int argc,char *argv[])
{
   int min_depth = 4,
      max_depth = std::max(min_depth+2,
         (argc == 2 ? atoi(argv[1]) : 10)),
      stretch_depth = max_depth+1;

   {
      Node_pool pool;
      Node *c = make(stretch_depth,pool);
      std::cout << "stretch tree of depth " << stretch_depth << "\t "
         << "check: " << c->check() << std::endl;
   }

   Node_pool long_lived_pool;
   Node *long_lived_tree=make(max_depth,long_lived_pool);

   for (int d=min_depth; d<=max_depth; d+=2) {
      int iterations = 1 << (max_depth - d + min_depth);
      int   c=0;
      c = make_iteration(1,iterations,d,true);
      std::cout << iterations << "\t trees of depth " << d << "\t "
         << "check: " << c << std::endl;
   }

   std::cout << "long lived tree of depth " << max_depth << "\t "
      << "check: " << (long_lived_tree->check()) << "\n";

   return 0;
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0


Mon, 04 May 2020 17:29:41 GMT

MAKE:
/usr/bin/g++ -c -pipe -O3 -fomit-frame-pointer -march=core2   binarytrees.c++ -o binarytrees.c++.o &&  \
        /usr/bin/g++ binarytrees.c++.o -o binarytrees.gpp_run -lpthread 
rm binarytrees.c++

7.98s to complete and log all make actions

COMMAND LINE:
./binarytrees.gpp_run 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303