MSVC8中的SafeCode對性能的影響
由于前面一篇關于編譯boost1.34.0編譯的blog
(
http://www.shnenglu.com/chemz/archive/2007/06/06/25666.html
)
中有人提到在Visual C++ 2005中由于微軟采用了safe code技術會對性能產生非常大的影
響(下降50%),所以本著性能不可以通過估計和猜測來確定,所以編寫了一個針對于此的
測試程序進行了測試。
該測試程序為了做到足夠的覆蓋C++標準庫中STL部分,所以選擇了容器/跌代器/算法三
個關鍵組成中的具有代表性的類和對象進行了測試。首先解釋一下微軟所提供的safe code
的目的和作用,由于STL庫中為了考慮到性能的最大化所以對于像:邊界值檢查、合法性檢
查以及范圍檢查等都省略了,此部分需要依靠程序員自行處理,而safe code就是在STL設計
到此部分的代碼中添加了判斷用的語句(通過宏引入的)。究竟會不會影響到性能呢?有多
大的影響呢?
測試代碼如下:
#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#define MAX_TEST_COUNT 1000 * 1000 * 2
#define TIME_ELAPSE() ( std::clock() - start * 1.0 ) / CLOCKS_PER_SEC
int main( int argc, char *argv[] )
{
double times[6];
size_t idx = 0;
/*
* vector
*/
std::vector<size_t> vec1;
std::clock_t start = std::clock();
{
for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
{
vec1.push_back( i );
}
}
times[idx++] = TIME_ELAPSE();
std::vector<std::string> vec2;
start = std::clock();
{
for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
{
vec2.push_back( "std::vector<std::string>" );
}
}
times[idx++] = TIME_ELAPSE();
/*
* list
*/
std::list<size_t> lst1;
start = std::clock();
{
for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
{
lst1.push_back( i );
}
}
times[idx++] = TIME_ELAPSE();
std::list<std::string> lst2;
start = std::clock();
{
for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
{
lst2.push_back( "std::vector<std::string>" );
}
}
times[idx++] = TIME_ELAPSE();
/*
* map
*/
std::map<size_t, size_t> map1;
start = std::clock();
{
for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
{
map1.insert( std::make_pair( i, i ) );
}
}
times[idx++] = TIME_ELAPSE();
std::map<size_t, std::string> map2;
start = std::clock();
{
for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
{
map2.insert( std::make_pair( i, "std::vector<std::string>" ) );
}
}
times[idx++] = TIME_ELAPSE();
std::cout << "std::vector<size_t>::push_back " << times[0] << " s" << std::endl;
std::cout << "std::vector<std::string>::push_back " << times[1] << " s" << std::endl;
std::cout << "std::list<size_t>::push_back " << times[2] << " s" << std::endl;
std::cout << "std::list<std::string>::push_back " << times[3] << " s" << std::endl;
std::cout << "std::map<size_t, size_t>::insert " << times[4] << " s" << std::endl;
std::cout << "std::map<size_t, std::string>::insert " << times[5] << " s" << std::endl;
std::cout << std::endl;
/*
* iterator
*/
idx = 0;
start = std::clock();
{
for ( std::vector<size_t>::iterator it = vec1.begin(); it != vec1.end(); ++it )
{
(*it) = (*it) + 1;
}
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
for ( std::vector<std::string>::iterator it = vec2.begin(); it != vec2.end(); ++it )
{
(*it) = "std::vector<std::string>::iterator";
}
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
for ( std::list<size_t>::iterator it = lst1.begin(); it != lst1.end(); ++it )
{
(*it) = (*it) + 1;
}
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
for ( std::list<std::string>::iterator it = lst2.begin(); it != lst2.end(); ++it )
{
(*it) = "std::list<std::string>::iterator";
}
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
for ( std::map<size_t, size_t>::iterator it = map1.begin(); it != map1.end(); ++it )
{
(*it).second = (*it).second + 1;
}
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
for ( std::map<size_t, std::string>::iterator it = map2.begin(); it != map2.end(); ++it )
{
(*it).second = "std::map<size_t, std::string>::iterator";
}
}
times[idx++] = TIME_ELAPSE();
std::cout << "std::vector<size_t>::iterator " << times[0] << " s" << std::endl;
std::cout << "std::vector<std::string>::iterator " << times[1] << " s" << std::endl;
std::cout << "std::list<size_t>::iterator " << times[2] << " s" << std::endl;
std::cout << "std::list<std::string>::iterator " << times[3] << " s" << std::endl;
std::cout << "std::map<size_t, size_t>::iterator " << times[4] << " s" << std::endl;
std::cout << "std::map<size_t, std::string>::iterator " << times[5] << " s" << std::endl;
std::cout << std::endl;
/*
* for_each
*/
using namespace boost::lambda;
idx = 0;
start = std::clock();
{
std::for_each( vec1.begin(), vec1.end(), _1 = _1 + 1 );
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
std::for_each( vec2.begin(), vec2.end(), _1 = "std::for_each" );
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
std::for_each( lst1.begin(), lst1.end(), _1 = _1 + 1 );
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
std::for_each( lst2.begin(), lst2.end(), _1 = "std::for_each" );
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
std::for_each( map1.begin(), map1.end(), bind( &std::map<size_t, size_t>::value_type::second, _1 ) = bind( &std::map<size_t, size_t>::value_type::second, _1 ) + 1 );
}
times[idx++] = TIME_ELAPSE();
start = std::clock();
{
std::for_each( map2.begin(), map2.end(), bind( &std::map<size_t, std::string>::value_type::second, _1 ) = "std::for_each" );
}
times[idx++] = TIME_ELAPSE();
std::cout << "std::vector<size_t>::for_each " << times[0] << " s" << std::endl;
std::cout << "std::vector<std::string>::for_each " << times[1] << " s" << std::endl;
std::cout << "std::list<size_t>::for_each " << times[2] << " s" << std::endl;
std::cout << "std::list<std::string>::for_each " << times[3] << " s" << std::endl;
std::cout << "std::map<size_t, size_t>::for_each " << times[4] << " s" << std::endl;
std::cout << "std::map<size_t, std::string>::for_each " << times[5] << " s" << std::endl;
return 0;
}
我的測試環境如下:
軟件環境:Visual Studio2005 Pro + SP1, boost1.34.0
硬件環境:PentiumD 3.0GHz, 4G RAM
測試數據:
1. 默認開啟safe code
std::vector<size_t>::push_back 0.031 s
std::vector<std::string>::push_back 2.609 s
std::list<size_t>::push_back 0.797 s
std::list<std::string>::push_back 2.813 s
std::map<size_t, size_t>::insert 1.906 s
std::map<size_t, std::string>::insert 4.047 s
std::vector<size_t>::iterator 0 s
std::vector<std::string>::iterator 1.718 s
std::list<size_t>::iterator 0.016 s
std::list<std::string>::iterator 1.734 s
std::map<size_t, size_t>::iterator 0.063 s
std::map<size_t, std::string>::iterator 1.891 s
std::vector<size_t>::for_each 0.015 s
std::vector<std::string>::for_each 0.156 s
std::list<size_t>::for_each 0.016 s
std::list<std::string>::for_each 0.156 s
std::map<size_t, size_t>::for_each 0.078 s
std::map<size_t, std::string>::for_each 0.313 s
2. 關閉safe code(通過定義如下宏:_CRT_SECURE_NO_DEPRECATE,
_SCL_SECURE_NO_DEPRECATE,
_SECURE_SCL=0)
std::vector<size_t>::push_back 0.031 s
std::vector<std::string>::push_back 2.594 s
std::list<size_t>::push_back 0.796 s
std::list<std::string>::push_back 2.813 s
std::map<size_t, size_t>::insert 1.875 s
std::map<size_t, std::string>::insert 4.047 s
std::vector<size_t>::iterator 0 s
std::vector<std::string>::iterator 1.703 s
std::list<size_t>::iterator 0.031 s
std::list<std::string>::iterator 1.719 s
std::map<size_t, size_t>::iterator 0.062 s
std::map<size_t, std::string>::iterator 1.891 s
std::vector<size_t>::for_each 0.016 s
std::vector<std::string>::for_each 0.14 s
std::list<size_t>::for_each 0.016 s
std::list<std::string>::for_each 0.172 s
std::map<size_t, size_t>::for_each 0.062 s
std::map<size_t, std::string>::for_each 0.313 s
由上面的測試數據可以的出如下的結論:
1. safe code對代碼的性能確實是有影響的,畢竟加入了判斷的代碼;
2. safe code對代碼的性能影響非常的小(最大沒有超過5%),基本上為了安全型可以
忽略其影響,如果確實比較注重性能,可以考慮在release版本下關閉safe code檢查,
在debug版下還是打開以發現錯誤比較好。
注:宏解釋
_CRT_SECURE_NO_DEPRECATE和_SCL_SECURE_NO_DEPRECATE用于關閉safe code代碼警告;
_SECURE_SCL用于控制是否采用safe code對STL邊界進行檢查。