测试结果:
Test std::vector<int>(1000):
(+:234) (-:266) (*:281) (/:1687)
(+:219) (-:234) (*:282) (/:1656)
(+:234) (-:235) (*:265) (/:1625)
(+:235) (-:218) (*:297) (/:1594)
Test std::vector<long>(1000):
(+:219) (-:234) (*:313) (/:1625)
(+:218) (-:235) (*:265) (/:1704)
(+:218) (-:219) (*:297) (/:1594)
(+:234) (-:234) (*:282) (/:2109)
Test std::vector<float>(1000):
(+:250) (-:266) (*:265) (/:2328)
(+:266) (-:250) (*:250) (/:2281)
(+:282) (-:250) (*:250) (/:2609)
(+:250) (-:250) (*:250) (/:2781)
Test std::vector<double>(1000):
(+:422) (-:328) (*:344) (/:2422)
(+:344) (-:328) (*:578) (/:2422)
(+:360) (-:453) (*:359) (/:2422)
(+:297) (-:328) (*:313) (/:2312)
结论:
int,long,float,double的 +, -, * 运算性能是一样的,除法运算比较慢,占用时间较多,要尽量避免除法。
即:加,减,乘的运算时间与数据类型无关,所以可以随意使用更精确的float、double型,不影响程序速度。
以下是测试代码(请用Vc7生成一个console类型的空项目,将如下代码保存成文件加入项目,Release模式编译):
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <fstream>
#include <time.h>
#include <boost/bind.hpp> // for boost::bind
#include <boost/lambda/lambda.hpp> // for boost::lambda
#include <typeinfo.h>
template<typename T,typename TO>
void test(T &vec,TO &out)
{
out<< "Test std::vector<" << typeid(T::value_type).name() << ">(" << vec.size() << "):\t" ;
std::vector<T::value_type> myVecTest(4),myDst(vec.size());
std::generate( myVecTest.begin(),myVecTest.end(),boost::bind(std::divides<T::value_type>(),boost::bind(&rand),(T::value_type)9876) );
std::transform( myVecTest.begin(),myVecTest.end(),myVecTest.begin(),boost::bind(std::plus<T::value_type>(),3,_1) );
out<<"(";std::for_each( myVecTest.begin(),myVecTest.end(), out << boost::lambda::_1 << '\t' );out<<")";
out<<'\n';
for(int testIndex=0; testIndex<myVecTest.size(); ++testIndex)
{
clock_t dwStartTime = clock();
for(int nTimes = 0; nTimes < 100000; ++nTimes) // 计算的次数
for(int i=0,nSize=vec.size(); i<nSize; ++i) // 遍历数组
myDst[i] = vec[i] + myVecTest[testIndex];
out<< "(+:" << clock() - dwStartTime << ")\t"; // 输出时间
dwStartTime = clock();
for(int nTimes = 0; nTimes < 100000; ++nTimes) // 计算的次数
for(int i=0,nSize=vec.size(); i<nSize; ++i) // 遍历数组
myDst[i] = vec[i] - myVecTest[testIndex];
out<< "(-:" << clock() - dwStartTime << ")\t"; // 输出时间
dwStartTime = clock();
for(int nTimes = 0; nTimes < 100000; ++nTimes) // 计算的次数
for(int i=0,nSize=vec.size(); i<nSize; ++i) // 遍历数组
myDst[i] = vec[i] * myVecTest[testIndex];
out<< "(*:" << clock() - dwStartTime << ")\t"; // 输出时间
//dwStartTime = clock();
//for(int nTimes = 0; nTimes < 100000; ++nTimes) // 计算的次数
// for(int i=0,nSize=vec.size(); i<nSize; ++i) // 遍历数组
// myDst[i] = (long)vec[i] % (long)myVecTest[testIndex];
//out<< "(%:" << clock() - dwStartTime << ")\t"; // 输出时间
dwStartTime = clock();
for(int nTimes = 0; nTimes < 100000; ++nTimes) // 计算的次数
for(int i=0,nSize=vec.size(); i<nSize; ++i) // 遍历数组
myDst[i] = vec[i] / myVecTest[testIndex];
out<< "(/:" << clock() - dwStartTime << ")\n"; // 输出时间
}
out<<'\n';
std::transform( vec.begin(),vec.end(),myVecTest.begin(),vec.begin(),std::plus<T::value_type>() );
}
int main(int argc,char *argv[])
{
long lSize = 1000;
std::vector<int> myVecInt(lSize);
std::vector<long> myVecLong(lSize);
std::vector<float> myVecFloat(lSize);
std::vector<double> myVecDouble(lSize);
// 产生测试数据
std::generate( myVecInt.begin(), myVecInt.end(),rand );
std::generate( myVecLong.begin(), myVecLong.end(),rand );
std::generate( myVecFloat.begin(), myVecFloat.end(), boost::bind( std::divides<float>(),6789,boost::bind(&rand) ) );
std::transform( myVecFloat.begin(), myVecFloat.end(), myVecDouble.begin(), boost::bind( std::multiplies<double>(),3,_1) );
// 显示前n个数据
int n = 6;
std::for_each( myVecInt.begin(),myVecInt.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
std::for_each( myVecLong.begin(),myVecLong.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
std::for_each( myVecFloat.begin(),myVecFloat.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
std::for_each( myVecDouble.begin(),myVecDouble.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
std::ostream &of = std::cout; // 输出到屏幕
//std::ofstream of("C:\\testTime.txt",std::ios_base::out); // 输出到文件
test( myVecInt, of );
test( myVecLong, of );
test( myVecFloat, of );
test( myVecDouble, of );
//of.close(); // 关闭文件
// 显示前n个数据
std::for_each( myVecInt.begin(),myVecInt.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
std::for_each( myVecLong.begin(),myVecLong.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
std::for_each( myVecFloat.begin(),myVecFloat.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
std::for_each( myVecDouble.begin(),myVecDouble.begin()+n, std::cout << boost::lambda::_1 << '\t' );std::cout<<"\n\n";
}
本文地址:http://com.8s8s.com/it/it22956.htm