MSVC8中的SafeCode对性能的影响
由于前面一篇关于编译boost1.34.0编译的blog
(
http://www.cppblog.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边界进行检查。