int power10(int exp)
{
int n = 1;
int i = 0;
for (i = 0; i < exp; ++i)
{
n = n * 10;
}
return n;
}
void ft_putnbr(int nb)
{
....
for (i = i - 1; i >= 0; --i)
{
ft_putchar((nb / power10(i)) % 10 + '0');
}
}
int power(int num, int exp)
{
if ((num == 0) || (exp < 0))
return 0;
int n = 1;
for (int i = 0; i < exp; ++i)
{
n = n * num;
}
return n;
}
最后,如果您想加快代码,则应重新考虑使用简单的静态势阵列计算10的呼叫,例如以下内容:
static int power10[] = {1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000};
....
void ft_putnbr(int nb)
{
....
for (i = i - 1; i >= 0; --i)
{
ft_putchar((nb / power10[i]) % 10 + '0');
}
}
The first candidate for improvements is the power() function.
If the exp parameter is 0, your function should return 1 immediately, without unnecessary calculations, loops etc. It can be achieved by moving the if instruction to the beginning of the function. Or, you can eliminate this checking using 1 as the start value for n and extending the loop, like below:
int power(int num, int exp)
{
int n = 1;
for (int i = 0; i < exp; ++i)
{
n = n * num;
}
return n;
}
The next thing is that you are using only powers of 10, so passing this by parameter is not necessary. Simply rename your function to power10() and pass the exponent only:
int power10(int exp)
{
int n = 1;
int i = 0;
for (i = 0; i < exp; ++i)
{
n = n * 10;
}
return n;
}
void ft_putnbr(int nb)
{
....
for (i = i - 1; i >= 0; --i)
{
ft_putchar((nb / power10(i)) % 10 + '0');
}
}
Or, if you want to have more "generic" power() fuction, you should add the checking for 0 number (in this case 0 value should be returned regardless to exponent), and for negative exponents (0 value should be returned too):
int power(int num, int exp)
{
if ((num == 0) || (exp < 0))
return 0;
int n = 1;
for (int i = 0; i < exp; ++i)
{
n = n * num;
}
return n;
}
Finally, if you want to speed-up your code, you should rethink replacing the call for calculating the power of 10 with simple static array of powers like below:
static int power10[] = {1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000};
....
void ft_putnbr(int nb)
{
....
for (i = i - 1; i >= 0; --i)
{
ft_putchar((nb / power10[i]) % 10 + '0');
}
}
发布评论
评论(1)
改进的第一个候选者是
power()
函数。exp
参数为0,则您的函数应立即返回1,而无需不必要的计算,循环等。 。或者,您可以使用1
作为n
的开始值并扩展循环,如下所示:10 ,因此不需要通过参数传递此内容。只需将您的函数重命名为
power10()
,然后仅通过指数:power()
fuction,则应添加检查检查0
number(在这种情况下, 0 值应返回,无论指数如何),对于负指数(0
值也应返回):10
的呼叫,例如以下内容:The first candidate for improvements is the
power()
function.exp
parameter is 0, your function should return 1 immediately, without unnecessary calculations, loops etc. It can be achieved by moving theif
instruction to the beginning of the function. Or, you can eliminate this checking using1
as the start value forn
and extending the loop, like below:10
, so passing this by parameter is not necessary. Simply rename your function topower10()
and pass the exponent only:power()
fuction, you should add the checking for0
number (in this case0
value should be returned regardless to exponent), and for negative exponents (0
value should be returned too):10
with simple static array of powers like below: