Python字典vs If语句速度 [英] Python Dictionary vs If Statement Speed

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

问题描述

我发现一些链接,在c ++中比switch更快,因为它可以在编译中进行优化。然后,我发现人们使用字典的一些建议可能比If语句更快。然而,大部分的谈话都是关于某人的工作,最后只是讨论一下,他们应该首先优化代码的其他部分,除非你做数以百万计的其他事情,否则它不重要。有人可以解释为什么这是吗?

I have found a few links talking about switch cases being faster in c++ than if else because it can be optimized in compilation. I then found some suggestions people had that using a dictionary may be faster than an If statement. However, most of the conversation are about someones work end just end up discussing that they should optimize other parts of the code first and it wont matter unless your doing millions of if else. Can anyone explain why this is?

说我有100个独特的数字,将被不断地流式传输到一个python代码。我想检查它是哪个号码,然后执行一些。所以我可以做一吨,否则,或者我可以把每个数字在一个字典。为了争论,让我们说一个线程。

Say I have 100 unique numbers that are going to be streamed in to a python code constantly. I want to check which number it is, then execute something. So i could either do a ton of if else, or i could put each number in a dictionary. For arguments sake, lets say its a single thread.

有人理解python和低级别执行之间的层次,可以解释这是如何工作的?

Does someone understand the layer between python and the low level execution that can explain how this is working?

谢谢:)

推荐答案


然而,大部分的谈话是关于某人的工作结束只是结束
讨论他们应该首先优化代码的其他部分
,它不会重要,除非你做数百万,否则。任何人可以
解释为什么这是?

However, most of the conversation are about someones work end just end up discussing that they should optimize other parts of the code first and it wont matter unless your doing millions of if else. Can anyone explain why this is?

一般来说,你应该只是想要优化代码,如果你真的需要,即如果程序的性能不佳,那么程序的性能就会变得很慢。

Generally, you should only bother to optimize code if you really need to, i.e. if the program's performance is unusably slow.

如果是这种情况,你应该使用一个分析器来确定哪些部分实际上是最大的问题。对于Python, cProfile 模块非常好。

If this is the case, you should use a profiler to determine which parts are actually causing the most problems. For Python, the cProfile module is pretty good for this.


有人理解python和低级
执行之间的层次,可以解释这是如何工作的?

Does someone understand the layer between python and the low level execution that can explain how this is working?

如果您想了解代码的执行方式,请查看 dis 模块。

If you want to get an idea of how your code executes, take a look at the dis module.

一个快速示例...

import dis

# Here are the things we might want to do
def do_something_a():
    print 'I did a'


def do_something_b():
    print 'I did b'


def do_something_c():
    print 'I did c'


# Case 1
def f1(x):
    if x == 1:
        do_something_a()
    elif x == 2:
        do_something_b()
    elif x == 3:
        do_something_c()


# Case 2
FUNC_MAP = {1: do_something_a, 2: do_something_b, 3: do_something_c}
def f2(x):
    FUNC_MAP[x]()


# Show how the functions execute
print 'Case 1'
dis.dis(f1)
print '\n\nCase 2'
dis.dis(f2)

...哪些输出...

...which outputs...

Case 1
 18           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (1)
              6 COMPARE_OP               2 (==)
              9 POP_JUMP_IF_FALSE       22

 19          12 LOAD_GLOBAL              0 (do_something_a)
             15 CALL_FUNCTION            0
             18 POP_TOP
             19 JUMP_FORWARD            44 (to 66)

 20     >>   22 LOAD_FAST                0 (x)
             25 LOAD_CONST               2 (2)
             28 COMPARE_OP               2 (==)
             31 POP_JUMP_IF_FALSE       44

 21          34 LOAD_GLOBAL              1 (do_something_b)
             37 CALL_FUNCTION            0
             40 POP_TOP
             41 JUMP_FORWARD            22 (to 66)

 22     >>   44 LOAD_FAST                0 (x)
             47 LOAD_CONST               3 (3)
             50 COMPARE_OP               2 (==)
             53 POP_JUMP_IF_FALSE       66

 23          56 LOAD_GLOBAL              2 (do_something_c)
             59 CALL_FUNCTION            0
             62 POP_TOP
             63 JUMP_FORWARD             0 (to 66)
        >>   66 LOAD_CONST               0 (None)
             69 RETURN_VALUE


Case 2
 29           0 LOAD_GLOBAL              0 (FUNC_MAP)
              3 LOAD_FAST                0 (x)
              6 BINARY_SUBSCR
              7 CALL_FUNCTION            0
             10 POP_TOP
             11 LOAD_CONST               0 (None)
             14 RETURN_VALUE

...所以很容易看出哪个函数必须执行最多的指令。

...so it's pretty easy to see which function has to execute the most instructions.

至于哪一个实际上更快通过分析代码,您需要检查。

As for which is actually faster, that's something you'd have to check by profiling the code.

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

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