Forums

Object size / performance

Hello again wizards!

Having kicked off my chatbot (It works! Happy dance!), I've turned my attention to the automatic sorting hat as a web app.

I have 9 different developers with 9 different sets of locations and qualifications. Instead of building basic rules with a few exceptions for the quirky ones, I'm building all of the tedious rules in for every developer, so even the quirkiest one is just another cog in the wheel for processing. I wish there was a better way to process things than a nested if sequence, but that's probably the best I have.

I've opted to create a Developer class and write each individual is an instance of that class. It seems like this should save time, because the processor will only be accessing a piece of each object at a time. My concern is that the object now has 20+ variable assignments and most of those assignments are dictionaries. When it comes time to tie everything together, I don't want my code to slow down from having to push all of those instances around.

Is object-creation the most efficient way to do this, and will it slow down my code?

Slow down your code in comparison to what? Object creation is not a particularly expensive process. If you're really concerned do some experiments (the python timeit module is useful for this) and see whether there's a big enough difference to matter.

Just in general. My first attempt had been a loop through strings (slow, I know) that slowed my process down to several seconds. I just don't want to get neck-deep in building a process that will have the same problem.

Looping through strings can be slow. Object creation probably isn't. Write your code, then work out if it needs to be optimised. Trying to optimise for problems that only exist in your head will mean that you optimise the wrong things and end up with unreadable code. The golden rule for optimisation is "Make it work then make it fast"

Cool.

One optimization I'd like to pre-empt, I'm searching for a text match in a list of zip codes using if zip_code in developer.restricted_zip_codes[location] . The list of restricted zip codes is hundreds long, and of course I may have to go through multiple lists.

Is this the most efficient way to do that kind of search?

Sets have fast membership tests, so if you use a set for the list of restricted zip codes instead of a list, you'll probably see a pretty large performance improvement.