BASH很慢吗? [英] Is BASH very slow?

查看:83
本文介绍了BASH很慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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可执行文件的源代码而言).

列表继续.

C和Bash是两种非常不同的语言和环境.如果您抱怨Bash运行缓慢,则说明它是用于错误的问题.

不要抱怨螺丝刀在将钉子钉入墙壁时会吮吸."

I was solving this question on SPOJ - http://www.spoj.com/problems/ALICESIE/

What the question boils down to print (n+1)/2

This is my C code which passes in 0.03s

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

while this is my BASH code which gives Time Limit Exceeded ( i.e. > 1s )

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

Can anyone let me know why is this happening ? Is BASH very slow ? Thanks.

解决方案

Bash is slow in executing number-crunching. But that isn't what Bash was designed for.

Bash is very fast in whipping up a script for automating some repetitive action. It's fast to modify a faulty Bash script and run it again. It's fast to find out what exactly a Bash script is doing (as opposed to having to hunt down the source for the C executable you're looking at).

And the list goes on.

C and Bash are two very different breeds of languages and environments. If you complain about Bash being slow, you are using it for the wrong kind of problem.

"Do not complain that the screwdriver sucks at driving a nail into the wall."

这篇关于BASH很慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆