gpt4 book ai didi

bash - BASH 很慢吗?

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:08 30 4
gpt4 key购买 nike

我在 SPOJ 上解决这个问题 - http://www.spoj.com/problems/ALICESIE/

问题归结为打印 (n+1)/2

这是我的 C 代码,在 0.03 秒内通过

    #include <stdio.h>
int main() {
int test, n;
scanf("%d", &test);
while(test--) {
scanf("%d", &n);
printf("%d\n", (n + 1) >> 1);
}
return 0;
}

虽然这是我的 BASH 代码,它给出了超过时间限制(即 > 1s)

read test
while (( test-- ))
do
read n
echo "$(((n+1)/2))"
done

谁能告诉我为什么会这样? BASH 很慢吗?谢谢。

最佳答案

Bash 在执行数字运算时很慢。但这不是 Bash 的设计目的。

Bash 可以非常快速地编写一个脚本来自动执行一些重复的操作。修改错误的 Bash 脚本并再次运行它是很快的。可以快速找出 Bash 脚本到底在做什么(而不是必须寻找您正在查看的 C 可执行文件的源代码)。

这个 list 还在继续。

C 和 Bash 是两种截然不同的语言和环境。如果您提示 Bash 运行缓慢,那么您用它来解决错误的问题。

“不要提示 Screwdriver 在将钉子钉入墙壁时很糟糕。”

关于bash - BASH 很慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53537277/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com