二叉树基准测试结果

Binary tree benchmark results

本文关键字:测试结果 基准 二叉树      更新时间:2023-10-16

我偶然发现了一个制作benchmakrs的网站。 在这种情况下,Golang vs C++,二叉树。

C++解决方案比使用内存池分配的 golang 要好得多。 我可以支持这一点,但想知道没有它的实现会如何。所以我修改了它,使其看起来更像Golang代码,并删除了两者的并发性。

在这个例子和我的机器上,golang 代码在大约 24 秒内运行。 C++代码平均需要 126 秒。我完全没想到这个结果。我预计C++仍然会更快,或者可能更慢一点,但不是 5 倍。

我犯了什么大错误吗?或者你知道其中的原因吗?这两个程序的代码如下:

内置:

mingw32-g++.exe -Wall -fexceptions -O2 -c D:\TMP\Test\main.cpp -o obj\Release\main.o mingw32-g++.exe -o bin\Release\Test.exe obj\Release\main.o -s

#include <iostream>
using namespace std;
class Node {
public:
Node(uint64_t d);
~Node();
int Check();
private:
Node* l;
Node* r;
};
Node::Node(uint64_t d){
if (d > 0){
l = new Node(d - 1);
r = new Node(d - 1);
} else {
l = 0;
r = 0;
}
}
Node::~Node(){
if(l){
delete l;
delete r;
}
}
int Node::Check(){
if (l) {
return l->Check() + 1 + r->Check();
} else {
return 1;
}
}
int main()
{
uint64_t min_depth = 4;
uint64_t max_depth = 21;
for (uint64_t d = min_depth; d <= max_depth; d += 2) {
uint64_t iterations = 1 << (max_depth - d + min_depth);
uint64_t c = 0;
for (uint64_t i = 1; i < iterations; i++) {
Node* a = new Node(d);
c += a->Check();
delete a; // I tried commenting this line but it made no big impact
}
cout << iterations << " trees of depth " << d << " check: " << c << "n";
}
return 0;
}

戈朗:

Go 版本 go1.7.1 Windows/amd64

package main
import(
"fmt"
)
type Node struct {
l *Node
r *Node
}
func (n *Node) check() int {
if n.l != nil {
return n.l.check() + 1 + n.r.check()
} else {
return 1
}
}
func make(d uint) *Node {
root := new(Node)
if d > 0 {
root.l = make(d-1)
root.r = make(d-1)
}
return root
}
func main(){
min_depth := uint(4)
max_depth := uint(21)
for d := min_depth; d <= max_depth; d += 2 {
iterations := 1 << (max_depth - d + min_depth)
c := 0
for i := 1; i < iterations; i++ {
a := make(d)
c += a.check()
}
fmt.Println(iterations, " trees of depth ", d, " check: ", c)
}
}

这是你运行的计算机的东西,因为我得到了预期的结果,C++的速度是go的两倍。

C++

time cmake-build-debug/main
2097152 trees of depth 4 check: 65011681
524288 trees of depth 6 check: 66584449
131072 trees of depth 8 check: 66977281
32768 trees of depth 10 check: 67074049
8192 trees of depth 12 check: 67092481
2048 trees of depth 14 check: 67074049
512 trees of depth 16 check: 66977281
128 trees of depth 18 check: 66584449
32 trees of depth 20 check: 65011681
cmake-build-debug/main  21.09s user 0.02s system 99% cpu 21.113 total

jonny@skyhawk  ~/Projects/benchmark  time ./main            ✔  2604  02:34:29 
2097152  trees of depth  4  check:  65011681
524288  trees of depth  6  check:  66584449
131072  trees of depth  8  check:  66977281
32768  trees of depth  10  check:  67074049
8192  trees of depth  12  check:  67092481
2048  trees of depth  14  check:  67074049
512  trees of depth  16  check:  66977281
128  trees of depth  18  check:  66584449
32  trees of depth  20  check:  65011681
./main  48.72s user 0.52s system 197% cpu 24.905 total

我用CLion的mose基本/默认设置(这个CMakeList.txt构建了C++main.cpp它将构建一个main.cpp(

cmake_minimum_required(VERSION 3.3)
project(test_build)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BUILD_1 main)
set(SOURCE_FILES_1 main.cpp)
add_executable(${BUILD_1} ${SOURCE_FILES_1})