The examples below are from my chess game but the question is more general for runtime optimizing if-statements in Python. What type of code will run faster, and is there a huge difference? Bare in mind that in this case I will calculated this inside a minimax algorithm (thousands of times). Case 3 if of course nicer, but is it faster to run?
Another question would be if there is a difference in the order of which I put these values in? In this case the pawn option will come up the most times, is it then better to have that one first in the if-statements/dict?
# Case 1:
if piece_type == 'K':
value = 20000
elif piece_type == 'Q':
value = 900
elif piece_type == 'B':
value = 330
elif piece_type == 'N':
value = 320
elif piece_type == 'R':
value = 500
elif piece_type == 'p':
value = 100
return value
# Case 2:
if piece_type == 'K':
return 20000
elif piece_type == 'Q':
return 900
elif piece_type == 'B':
return 330
elif piece_type == 'N':
return 320
elif piece_type == 'R':
return 500
elif piece_type == 'p':
return 100
# Case 3:
piece_values = {'K': 20000, 'Q': 900, 'B': 330, 'N': 320, 'R': 500, 'p': 100}
return piece_values.get('K')