103 Star 575 Fork 242

cpp-master/cpp-tbox

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
海卫哥 提交于 2023-07-25 00:13 +08:00 . tidy: 添加文件头
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
/**
* 本示例对 ObjectPool 与 new,delete 的时间耗时进行了统计,然后打印出两者平均耗时
*/
#include <tbox/base/object_pool.hpp>
#include <chrono>
#include <iostream>
struct MyStruct {
public:
MyStruct(int i) : i_(i) { }
int i_;
char array[100];
};
struct MyBlock {
char array[1500];
};
int main() {
tbox::ObjectPool<int> op1;
tbox::ObjectPool<MyStruct> op2;
tbox::ObjectPool<MyBlock> op3;
std::chrono::nanoseconds acc_new = std::chrono::nanoseconds::zero();
std::chrono::nanoseconds acc_op = std::chrono::nanoseconds::zero();
std::chrono::steady_clock::time_point tp;
constexpr int count = 100000;
for (int i = 0; i < count; ++i) {
tp = std::chrono::steady_clock::now();
auto p01 = new MyStruct(i);
auto p02 = new int(i);
auto p03 = new MyBlock;
acc_new += std::chrono::steady_clock::now() - tp;
tp = std::chrono::steady_clock::now();
auto p11 = op1.alloc(i);
auto p12 = op2.alloc(i);
auto p13 = op3.alloc();
acc_op += std::chrono::steady_clock::now() - tp;
tp = std::chrono::steady_clock::now();
delete p01;
delete p02;
delete p03;
acc_new += std::chrono::steady_clock::now() - tp;
tp = std::chrono::steady_clock::now();
op1.free(p11);
op2.free(p12);
op3.free(p13);
acc_op += std::chrono::steady_clock::now() - tp;
}
std::cout << "new_acc: " << acc_new.count() / count / 3 << " ns" << std::endl;
std::cout << "op_acc : " << acc_op.count() / count / 3 << " ns" << std::endl;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cpp-master/cpp-tbox.git
git@gitee.com:cpp-master/cpp-tbox.git
cpp-master
cpp-tbox
cpp-tbox
develop

搜索帮助