C++ 虚拟函数与CRTP
我正在尝试使用VS2010在虚拟函数和CRTP上运行一个简单的测量测试,这是一个简单的控制台程序,在这里,我向文本文件写入10000次,每当我在IDE中运行CTRL+F5start而不进行调试时,我都会在发布版本中观察到这一点,我看到虚拟函数调用大大提高了性能,但与此同时,当我作为管理员通过windows资源管理器运行程序时,我看到CRTP大大提高了性能,我真的很好奇为什么会发生这种情况。我的代码如下:-C++ 虚拟函数与CRTP,c++,performance,crtp,C++,Performance,Crtp,我正在尝试使用VS2010在虚拟函数和CRTP上运行一个简单的测量测试,这是一个简单的控制台程序,在这里,我向文本文件写入10000次,每当我在IDE中运行CTRL+F5start而不进行调试时,我都会在发布版本中观察到这一点,我看到虚拟函数调用大大提高了性能,但与此同时,当我作为管理员通过windows资源管理器运行程序时,我看到CRTP大大提高了性能,我真的很好奇为什么会发生这种情况。我的代码如下:- class CFileProfile { public: CFileProfile()
class CFileProfile
{
public:
CFileProfile()
{
if(!m_file.Open(_T("C:\\File_logger_opt.txt") , CFile::modeCreate|CFile::modeWrite))
{
printf("\n Error In Opening File \n");
}
}
~CFileProfile()
{
m_file.Close();
}
static CFileProfile* GetDefault()
{
if(m_pMe == NULL)
{
m_pMe = new CFileProfile;
}
return m_pMe;
}
static void ReleaseDefault()
{
if(m_pMe)
{
delete m_pMe;m_pMe = NULL;
}
}
CStdioFile* GetFile()
{
return &m_file;
}
static CFileProfile* m_pMe;
CStdioFile m_file;
};
template<typename CRTP_ARGS>
class CBase_CRTP
{
public:
void Execute()
{
CRTP_ARGS::ExecuteA();
}
};
class CDerived_CRTP : public CBase_CRTP<CDerived_CRTP>
{
public:
static void ExecuteA()
{
CString sLine;
sLine.Format(_T("\n Executing CDerived_CRTP \n"));
CFileProfile::GetDefault()->GetFile()->WriteString(sLine);
}
};
class CBase_WCRTP
{
public:
virtual void Execute()
{
}
};
class CDerived_WCRTP :public CBase_WCRTP
{
public:
virtual void Execute()
{
CString sLine;
sLine.Format(_T("\n Executing CDerived_WCRTP \n"));
CFileProfile::GetDefault()->GetFile()->WriteString(sLine);
}
};
Inside Main:-
CFileProfile::GetDefault();
SYSTEMTIME sys_time;
::GetSystemTime(&sys_time);
CString sLine;
sLine.Format(_T("\n The Virtual Start :-[%d:%d:%d:%d] \n") , sys_time.wHour , sys_time.wMinute , sys_time.wSecond,sys_time.wMilliseconds);
CFileProfile::GetDefault()->GetFile()->WriteString(sLine);
CDerived_WCRTP wcrtp;
for(int i = 0; i < 10000; i++)
{
wcrtp.Execute();
}
::GetSystemTime(&sys_time);
sLine.Format(_T("\n The Virtual End :-[%d:%d:%d:%d] \n") , sys_time.wHour , sys_time.wMinute , sys_time.wSecond,sys_time.wMilliseconds);
CFileProfile::GetDefault()->GetFile()->WriteString(sLine);
::GetSystemTime(&sys_time);
sLine.Format(_T("\n The CRTP Start :-[%d:%d:%d:%d] \n") , sys_time.wHour , sys_time.wMinute , sys_time.wSecond,sys_time.wMilliseconds);
CFileProfile::GetDefault()->GetFile()->WriteString(sLine);
CDerived_CRTP crtp;
for(int i = 0; i < 10000; i++)
{
crtp.Execute();
}
::GetSystemTime(&sys_time);
sLine.Format(_T("\n The CRTP End :-[%d:%d:%d:%d] \n") , sys_time.wHour , sys_time.wMinute , sys_time.wSecond,sys_time.wMilliseconds);
CFileProfile::GetDefault()->GetFile()->WriteString(sLine);
CFileProfile::ReleaseDefault();
如果你不标记一种语言,没有人会看它。在project>debugging>environment中,尝试一下\u NO\u DEBUG\u HEAP=1,看看它是否有任何效果。当然,瓶颈是写入文件,而不是函数调用开销?谢谢你的评论Neil,我会记住下次包括语言标记..,你的建议有效,当我在没有调试的情况下使用ctrl+F5/start运行它时,我使用CRTP获得了30%的提升,但是如果我以管理员的身份从windows资源管理器中运行它,我会获得完全相同的性能。如果我从windows资源管理器中执行它,CRTP和虚拟函数似乎都是相同的。以前,CRTP的速度快了50%,我仍然有点好奇