EasilyBaffled
EasilyBaffled

Reputation: 3882

Fastest Get Python Data Structures

I am developing AI to perform MDP, I am getting states(just integers in this case) and assigning it a value, and I am going to be doing this a lot. So I am looking for a data structure that can hold(no need for delete) that information and will have a very fast get/update function. Is there something faster than the regular dictionary? I am looking for anything really so native python, open sourced, I just need fast getting.

Upvotes: 0

Views: 2094

Answers (3)

ziggystar
ziggystar

Reputation: 28676

If you want a rapid prototype, use python. And don't worry about speed.

If you want to write fast scientific code (and you can't build on fast native libraries, like LAPACK for linear algebra stuff) write it in C, C++ (maybe only to call from Python). If fast instead of ultra-fast is enough, you can also use Java or Scala.

Upvotes: 1

Kyle Strand
Kyle Strand

Reputation: 16509

You're saying that all your keys are integers? In that case, it might be faster to use a list and just treat the list indices as the key values. However, you'd have to make sure that you never delete or add list items; just start with as many as you think you'll need, setting them all equal to None, as shown:

mylist = [None for i in xrange(totalitems)]

Then, when you need to "add" an item, just set the corresponding value.

Note that this probably won't actually gain you much in terms of actual efficiency, and it might be more confusing than just using a dictionary.

For 10,000 items, it turns out (on my machine, with my particular test case) that accessing each one and assigning it to a variable takes about 334.8 seconds with a list and 565 seconds with a dictionary.

Upvotes: 2

Andrew Clark
Andrew Clark

Reputation: 208665

Using a Python dictionary is the way to go.

Upvotes: 9

Related Questions