蟒蛇 |效率和性能

发布于 2024-08-25 13:14:06 字数 557 浏览 4 评论 0原文

假设我要通过运行单个脚本在列表中保存 100 个浮点数,很可能需要一些内存来处理。因此,如果此代码每次作为应用程序的要求执行,则会影响性能,所以我的问题是如何保持效率以获得绩效。

模拟代码:

def generate_lglt():
    float1, float2 = 27.2423423, 12.2323245
    lonlats = []
    for val in range(100, 0, -1):
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
    print lonlats

谢谢。

Lets say I'm going to save 100 floating point numbers in a list by running a single script, most probably it will take some memory to process.So if this code executes every time as a requirement of an application there will be performance hits, so my question is how to maintain efficiency in order to gain performance.

Mock-up code:

def generate_lglt():
    float1, float2 = 27.2423423, 12.2323245
    lonlats = []
    for val in range(100, 0, -1):
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
    print lonlats

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2024-09-01 13:14:06

瓶颈出现在意想不到的地方,因此永远不要仅仅因为您认为代码可能是需要改进的正确代码而对其进行优化。你需要做的就是

  1. 编写你的程序,使其完全运行。
  2. 开发测试以确保您的程序正确。
  3. 确定您的程序是否太慢。
    • 您很有可能会在这一步退出。
  4. 开发实际运行程序的性能测试。
  5. 使用 cProfile 模块在实际性能测试中分析代码。
  6. 找出哪些算法改进可以提高代码的性能。
    • 这通常是最能提高速度的方法。
  7. 如果您使用最适合该工作的算法,请执行微优化。
    • 用 C 语言重写关键部分(可能使用 Cython)通常比 Python 中的微优化更有效。

Bottlenecks occur at unexpected places, so never optimize code just because you think it might be the right code to try to improve. What you need to do is

  1. Write your program so that it runs completely.
  2. Develop tests to make sure your program is correct.
  3. Decide whether your program is too slow.
    • There is a good chance you will quit at this step.
  4. Develop performance tests that run your program realistically.
  5. Profile the code in its realistic performance tests using the cProfile module.
  6. Figure out what algorithmic improvements can improve your code's performance.
    • This is usually the way to improve speed the most.
  7. If you are using the best algorithm for the job, perform micro-optimizations.
    • Rewriting critical parts in C (possibly using Cython) is often more effective than in-Python micro-optimizations.
最美的太阳 2024-09-01 13:14:06

如果要多次调用generate_lglt(),您可能希望避免在每次调用代码时重新生成相同的范围(100,0,-1)。您可能希望将生成的范围缓存在某处并一遍又一遍地使用它。

另外,如果要退出 for 循环而不完成每次迭代,请使用 xrange 而不是 range。

If generate_lglt() is going to be called a lot of different times, you may want to keep from regenerating the same range(100,0,-1) with every call of the code. You may want to cache that generated range somewhere and use it over and over again.

Also, if you are going to be exiting a for loop without completing each iteration, use xrange instead of range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文