加速 Python [英] Speeding Up Python

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

问题描述

这确实是两个问题,但它们非常相似,为了简单起见,我想我应该把它们放在一起:

This is really two questions, but they are so similar, and to keep it simple, I figured I'd just roll them together:

  • 首先:给定一个已建立的 Python 项目,除了简单的代码优化之外,还有哪些不错的方法可以加快它的速度?

  • Firstly: Given an established python project, what are some decent ways to speed it up beyond just plain in-code optimization?

其次:在用python从头开始编写程序时,有哪些可以大大提高性能的好方法?

Secondly: When writing a program from scratch in python, what are some good ways to greatly improve performance?

对于第一个问题,假设您收到了一个编写得体的项目,您需要提高性能,但您似乎无法通过重构/优化获得太多收益.在这种情况下,除了用 C 之类的东西重写它之外,你会怎么做来加速它?

For the first question, imagine you are handed a decently written project and you need to improve performance, but you can't seem to get much of a gain through refactoring/optimization. What would you do to speed it up in this case short of rewriting it in something like C?

推荐答案

关于其次:当用python从头开始编写程序时,有哪些可以大大提高性能的好方法?"

Regarding "Secondly: When writing a program from scratch in python, what are some good ways to greatly improve performance?"

记住优化的杰克逊规则:

Remember the Jackson rules of optimization:

  • 规则 1:不要这样做.
  • 规则 2(仅限专家):先不要这样做.

还有克努斯法则:

  • 过早的优化是万恶之源."

更有用的规则在优化的一般规则.

  1. 不要边走边优化.先弄对.那就快点拿吧.优化错误的程序仍然是错误的.

  1. Don't optimize as you go. First get it right. Then get it fast. Optimizing a wrong program is still wrong.

记住 80/20 规则.

Remember the 80/20 rule.

始终运行之前"和之后"基准测试.否则,您将不知道是否找到了 80%.

Always run "before" and "after" benchmarks. Otherwise, you won't know if you've found the 80%.

使用正确的算法和数据结构.这个规则应该是第一位的.没有什么比算法和数据结构更重要.

Use the right algorithms and data structures. This rule should be first. Nothing matters as much as algorithm and data structure.

底线

您无法阻止或避免优化此程序"的努力.这是工作的一部分.就像设计、代码和测试活动一样,你必须为它计划并仔细地做.

You can't prevent or avoid the "optimize this program" effort. It's part of the job. You have to plan for it and do it carefully, just like the design, code and test activities.

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

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