mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
translated
This commit is contained in:
parent
e041bbc4c5
commit
d7090acac8
@ -0,0 +1,67 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (geekpi)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
[#]: subject: (Bash vs. Python: Which language should you use?)
|
||||||
|
[#]: via: (https://opensource.com/article/19/4/bash-vs-python)
|
||||||
|
[#]: author: (Archit Modi (Red Hat) https://opensource.com/users/architmodi/users/greg-p/users/oz123)
|
||||||
|
|
||||||
|
Bash 对比 Python:你应该使用哪种语言?
|
||||||
|
======
|
||||||
|
两种编程语言都有利有弊,这使得它们在某些任务上比对方更好。
|
||||||
|
![][1]
|
||||||
|
|
||||||
|
[Bash][2] 和 [Python][3] 是大多数自动化工程师最喜欢的编程语言。两者都有利有弊,有时很难选择应该使用哪一种。诚实的回答是:它取决于任务、范围、上下文和复杂性。
|
||||||
|
|
||||||
|
让我们比较这两种语言,以便更好地了解每种语言的用处。
|
||||||
|
|
||||||
|
### Bash
|
||||||
|
|
||||||
|
* 是 Linux/Unix shell 命令语言
|
||||||
|
* 非常适合编写使用命令行 (CLI) 工具,从一个命令到另一个命令(管道)的输出,以及执行简单任务(最多 100 行代码)的 shell 脚本
|
||||||
|
* 可以按原样使用命令行命令和工具
|
||||||
|
* 具有比 Python 更佳的启动时间,但执行时间性能较差
|
||||||
|
* 未在 Windows 中预装。你的脚本可能与多个操作系统不兼容,但 Bash 是大多数 Linux/Unix 系统上的默认 shell
|
||||||
|
* _不_与其他 shell 完全兼容(例如 csh、zsh、fish)
|
||||||
|
* 管道(“|”)CLI 工具(如 sed、awk、grep 等)可能会降低其性能
|
||||||
|
* 缺少许多函数、对象、数据结构和多线程,这限制了它在复杂脚本/编程中的使用
|
||||||
|
* 缺乏良好的调试工具和程序
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Python
|
||||||
|
|
||||||
|
* 是一门面向对象编程 (OOP) 语言,因此它比 Bash 更通用
|
||||||
|
* 几乎可以用于任何任务
|
||||||
|
* 适用于大多数主流操作系统,默认情况下也安装在大多数 Unix/Linux 系统上
|
||||||
|
* 与编写伪代码非常相似
|
||||||
|
* 具有简单、清晰、易于学习和易于阅读的语法
|
||||||
|
* 拥有大量的库,文档和活跃的社区
|
||||||
|
* 提供比 Bash 更好的错误处理功能
|
||||||
|
* 拥有比 Bash 更好的调试工具和程序,这使它成为开发含有大量代码的复杂软件的理想语言
|
||||||
|
* 应用(或脚本)可以包含许多必须在执行之前安装的第三方依赖项
|
||||||
|
* 与 Bash 相比,需要为简单任务编写更多代码行
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
我希望这些列表能让你更好地了解使用哪种语言以及何时使用它。
|
||||||
|
|
||||||
|
你在日常工作中使用哪种语言,Bash 还是 Python?请分享评论。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://opensource.com/article/19/4/bash-vs-python
|
||||||
|
|
||||||
|
作者:[Archit Modi (Red Hat)][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[geekpi](https://github.com/geekpi)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://opensource.com/users/architmodi/users/greg-p/users/oz123
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_happy_sad_developer_programming.png?itok=72nkfSQ_
|
||||||
|
[2]: /article/18/7/admin-guide-bash
|
||||||
|
[3]: /article/17/11/5-approaches-learning-python
|
@ -7,17 +7,16 @@
|
|||||||
[#]: via: (https://opensource.com/article/19/4/parallel-computation-python-dask)
|
[#]: via: (https://opensource.com/article/19/4/parallel-computation-python-dask)
|
||||||
[#]: author: (Moshe Zadka (Community Moderator) https://opensource.com/users/moshez)
|
[#]: author: (Moshe Zadka (Community Moderator) https://opensource.com/users/moshez)
|
||||||
|
|
||||||
Parallel computation in Python with Dask
|
使用 Dask 在 Python 中进行并行计算
|
||||||
======
|
======
|
||||||
The Dask library scales Python computation to multiple cores or even to
|
Dask 库将 Python 计算扩展到多个核心甚至是多台机器。
|
||||||
multiple machines.
|
|
||||||
![Pair programming][1]
|
![Pair programming][1]
|
||||||
|
|
||||||
One frequent complaint about Python performance is the [global interpreter lock][2] (GIL). Because of GIL, only one thread can execute Python byte code at a time. As a consequence, using threads does not speed up computation—even on modern, multi-core machines.
|
关于 Python 性能的一个常见抱怨是[全局解释器锁][2](GIL)。由于 GIL,一次只能有一个线程执行 Python 字节码。因此,即使在现代的多核机器上,使用线程也不会加速计算。
|
||||||
|
|
||||||
But when you need to parallelize to many cores, you don't need to stop using Python: the **[Dask][3]** library will scale computation to multiple cores or even to multiple machines. Some setups configure Dask on thousands of machines, each with multiple cores; while there are scaling limits, they are not easy to hit.
|
但当你需要并行化到多核时,你不需要停止使用 Python:**[Dask][3]** 库可以将计算扩展到多个内核甚至多个机器。某些设置在数千台机器上配置 Dask,每台机器都有多个内核。虽然存在扩展限制,但并不容易达到。
|
||||||
|
|
||||||
While Dask has many built-in array operations, as an example of something not built-in, we can calculate the [skewness][4]:
|
虽然 Dask 有许多内置的数组操作,但举一个非内置的例子,我们可以计算[偏度][4]:
|
||||||
```
|
```
|
||||||
import numpy
|
import numpy
|
||||||
import dask
|
import dask
|
||||||
@ -32,11 +31,11 @@ skewness = ((unnormalized_moment - (3 * mean * stddev ** 2) - mean ** 3) /
|
|||||||
stddev ** 3)
|
stddev ** 3)
|
||||||
```
|
```
|
||||||
|
|
||||||
Notice that each operation will use as many cores as needed. This will parallelize across all cores, even when calculating across billions of elements.
|
请注意,每个操作将根据需要使用尽可能多的内核。这将在所有核心上并行化,即使在计算数十亿个元素时也是如此。
|
||||||
|
|
||||||
Of course, it is not always the case that our operations can be parallelized by the library; sometimes we need to implement parallelism on our own.
|
当然,并不是我们所有的操作都可由库并行化,有时我们需要自己实现并行性。
|
||||||
|
|
||||||
For that, Dask has a "delayed" functionality:
|
为此,Dask 有一个“延迟”功能:
|
||||||
```
|
```
|
||||||
import dask
|
import dask
|
||||||
|
|
||||||
@ -48,9 +47,9 @@ total = dask.delayed(sum)(palindromes)
|
|||||||
result = total.compute()
|
result = total.compute()
|
||||||
```
|
```
|
||||||
|
|
||||||
This will calculate whether strings are palindromes in parallel and will return a count of the palindromic ones.
|
这将计算字符串是否是回文并返回回回文的数量。
|
||||||
|
|
||||||
While Dask was created for data scientists, it is by no means limited to data science. Whenever we need to parallelize tasks in Python, we can turn to Dask—GIL or no GIL.
|
虽然 Dask 是为数据科学家创建的,但它绝不仅限于数据科学。每当我们需要在 Python 中并行化任务时,我们可以使用 Dask-有 GIL 或没有 GIL。
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -58,7 +57,7 @@ via: https://opensource.com/article/19/4/parallel-computation-python-dask
|
|||||||
|
|
||||||
作者:[Moshe Zadka (Community Moderator)][a]
|
作者:[Moshe Zadka (Community Moderator)][a]
|
||||||
选题:[lujun9972][b]
|
选题:[lujun9972][b]
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
译者:[geekpi](https://github.com/geekpi)
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user