问题描述
这两种方法有什么用C更有效率?又有怎样的:
POW(X,3)
VS。
X * X * X //等?
我测试之间的性能差异 X * X * ...
VS POW(X,I)
小型 I
使用此code:
的#include< cstdlib>
#包括LT&;&CMATH GT;
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time.hpp>内嵌的boost ::了posix_time ::现在分组时间()
{
返回的boost ::了posix_time :: microsec_clock :: LOCAL_TIME();
}#定义TEST(NUM,前pression)\\
双重考验## NUM(双B,长循环)\\
{\\
双X = 0.0; \\
\\
提高::了posix_time ::分组时间的startTime现在=(); \\
为(长I = 0; I<循环; ++ I)\\
{\\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
X + =前pression; \\
} \\
提高::了posix_time :: TIME_DURATION经过= NOW() - startTime时; \\
\\
性病::法院LT&;<经过<< ; \\
\\
返回X; \\
}试验(1,B)
试验(2,b *的二)
试验(3,B * B * B)
试验(4,B * B * B * B)
试验(5,B * B * B * B * B)模板< INT指数>
双testpow(双基,长循环)
{
双X = 0.0; 提高::了posix_time ::分组时间的startTime现在=();
为(长I = 0; I<循环; ++ I)
{
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
X + =的std :: POW(基数,指数);
}
提高::了posix_time :: TIME_DURATION经过= NOW() - startTime时; 性病::法院LT&;<经过<< ; 返回X;
}诠释的main()
{
使用std ::法院;
长循环=亿升;
双X = 0.0;
COUT<< 1;
X + = testpow< 1 GT;(RAND(),循环);
X + = TEST1(RAND(),循环); COUT<< \\ N2;
X + = testpow 2>(RAND(),循环);
X + = test2的(RAND(),循环); COUT<< \\ N3
X + = testpow 3;>(RAND(),循环);
X + = TEST3(RAND(),循环); COUT<< \\ N4
X + = testpow 4;>(RAND(),循环);
X + = TEST4(RAND(),循环); COUT<< \\ N5
X + = testpow小于5>(RAND(),循环);
X + = TEST5(RAND(),循环);
COUT<< \\ n<<点¯x所述&;&下; \\ n;
}
结果是:
1 00:00:01.126008 00:00:01.128338
2 00:00:01.125832 00:00:01.127227
3 00:00:01.125563 00:00:01.126590
4 00:00:01.126289 00:00:01.126086
5 00:00:01.126570 00:00:01.125930
2.45829e + 54
请注意,我每累计战俘计算的结果,以确保编译器不优化它了。
如果我使用的std ::战俘(双,双)
版本和循环=百万升
,我得到:
1 00:00:00.011339 00:00:00.011262
2 00:00:00.011259 00:00:00.011254
3 00:00:00.975658 00:00:00.011254
4 00:00:00.976427 00:00:00.011254
5 00:00:00.973029 00:00:00.011254
2.45829e + 52
这是对英特尔的Core Duo运行Ubuntu 9.10 64位。使用gcc 4.4.1 -o2优化编译。
因此,在C,是 X * X * X
将会比 POW(X,3)
更快,因为没有战俘(双,INT)
超载。在C ++中,这将是大致相同的。 (假设在我的测试方法是正确的。)
这是在回应一个Markm发表的意见:
即使是出台了使用命名空间std
指令,如果第二个参数 POW
是 INT
,然后从的std ::战俘(双,INT)
超载< CMATH>
将被调用,而不是 ::战俘(双,双)
从<文件math.h>
。
此测试code确认行为:
的#include<&iostream的GT;命名空间富
{ 双杆(双X,int i)以
{
性病::法院LT&;< 富::酒吧\\ N的;
返回X *我;
}
}双杆(双X,双Y)
{
性病::法院LT&;< ::谷仓;
返回X * Y;
}使用命名空间富;诠释的main()
{
双一=巴(1.2,3); //打印富::酒吧
性病::法院LT&;< A<< \\ n;
返回0;
}
What of these two methods is in C more efficient? And how about:
pow(x,3)
vs.
x*x*x // etc?
I tested the performance difference between x*x*...
vs pow(x,i)
for small i
using this code:
#include <cstdlib>
#include <cmath>
#include <boost/date_time/posix_time/posix_time.hpp>
inline boost::posix_time::ptime now()
{
return boost::posix_time::microsec_clock::local_time();
}
#define TEST(num, expression) \
double test##num(double b, long loops) \
{ \
double x = 0.0; \
\
boost::posix_time::ptime startTime = now(); \
for (long i=0; i<loops; ++i) \
{ \
x += expression; \
x += expression; \
x += expression; \
x += expression; \
x += expression; \
x += expression; \
x += expression; \
x += expression; \
x += expression; \
x += expression; \
} \
boost::posix_time::time_duration elapsed = now() - startTime; \
\
std::cout << elapsed << " "; \
\
return x; \
}
TEST(1, b)
TEST(2, b*b)
TEST(3, b*b*b)
TEST(4, b*b*b*b)
TEST(5, b*b*b*b*b)
template <int exponent>
double testpow(double base, long loops)
{
double x = 0.0;
boost::posix_time::ptime startTime = now();
for (long i=0; i<loops; ++i)
{
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
x += std::pow(base, exponent);
}
boost::posix_time::time_duration elapsed = now() - startTime;
std::cout << elapsed << " ";
return x;
}
int main()
{
using std::cout;
long loops = 100000000l;
double x = 0.0;
cout << "1 ";
x += testpow<1>(rand(), loops);
x += test1(rand(), loops);
cout << "\n2 ";
x += testpow<2>(rand(), loops);
x += test2(rand(), loops);
cout << "\n3 ";
x += testpow<3>(rand(), loops);
x += test3(rand(), loops);
cout << "\n4 ";
x += testpow<4>(rand(), loops);
x += test4(rand(), loops);
cout << "\n5 ";
x += testpow<5>(rand(), loops);
x += test5(rand(), loops);
cout << "\n" << x << "\n";
}
Results are:
1 00:00:01.126008 00:00:01.128338
2 00:00:01.125832 00:00:01.127227
3 00:00:01.125563 00:00:01.126590
4 00:00:01.126289 00:00:01.126086
5 00:00:01.126570 00:00:01.125930
2.45829e+54
Note that I accumulate the result of every pow calculation to make sure the compiler doesn't optimize it away.
If I use the std::pow(double, double)
version, and loops = 1000000l
, I get:
1 00:00:00.011339 00:00:00.011262
2 00:00:00.011259 00:00:00.011254
3 00:00:00.975658 00:00:00.011254
4 00:00:00.976427 00:00:00.011254
5 00:00:00.973029 00:00:00.011254
2.45829e+52
This is on an Intel Core Duo running Ubuntu 9.10 64bit. Compiled using gcc 4.4.1 with -o2 optimization.
So in C, yes x*x*x
will be faster than pow(x, 3)
, because there is no pow(double, int)
overload. In C++, it will be the roughly same. (Assuming the methodology in my testing is correct.)
This is in response to the comment made by An Markm:
Even if a using namespace std
directive was issued, if the second parameter to pow
is an int
, then the std::pow(double, int)
overload from <cmath>
will be called instead of ::pow(double, double)
from <math.h>
.
This test code confirms that behavior:
#include <iostream>
namespace foo
{
double bar(double x, int i)
{
std::cout << "foo::bar\n";
return x*i;
}
}
double bar(double x, double y)
{
std::cout << "::bar\n";
return x*y;
}
using namespace foo;
int main()
{
double a = bar(1.2, 3); // Prints "foo::bar"
std::cout << a << "\n";
return 0;
}
这篇关于这是更有效率?使用POW方或只是与自身相乘呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!