我是靠谱客的博主 跳跃白羊,最近开发中收集的这篇文章主要介绍python字典速度能比字典高多少,Python字典vs 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?

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.

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

Thanks :)

解决方案

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.

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.

Does someone understand the layer between python and the low level

execution that can explain how this is working?

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 'nnCase 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字典速度能比字典高多少,Python字典vs If语句速度的全部内容,希望文章能够帮你解决python字典速度能比字典高多少,Python字典vs If语句速度所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部