program tip

Python vs Bash-어떤 종류의 작업에서 각각 성능면에서 다른 작업을 능가합니까?

radiobox 2020. 9. 15. 07:44
반응형

Python vs Bash-어떤 종류의 작업에서 각각 성능면에서 다른 작업을 능가합니까?


분명히 Python은 더 사용자 친화적이며, Google의 빠른 검색은 Python이 바이트 컴파일되므로 일반적으로 더 빠르다는 많은 결과를 보여줍니다. 심지어 사전 기반 작업에서 2000 % 이상의 향상을 볼 수 있다고 주장하는 사실 도 발견 했습니다 .

이 문제에 대한 귀하의 경험은 무엇입니까? 어떤 종류의 작업에서 각자가 확실한 승자입니까?


일반적인 메인 프레임 흐름 ...

Input Disk/Tape/User (runtime) --> Job Control Language (JCL) --> Output Disk/Tape/Screen/Printer
                                   |                          ^
                                   v                          |
                                   `--> COBOL Program --------' 

일반적인 Linux 흐름 ...

Input Disk/SSD/User (runtime) --> sh/bash/ksh/zsh/... ----------> Output Disk/SSD/Screen/Printer
                                   |                          ^
                                   v                          |
                                   `--> Python script --------'
                                   |                          ^
                                   v                          |
                                   `--> awk script -----------'
                                   |                          ^
                                   v                          |
                                   `--> sed script -----------'
                                   |                          ^
                                   v                          |
                                   `--> C/C++ program --------'
                                   |                          ^
                                   v                          |
                                   `--- Java program ---------'
                                   |                          ^
                                   v                          |
                                   :                          :

쉘은 Linux의 접착제입니다.

sh / ksh / bash / ... 와 같은 Linux 쉘 은 이전 메인 프레임 작업 제어 언어와 매우 유사한 입력 / 출력 / 흐름 제어 지정 기능을 제공합니다. O / S가 지원하는 모든 언어로 작성된 다른 실행 프로세스와 데이터 및 제어를 효율적으로 전달하도록 최적화 된 상태에서 자체적 으로 Turing 완전한 언어 입니다.

대부분의 Linux 응용 프로그램은 대부분의 프로그램이 작성된 언어에 관계없이 쉘 스크립트에 의존하며 Bash 가 가장 일반적입니다. 데스크탑에서 아이콘을 클릭하면 일반적으로 짧은 Bash 스크립트 가 실행됩니다 . 이 스크립트는 직접 또는 간접적으로 필요한 모든 파일이 어디에 있는지 알고 변수와 명령 줄 매개 변수를 설정하여 마지막으로 프로그램을 호출합니다. 이것은 쉘의 가장 간단한 사용입니다.

그러나 우리가 알고있는 Linux는 시스템을 시작하고, 이벤트에 응답하고, 실행 우선 순위를 제어하고, 프로그램을 컴파일, 구성 및 실행하는 수천 개의 쉘 스크립트가 없으면 Linux가 될 수 없습니다. 이들 중 다수는 상당히 크고 복잡합니다.

셸은 컴파일 타임이 아닌 런타임에 함께 연결되는 미리 빌드 된 구성 요소를 사용할 수있는 인프라를 제공합니다. 이러한 구성 요소는 단독으로 또는 재 컴파일없이 다른 조합으로 사용할 수있는 독립형 프로그램입니다. 이들을 호출하는 구문은 Bash 내장 명령 의 구문과 구별 할 수 없으며 실제로 시스템에 독립 실행 형 실행 파일도 있으며 종종 추가 옵션이있는 수많은 내장 명령이 있습니다.

성능면에서 PythonBash 사이에는 언어 차원의 차이가 없습니다 . 그것은 전적으로 각각이 어떻게 코딩되고 어떤 외부 도구가 호출되는지에 달려 있습니다.

모든 같은 잘 알려진 도구 나오지 AWK, 그렙, BC 주, DC, TR, 등은 먼지 중 하나의 언어로 그 작업을하고 떠날 것이다. BashPython 보다 Bash 를 사용하는 도구와 같은 도구에서 데이터를 다시 호출하고 전달하는 것이 더 쉽고 효율적이기 때문에 그래픽 사용자 인터페이스가없는 모든 경우에 선호됩니다 .

공연

Bash 셸 스크립트가 호출 하는 프로그램 과 전체 처리량 및 / 또는 응답 성이 동등한 Python 보다 좋거나 나쁠 지 여부에 따라 주어진 하위 작업에 대한 적합성에 따라 다릅니다 . 문제를 복잡하게 만들기 위해 Python 은 대부분의 언어와 마찬가지로 다른 실행 파일을 호출 할 수도 있지만 더 번거롭고 자주 사용되지는 않습니다.

사용자 인터페이스

Python 이 확실한 승자 인 한 영역 은 사용자 인터페이스입니다. 이는 기본적으로 GTK 그래픽을 지원하고 Bash 보다 훨씬 직관적이기 때문에 로컬 또는 클라이언트-서버 애플리케이션을 구축하는 데 탁월한 언어입니다 .

Bash only understands text. Other tools must be called for a GUI and data passed back from them. A Python script is one option. Faster but less flexible options are the binaries like YAD, Zenity, and GTKDialog.

While shells like Bash work well with GUIs like Yad, GtkDialog (embedded XML-like interface to GTK+ functions), dialog, and xmessage, Python is usually easier and more capable.

Summary

Building with shell scripts is like assembling a computer with off-the-shelf components the way desktop PCs are.

Building with Python, C++ or most any other language is more like building a computer by soldering the chips (libraries) and other electronic parts together the way smartphones are.


Generally, bash works better than python only in those environments where python is not available. :)

Seriously, I have to deal with both languages daily, and will take python instantly over bash if given the choice. Alas, I am forced to use bash on certain "small" platforms because someone has (mistakenly, IMHO) decided that python is "too large" to fit.

While it is true that bash might be faster than python for some select tasks, it can never be as quick to develop with, or as easy to maintain (at least after you get past 10 lines of code or so). Bash's sole strong point wrt python or ruby or lua, etc., is its ubiquity.


Developer efficiency matters much more to me in scenarios where both bash and Python are sensible choices.

Some tasks lend themselves well to bash, and others to Python. It also isn't unusual for me to start something as a bash script and change it to Python as it evolves over several weeks.

A big advantage Python has is in corner cases around filename handling, while it has glob, shutil, subprocess, and others for common scripting needs.


When you writing scripts performance does not matter (in most cases).
If you care about performance 'Python vs Bash' is a false question.

Python:
+ easier to write
+ easier to maintain
+ easier code reuse (try to find universal error-proof way to include files with common code in sh, I dare you)
+ you can do OOP with it too!
+ easier arguments parsing. well, not easier, exactly. it still will be too wordy to my taste, but python have argparse facility built in.
- ugly ugly 'subprocess'. try to chain commands and not to cry a river how ugly your code will become. especially if you care about exit codes.

Bash:
+ ubiquity, as was said earlier, indeed.
+ simple commands chaining. that's how you glue together different commands in a simple way. Also Bash (not sh) have some improvements, like pipefail, so chaining is really short and expressive.
+ do not require 3rd-party programs to be installed. can be executed right away.
- god, it's full of gotchas. IFS, CDPATH.. thousands of them.

If one writing a script bigger than 100 LOC: choose Python
If one need path manipulation in script: choose Python(3)
If one need somewhat like alias but slightly complicated: choose Bash/sh

Anyway, one should try both sides to get the idea what are they capable of.

Maybe answer can be extended with packaging and IDE support points, but I'm not familiar with this sides.

As always you have to choose from turd sandwich and giant douche. And remember, just a few years ago Perl was new hope. Where it is now.


Performance-wise bash outperforms python in the process startup time.

Here are some measurements from my core i7 laptop running Linux Mint:

Starting process                       Startup time

empty /bin/sh script                   1.7 ms
empty /bin/bash script                 2.8 ms
empty python script                    11.1 ms
python script with a few libs*         110 ms

*Python loaded libs are: os, os.path, json, time, requests, threading, subprocess

This shows a huge difference however bash execution time degrades quickly if it has to do anything sensible since it usually must call external processes.

If you care about performance use bash only for:

  • really simple and frequently called scripts
  • scripts that mainly call other processes
  • when you need minimal friction between manual administrative actions and scripting - fast check a few commands and place them in the file.sh

Bash is primarily a batch / shell scripting language with far less support for various data types and all sorts of quirks around control structures -- not to mention compatibility issues.

Which is faster? Neither, because you are not comparing apples to apples here. If you had to sort an ascii text file and you were using tools like zcat, sort, uniq, and sed then you will smoke Python performance wise.

However, if you need a proper programming environment that supports floating point and various control flow, then Python wins hands down. If you wrote say a recursive algorithm in Bash and Python, the Python version will win in an order of magnitude or more.


If you are looking to cobble together a quick utility with minimal effort, bash is good. For a wrapper round an application, bash is invaluable.

Anything that may have you coming back over and over to add improvements is probably (though not always) better suited to a language like Python as Bash code comprising over a 1000 lines gets very painful to maintain. Bash code is also irritating to debug when it gets long.......

Part of the problem with these kind of questions is, from my experience, that shell scripts are usually all custom tasks. There have been very few shell scripting tasks that I have come across where there is already a solution freely available.


There are 2 scenario's where Bash performance is at least equal I believe:

  • Scripting of command line utilities
  • Scripts which take only a short time to execute; where starting the Python interpreter takes more time than the operation itself

That said, I usually don't really concern myself with performance of the scripting language itself. If performance is a real issue you don't script but program (possibly in Python).


I don't know if this is accurate, but I have found that python/ruby works much better for scripts that have a lot of mathematical computations. Otherwise you have to use dc or some other "arbitrary precision calculator". It just becomes a very big pain. With python you have much more control over floats vs ints and it is much easier to perform a lot of computations and sometimes.

In particular, I would never work with a bash script to handle binary information or bytes. Instead I would use something like python (maybe) or C++ or even Node.JS.


I'm posting this late answer primarily because Google likes this question.

I believe the issue and context really should be about the workflow, not the tools. The overall philosophy is always "Use the right tool for the job." But before this comes one that many often forget when they get lost in the tools: "Get the job done."

When I have a problem that isn't completely defined, I almost always start with Bash. I have solved some gnarly problems in large Bash scripts that are both readable and maintainable.

But when does the problem start to exceed what Bash should be asked to do? I have some checks I use to give me warnings:

  1. Am I wishing Bash had 2D (or higher) arrays? If yes, it's time to realize that Bash is not a great data processing language.
  2. Am I doing more work preparing data for other utilities than I am actually running those utilities? If yes, time again to realize Bash is not a great data processing language.
  3. Is my script simply getting too large to manage? If yes, it is important to realize that while Bash can import script libraries, it lacks a package system like other languages. It's really a "roll your own" language compared to most others. Then again, it has a enormous amount of functionality built-in (some say too much...)

The list goes on. Bottom-line, when you are working harder to keep your scripts running that you do adding features, it's time to leave Bash.

Let's assume you've decided to move your work to Python. If your Bash scripts are clean, the initial conversion is quite straightforward. There are even several converters / translators that will do the first pass for you.

The next question is: What do you give up moving to Python?

  1. All calls to external utilities must be wrapped in something from the subprocess module (or equivalent). There are multiple ways to do this, and until 3.7 it took some effort to get it right (3.7 improved subprocess.run() to handle all common cases on its own).

  2. Surprisingly, Python has no standard platform-independent non-blocking utility (with timeout) for polling the keyboard (stdin). The Bash read command is an awesome tool for simple user interaction. My most common use is to show a spinner until the user presses a key, while also running a polling function (with each spinner step) to make sure things are still running well. This is a harder problem than it would appear at first, so I often simply make a call to Bash: Expensive, but it does precisely what I need.

  3. If you are developing on an embedded or memory-constrained system, Python's memory footprint can be many times larger than Bash's (depending on the task at hand). Plus, there is almost always an instance of Bash already in memory, which may not be the case for Python.

  4. For scripts that run once and exit quickly, Python's startup time can be much longer than Bash's. But if the script contains significant calculations, Python quickly pulls ahead.

  5. Python has the most comprehensive package system on the planet. When Bash gets even slightly complex, Python probably has a package that makes whole chunks of Bash become a single call. However, finding the right package(s) to use is the biggest and most daunting part of becoming a Pythonista. Fortunately, Google and StackExchange are your friends.

참고URL : https://stackoverflow.com/questions/2424921/python-vs-bash-in-which-kind-of-tasks-each-one-outruns-the-other-performance-w

반응형