先开门见山,直接给结论:switch语句比显示类型转换要快,至于快多少,在500000(50W)次下200us
以下代码运行平台:windows7,x86(32位),2G内存,双核,CPU频率:3GHz,IDE:VS2010
有个疑问是:我的电脑是3GHz,为什么用QueryPerformanceFrequency,得出的是:2922451,应该是2922451000才对吧?
大家有什么意见?
#include <iostream>
#include <windows.h>
using namespace std;
class HARQ
{
public:
EHARQFeedback m_eFeedback;
};
class Entity
{
public:
HARQ m_HARQPro[1];
unsigned char m_byFeebackProNum;
LARGE_INTEGER listart;
LARGE_INTEGER liend;
LONGLONG freq;
LONGLONG start;
LONGLONG end;
double elapse;
Entity();
bool testSwitchSpeed(unsigned char *t_pbyMsg);
bool testExplicitTypeConversion(unsigned char *t_pbyMsg);
};
void test_eNBULHARQ();
int main()
{
unsigned char abyMsg[2];
abyMsg[1] = 0;
Entity en;
en.testSwitchSpeed(abyMsg);
en.testExplicitTypeConversion(abyMsg);
return 0;
/*
* frq = 2922451
* 10000
* 1st
* 0.0444832 ms testSwitchSpeed
* 0.0472206 ms testExplicitTypeConversion
* 2nd
* 0.044141 ms testSwitchSpeed
* 0.0472206 ms testExplicitTypeConversion
* 3rd
* 0.0557751 ms testSwitchSpeed
* 0.0585125 ms testExplicitTypeConversion
* ******************************************
* 50000
* 1st
* 0.282297 ms testSwitchSpeed
* 0.300091 ms testExplicitTypeConversion
* 2nd
* 0.261424 ms testSwitchSpeed
* 0.311382 ms testExplicitTypeConversion
* 3rd
* 0.266899 ms testSwitchSpeed
* 0.285377 ms testExplicitTypeConversion
* ******************************************
* 500000
* 1st
* 2.77165 ms testSwitchSpeed
* 2.93007 ms testExplicitTypeConversion
* 2nd
* 2.70732 ms testSwitchSpeed
* 2.91262 ms testExplicitTypeConversion
* 3rd
* 2.67857 ms testSwitchSpeed
* 2.86609 ms testExplicitTypeConversion
*/
/*******************************************
* conclusion: switch语句比显示类型转换要快 *
*******************************************/
}
Entity::Entity()
{
m_byFeebackProNum = 0;
listart;
QueryPerformanceFrequency(&listart);
freq = listart.QuadPart;
start = 0;
end = 0;
elapse = 0.0;
}
bool Entity::testSwitchSpeed(unsigned char *t_pbyMsg)
{
QueryPerformanceCounter(&listart);
for (int i = 0; i != 500000; ++i)
{
switch (t_pbyMsg[1])
{
case 0:
m_HARQPro[m_byFeebackProNum].m_eFeedback = ACK;
break;
case 1:
m_HARQPro[m_byFeebackProNum].m_eFeedback = NACK;
break;
default:
cout << "error In Ue ULEntityProcess: m_eFeedback != ACK And NACK" << endl;
return false;
break;
}
}
QueryPerformanceCounter(&liend);
start = listart.QuadPart;
end = liend.QuadPart;
elapse = (end - start) * 1000.0 / freq; // s
cout << elapse << " ms testSwitchSpeed" << endl;
return true;
}
bool Entity::testExplicitTypeConversion(unsigned char *t_pbyMsg)
{
// start = 0;
// end = 0;
QueryPerformanceCounter(&listart);
for (int i = 0; i != 500000; ++i)
{
if (t_pbyMsg[1] != 0 && t_pbyMsg[1] != 1)
{
cout << "error In Ue ULEntityProcess: m_eFeedback != ACK And NACK" << endl;
return false;
}
m_HARQPro[m_byFeebackProNum].m_eFeedback = (EHARQFeedback)(t_pbyMsg[1]);
}
QueryPerformanceCounter(&liend);
start = listart.QuadPart;
end = liend.QuadPart;
elapse = (end - start) * 1000.0 / freq; // s
cout << elapse << " ms testExplicitTypeConversion" << endl;
return true;
}