Bill wrote:Yeah -- replace the try/except block with a simple if. I replaced the try/except block with this:WaynaPicchu wrote:Still slower than others have reported. Any other optimization tips?
- Code: Select all
v = A.get(k)
if v:
cnt += A[k]
break
The modified program runs in about 5.7 seconds.
Oohhhhhh: "get(key[, default]) Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError."
Didn't know about the get method. That last part is essential. It solves that nasty KeyError problem I had to get around with the try/except block.
Ran in 9 seconds with that modification. Thanks.
