github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/bench/binarytree.pl (about)

     1  use threads;
     2  
     3  run( @ARGV );
     4  
     5  sub bottomup_tree {
     6      my $depth = shift;
     7      return 0 unless $depth;
     8      --$depth;
     9      [ bottomup_tree($depth), bottomup_tree($depth) ];
    10  }
    11  
    12  sub check_tree {
    13      return 1 unless ref $_[0];
    14      1 + check_tree($_[0][0]) + check_tree($_[0][1]);
    15  }
    16  
    17  sub stretch_tree {
    18      my $stretch_depth = shift;
    19      my $stretch_tree = bottomup_tree($stretch_depth);
    20      print "stretch tree of depth $stretch_depth\t check: ",
    21      check_tree($stretch_tree), "\n";
    22  }
    23  
    24  sub depth_iteration {
    25      my ( $depth, $max_depth, $min_depth ) = @_;
    26  
    27      my $iterations = 2**($max_depth - $depth + $min_depth);
    28      my $check      = 0;
    29  
    30      foreach ( 1 .. $iterations ) {
    31          $check += check_tree( bottomup_tree( $depth ) );
    32      }
    33  
    34      return ( $depth => [ $iterations, $depth, $check ] );
    35  }
    36  
    37  sub run {
    38      my $max_depth = shift;
    39      my $min_depth = 4;
    40  
    41      $max_depth = $min_depth + 2 if $min_depth + 2 > $max_depth;
    42  
    43      stretch_tree( $max_depth + 1 );
    44  }