• <tfoot id='vpm4r'></tfoot>

    <small id='vpm4r'></small><noframes id='vpm4r'>

    • <bdo id='vpm4r'></bdo><ul id='vpm4r'></ul>

      <i id='vpm4r'><tr id='vpm4r'><dt id='vpm4r'><q id='vpm4r'><span id='vpm4r'><b id='vpm4r'><form id='vpm4r'><ins id='vpm4r'></ins><ul id='vpm4r'></ul><sub id='vpm4r'></sub></form><legend id='vpm4r'></legend><bdo id='vpm4r'><pre id='vpm4r'><center id='vpm4r'></center></pre></bdo></b><th id='vpm4r'></th></span></q></dt></tr></i><div id='vpm4r'><tfoot id='vpm4r'></tfoot><dl id='vpm4r'><fieldset id='vpm4r'></fieldset></dl></div>
    1. <legend id='vpm4r'><style id='vpm4r'><dir id='vpm4r'><q id='vpm4r'></q></dir></style></legend>
      1. Python 字典 vs If 语句速度

        Python Dictionary vs If Statement Speed(Python 字典 vs If 语句速度)
            <i id='XWRLU'><tr id='XWRLU'><dt id='XWRLU'><q id='XWRLU'><span id='XWRLU'><b id='XWRLU'><form id='XWRLU'><ins id='XWRLU'></ins><ul id='XWRLU'></ul><sub id='XWRLU'></sub></form><legend id='XWRLU'></legend><bdo id='XWRLU'><pre id='XWRLU'><center id='XWRLU'></center></pre></bdo></b><th id='XWRLU'></th></span></q></dt></tr></i><div id='XWRLU'><tfoot id='XWRLU'></tfoot><dl id='XWRLU'><fieldset id='XWRLU'></fieldset></dl></div>
              <bdo id='XWRLU'></bdo><ul id='XWRLU'></ul>

              <small id='XWRLU'></small><noframes id='XWRLU'>

              <tfoot id='XWRLU'></tfoot>
                <tbody id='XWRLU'></tbody>
                  <legend id='XWRLU'><style id='XWRLU'><dir id='XWRLU'><q id='XWRLU'></q></dir></style></legend>
                  本文介绍了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 语句速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    1. <legend id='CsFh6'><style id='CsFh6'><dir id='CsFh6'><q id='CsFh6'></q></dir></style></legend>

                          <tbody id='CsFh6'></tbody>

                        <small id='CsFh6'></small><noframes id='CsFh6'>

                          <bdo id='CsFh6'></bdo><ul id='CsFh6'></ul>

                          1. <tfoot id='CsFh6'></tfoot>

                            <i id='CsFh6'><tr id='CsFh6'><dt id='CsFh6'><q id='CsFh6'><span id='CsFh6'><b id='CsFh6'><form id='CsFh6'><ins id='CsFh6'></ins><ul id='CsFh6'></ul><sub id='CsFh6'></sub></form><legend id='CsFh6'></legend><bdo id='CsFh6'><pre id='CsFh6'><center id='CsFh6'></center></pre></bdo></b><th id='CsFh6'></th></span></q></dt></tr></i><div id='CsFh6'><tfoot id='CsFh6'></tfoot><dl id='CsFh6'><fieldset id='CsFh6'></fieldset></dl></div>