二叉树基准测试结果

时间:2018-09-19 18:42:10

标签: c++ go benchmarking

我偶然发现了a website making benchmakrs。 在这种情况下,Golang与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;
}

Golang:

  

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)
    }
}

1 个答案:

答案 0 :(得分:0)

这是您在计算机上运行的原因,因为在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

GO

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