Python 字典 vs If 语句速度

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

问题描述

我发现一些链接谈到在 c++ 中的 switch case 比 if else 更快,因为它可以在编译中进行优化.然后我发现了一些人们认为使用字典可能比使用 If 语句更快的建议.然而,大部分对话都是关于某人的工作,最终只是讨论他们应该首先优化代码的其他部分,除非你做了数百万个 if else,否则这无关紧要.谁能解释这是为什么?

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 代码.我想检查它是哪个数字,然后执行一些操作.所以我可以做大量的 if else,或者我可以把每个数字放在字典里.为了争论起见,可以说它是一个单线程.

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?

谢谢:)

推荐答案

然而,大部分谈话都是关于某人的工作结束刚刚结束讨论他们应该首先优化代码的其他部分除非你做了数百万个 if else,否则这无关紧要.任何人都可以解释这是为什么?

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.

一个简单的例子......

A quick example...

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 '

Case 2'
dis.dis(f2)

...输出...

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 语句速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)