dynamic_cast&&基准测试(C++基础)

dynamic_cast

dynamic_cast是专门用于沿继承层次结构进行的强制类型转换,更像是一个函数, 不是编译时进行的类型转换,而是在运行时计算,正因如此,有小性能损失。

在基类和派生类之间相互转换。dynamic_cast常用来做验证,下图中开启运行时检查。

复制代码
class Entity {
public:
	virtual void PrintName9() {}
};
class Player : public Entity{};
class Enemy : public Entity{};
int main() {
	Player* player = new Player();
	Entity* e = player;
	Enemy* e1 = new Enemy();
	//Enemy* e2 = (Enemy*)e;
	//Enemy* e2 = static_cast<Enemy*>(e);
	Player* e2 = dynamic_cast<Player*>(e1);
	Player* e3 = dynamic_cast<Player*>(e);
	//类似Java可以进行类型验证
	if (dynamic_cast<Player*>(e1) == NULL) {
		std::cout << "error";
	}
}

如果类型转换无效,就说明不是你声称的给定类型,那么就会返回null

使用需要rtti是打开状态,在绝大多数状态,打开会增加开销,但关闭可能会带来错误,如果只想优化,编写非常快的代码,需要避免使用。

基准测试

在这里复习了 如何编写计时类:

复制代码
class Timer {
public:
	Timer() {
		m_StartTimepoint = std::chrono::high_resolution_clock::now();
	}
	~Timer() {
		stop();
	}
	void stop() {
		auto endTimepoint = std::chrono::high_resolution_clock::now();
		auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
		auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
		auto duration = end - start;
		double ms = duration * 0.001;
		std::cout << "ns:" << duration<< "(ms:" << ms << ")" << std::endl;
	}
private:
	std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint;

};

比较了三种方法创建指针的效率:

复制代码
	{
		struct Vector2
		{
			float x, y;
		};
		{
			std::array<std::shared_ptr<Vector2>, 1000> sharedPtrs;
			Timer timer;
			for (int i = 0; i < sharedPtrs.size(); i++) {
				sharedPtrs[i] = std::make_shared<Vector2>();
			}
		}
		{
			std::array<std::shared_ptr<Vector2>, 1000> sharedPtrs;
			Timer timer;
			for (int i = 0; i < sharedPtrs.size(); i++) {
				sharedPtrs[i] = std::shared_ptr<Vector2>();
			}
		}
		{
			std::array<std::unique_ptr<Vector2>, 1000> sharedPtrs;
			Timer timer;
			for (int i = 0; i < sharedPtrs.size(); i++) {
				sharedPtrs[i] = std::make_unique<Vector2>();
			}
		}

	}

结果是最后一种最快,第一种中间,第二种最慢,因为第三种unique指针效率最高,而第二种需要构造shared_ptr所以他效率最低。

相关推荐
梁诚斌15 分钟前
使用OpenSSL接口读取pem编码格式文件中的证书
开发语言·c++
瓜子三百克33 分钟前
Objective-C 路由表原理详解
开发语言·ios·objective-c
幽蓝计划1 小时前
HarmonyOS NEXT仓颉开发语言实战案例:外卖App
开发语言·harmonyos
伍哥的传说1 小时前
鸿蒙系统(HarmonyOS)应用开发之实现电子签名效果
开发语言·前端·华为·harmonyos·鸿蒙·鸿蒙系统
小张成长计划..2 小时前
数据结构-栈的实现
开发语言·数据结构
旷世奇才李先生2 小时前
Lua 安装使用教程
开发语言·lua
Zonda要好好学习2 小时前
Python入门Day2
开发语言·python
不良手残2 小时前
IDEA类和方法注释模板设置-保姆教程
java·开发语言
项目題供诗2 小时前
黑马python(二十四)
开发语言·python
就改了3 小时前
JUC小册——公平锁和非公平锁
java·开发语言