자습

실험실)typeid(T).name() vs typeid(T).hash_code()

hyrule 2022. 11. 5. 18:25

name()이 걱정되는 점 : T에서 타입명을 추론하고 이를 문자열로 변환하는 과정이 오래걸릴 거 같음

 

hash_code()가 걱정되는 점: T를 해쉬코드로 변경하는 알고리즘이 복잡할 것 같음

 

 

#include <windows.h>
#include <iostream>

#include "Timer.h"


int main()
{
	CTimer Timer;
	float T = 0.f;

	Timer.StartTimer();

	for (int a = 0; a < 100000; ++a)
	{
		std::string mystr = typeid(int).name();
		mystr = typeid(float).name();
		mystr = typeid(CTimer).name();	
		mystr = typeid(char).name();
		mystr = typeid(DWORD).name();
	}

	T = Timer.EndTimer();


	Timer.StartTimer();

	for (int a = 0; a < 100000; ++a)
	{
		size_t mystr = typeid(int).hash_code();
		mystr = typeid(float).hash_code();
		mystr = typeid(CTimer).hash_code();
		mystr = typeid(char).hash_code();
		mystr = typeid(DWORD).hash_code();
	}

	T = Timer.EndTimer();
	
	return 0; 

}

 

 

결과: 

-> 전자가 name(), 후자가 hash_code()

작업순서를 바꿔도 결과는 동일

 

후자가 월등히 빠르다

엄청 크게 차이나는걸 보면 전자도 문자열 이름을 unordered_map 같은거에 hash_code와 pair해놔서 그런걸지도 모르겠다.