gpt4 book ai didi

c++ - 二叉树基准测试结果

转载 作者:数据小太阳 更新时间:2023-10-29 03:21:58 26 4
gpt4 key购买 nike

我偶然发现了 a website making 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 version 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++ 的速度是运行速度的两倍。

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 基本/默认设置构建了 C++ main.cpp(此 CMakeLists.txt 将构建一个 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})

关于c++ - 二叉树基准测试结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52412334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com