我有一个程序,它对权力26和结果数的根来说是2。
程序的源代码包含在几个文件中,我用带有-pg
标志的makefile编译它。我按gprof ./main
运行它,结果我得到了:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
51.95 1.73 1.73 52 0.03 0.03 very_smart_add
32.73 2.82 1.09 67108863 0.00 0.00 give_me_sum
11.71 3.21 0.39 13 0.03 0.16 the_middle_of
3.60 3.33 0.12 26 0.00 0.05 give_me_product
0.00 3.33 0.00 13 0.00 0.13 the_middle_of2
0.00 3.33 0.00 1 0.00 1.21 give_me_power
0.00 3.33 0.00 1 0.00 2.12 square_root
我想改进最耗时的功能,但我不知道如何做到这一点。在这种情况下可以做些什么?
文件:
part1.c
:
long long give_me_product(long long a, long long b);
long long give_me_power(long long a, long long b)
{
long long ret = 1;
while (b--)
{
ret = give_me_product(a, ret);
}
return ret;
}
part2.c
:
long long give_me_sum(long long a, long long b);
long long give_me_product(long long a, long long b)
{
long long ret = 0;
while (b--)
{
ret = give_me_sum(a, ret);
}
return ret;
}
part3.c
:
long long give_me_sum(long long a, long long b)
{
long long ret = 0;
while (a--)
{
ret++;
}
return ret + b;
while (b--)
{
ret++;
}
return ret;
}
sqrt.c
:
#define EPS 0.0000000001
#define STEP 1.0
/* This function adds two numbers. */
double very_smart_add(double a, double b)
{
while (b >= STEP)
{
a += STEP;
b -= STEP;
}
a += b;
return a;
}
double the_middle_of2(double a, double b)
{
double l = a, r = b;
double check, m;
while (1)
{
m = very_smart_add(l, r)/2;
check = very_smart_add(m, m);
if (check > very_smart_add(a, b) + EPS)
r = m;
else if (check < very_smart_add(a, b) - EPS)
l = m;
else
return m;
}
}
double the_middle_of(double a, double b)
{
double r = 0;
double s = a + b;
while (r + r < s)
{
r += 1.0;
}
return the_middle_of2(r - 1.0, s - (r - 1.0));
}
double square_root(double x)
{
double l = 0, r = x;
double check, m;
while (1)
{
m = the_middle_of(l, r);
check = m * m;
if (check > x + EPS)
r = m;
else if (check < x - EPS)
l = m;
else
return m;
}
}
答案 0 :(得分:1)
看起来像是一个家庭作业,但我会提出一个简化添加的想法 - 摆脱不必要的while循环,并可能检测溢出。
将其移至社区维基。
var Component2 = React.createClass({
doSomethingInParent: function() {
console.log('I called from component 2');
},
render: function() {
return (
<div><component1 callback={this.doSomethingInParent} /></div>
)
}
})
答案 1 :(得分:0)
你give_me_power
函数是O(n)。您可以将其优化为O(log n)。
long long give_me_power(long long a, long long b)
{
long long ret = 1;
while (b)
{
if ( b & 1) {
ret *= a;
b--;
} else {
a *= a;
b >>= 1;
}
}
return ret;
}