如何改进仅使用libc的写入功能的代码

发布于 2025-02-11 13:32:31 字数 1399 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2025-02-18 13:32:31

改进的第一个候选者是power()函数。

  1. 如果exp参数为0,则您的函数应立即返回1,而无需不必要的计算,循环等。 。或者,您可以使用1作为n的开始值并扩展循环,如下所示:
int power(int num, int exp)
{
    int n = 1;
    for (int i = 0; i < exp; ++i)
    {
        n = n * num;
    }
    return n;
}
  1. 接下来是您仅使用10 ,因此不需要通过参数传递此内容。只需将您的函数重命名为power10(),然后仅通过指数:
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');
    }
}
  1. 或者,如果要拥有更多的“通用” power() fuction,则应添加检查检查0 number(在这种情况下, 0 值应返回,无论指数如何),对于负指数(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;
}
  1. 最后,如果您想加快代码,则应重新考虑使用简单的静态势阵列计算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.

  1. 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;
}
  1. 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');
    }
}
  1. 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;
}
  1. 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');
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文