Python – get object – Is dictionary or if statements faster?

Question:

I am making a POST to a python script, the POST has 2 parameters. Name and Location, and then it returns one string. My question is I am going to have 100’s of these options, is it faster to do it in a dictionary like this:

myDictionary = {"Name":{"Location":"result", "LocationB":"resultB"},
                "Name2":{"Location2":"result2A", "Location2B":"result2B"}}

And then I would use.get("Name").get("Location") to get the results

OR do something like this:

if Name = "Name":
     if Location = "Location":
         result = "result"
     elif Location = "LocationB":
         result = "resultB"
elif Name = "Name2":
         if Location = "Location2B":
             result = "result2A"
         elif Location = "LocationB":
             result = "result2B"

Now if there are hundreds or thousands of these what is faster? Or is there a better way all together?

Asked By: james brown

||

Answers:

First of all:

Generally, it’s much more pythonic to match keys to values using dictionaries. You should do that from a point of style.

Secondly:

If you really care about performance, python might not always be the optimal tool. However, the dict approach should be much much faster, unless your selections happen about as often as the creation of these dicts. The creation of thousands and thousands of PyObjects to check your case is a really bad idea.

Thirdly:

If you care about your application so much, you might really want to benchmark both solutions — as usual when it comes to performance questions, there’s a million factors including your computing platform that only experiments will help to sort out

Fourth(ly?):

It looks like you’re building something like a protocol parser. That’s really not python’s forte, performance-wise. Maybe you’d want to look into one of the dozens of tools that can write C code parsers for you and wrap that in a native module, it’s pretty sure to be faster than either of your implementations, if done right.

Here’s the python documentation on Extending Python with C or C++

Answered By: Marcus Müller

I decided to test the two scenarios of 1000 Names and 2 locations

The Test Samples

Team Dictionary:

di = {}
for i in range(1000):
    di["Name{}".format(i)] = {'Location': 'result{}'.format(i), 'LocationB':'result{}B'.format(i)}

def get_dictionary_value():
    di.get("Name999").get("LocationB")

Team If Statement:

I used a python script to generate a 5000 line function if_statements(name, location): following this pattern

elif name == 'Name994':
    if location == 'Location':
        return 'result994'
    elif location == 'LocationB':
        return 'result994B'

# Some time later ...
def get_if_value():
    if_statements("Name999", "LocationB")

Timing Results

You can time with the timeit function to test the time it takes a function to complete.

import timeit
print(timeit.timeit(get_dictionary_value))
# 0.06353...

print(timeit.timeit(get_if_value))
# 6.3684...

So there you have it, dictionary was 100 times faster on my machine than the hefty 165 KB if-statement function.

Answered By: liamdiprose

I will root for dict().

In most cases [key] selection is much faster than conditional checks. Rule of thumb conditionals are generally used for boolean statements.

The reason for this is; when you create a dictionary you essentially create a registry of that data which is stored in as hashes in a bucket. When you say for instance my dictonary_name[‘key’] if that value exist python knows the exact location of that value and returns it in almost in an instant.

However conditionals are different. Conditionals are sequential checks meaning worse case it has to check every condition provided to first establish the value’s existence then it has return the respective data.

As you can see with 100’s of statements this can be problematic. Though in this case dictionaries are faster. You also need to be cognizant of how often and how quickly these checks are. Because if they are faster than the the building of your dictionary you might get an error of value not found.

Answered By: Decory Herbert