二叉树基准测试结果
Binary tree benchmark results
我偶然发现了一个制作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})
相关文章:
- 使用rdtsc进行基准测试的缺点是什么
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 更高效地在微控制器上对C++进行基准测试
- _mm256_load_ps调试模式下导致谷歌/基准测试的分段错误
- 二叉树基准测试结果
- 如何使用谷歌基准测试对自定义界面进行基准测试
- 谷歌基准测试结果中显示的时间没有意义
- 测试结果乘加减除法可能为一组数字
- 如何将单位测试标记为在Netbeans测试结果窗口中跳过的?(C )
- 对同一变量的测试结果似乎不同
- 对多线程输入的QTableWidget进行基准测试,结果很奇怪
- 奇怪的基准测试结果
- if/else优化测试结果令人震惊
- 如何在boost中以编程方式获得测试结果
- 提振.测试-从同一项目的不同构建配置中区分测试结果
- 是否有使用可能/不可能提示的性能测试结果
- 用于测试c++性能的单元测试(合成基准)
- 如何在jenkins中发布测试结果报告
- 使用数组获取测试结果
- Web-CAT CxxTest Eclipse GUI 用于测试结果未显示