Created
June 3, 2017 13:14
-
-
Save Veedrac/a3f0d6a0b1a2a64a28ac6da4637ae59d to your computer and use it in GitHub Desktop.
Normalized versions of https://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=java&id=3 and https://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=gpp&id=2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <iostream> | |
#include <memory> | |
class Tree { | |
std::unique_ptr<Tree> left; | |
std::unique_ptr<Tree> right; | |
public: | |
Tree(int depth) { | |
if (depth == 0) { | |
return; | |
} | |
left = std::make_unique<Tree>(depth - 1); | |
right = std::make_unique<Tree>(depth - 1); | |
} | |
int count() const { | |
return !left ? 1 : left->count() + 1 + right->count(); | |
} | |
}; | |
int main(int argc, char *argv[]) { | |
int min_depth = 4; | |
int max_depth = argc == 2 ? atoi(argv[1]) : 10; | |
max_depth = std::max(max_depth, min_depth + 2); | |
int stretch_depth = max_depth + 1; | |
{ | |
Tree tree{stretch_depth}; | |
std::cout << stretch_depth << " " << tree.count() << std::endl; | |
} | |
Tree long_lived{max_depth}; | |
for (int depth = min_depth; depth <= max_depth; depth += 2) { | |
int iterations = 16 << (max_depth - depth); | |
int count = 0; | |
for (int i = 0; i < iterations; ++i) { | |
count += Tree{depth}.count(); | |
} | |
std::cout << iterations << " " << depth << " " << count << std::endl; | |
} | |
std::cout << max_depth << " " << long_lived.count() << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tree { | |
private Tree left; | |
private Tree right; | |
Tree(int depth) { | |
if (depth == 0) { | |
return; | |
} | |
left = new Tree(depth - 1); | |
right = new Tree(depth - 1); | |
} | |
public int count() { | |
return left == null ? 1 : left.count() + 1 + right.count(); | |
} | |
} | |
public class binarytrees { | |
public static void main(String[] args) { | |
int minDepth = 4; | |
int maxDepth = args.length == 1 ? Integer.parseInt(args[0]) : 10; | |
maxDepth = Math.max(maxDepth, minDepth + 2); | |
int stretchDepth = maxDepth + 1; | |
{ | |
Tree tree = new Tree(stretchDepth); | |
System.out.println(stretchDepth + " " + tree.count()); | |
} | |
Tree longLived = new Tree(maxDepth); | |
for (int depth = minDepth; depth <= maxDepth; depth += 2) { | |
int iterations = 16 << (maxDepth - depth); | |
int count = 0; | |
for (int i = 0; i < iterations; i++) { | |
count += new Tree(depth).count(); | |
}; | |
System.out.println(iterations + " " + depth + " " + count); | |
}; | |
System.out.println(maxDepth + " " + longLived.count()); | |
} | |
} |
Author
Veedrac
commented
Jun 3, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment