二叉树基准测试结果

问题描述 投票:0回答:1

我偶然发现了a website making benchmakrs。在这种情况下Golang vs C ++,二叉树。

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

在这个例子和我的机器上,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:

去版本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 benchmarking
1个回答
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

 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 basic / default设置构建了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})
© www.soinside.com 2019 - 2024. All rights reserved.