Skip to content

Conversation

@jbrockmendel
Copy link
Contributor

At least we can get something out of the adventure with asv. We can be fairly confident this is real because we saw cProfile results.

return name.lower() in self._jump

def weekday(self, name):
if len(name) >= min(len(n) for n in self._weekdays.keys()):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was recently added here, replacing a len(name) >= 3 check that was part of the original commit in this repo.

I think it was just there for performance reasons.

@pganssle
Copy link
Member

This is an unambigous win over the current code.

To see what the best thing to do is, I made two versions, parserinfo does the min call in the constructor and stores it as a private attribute, and parserinfo2 is the code as presented here. and parserinfo3 is the code as is now:

>>> i = parserinfo()
>>> i2 = parserinfo2()
>>> i3 = parserinfo3()

>>> %timeit i.weekday('Wed')
1000000 loops, best of 3: 627 ns per loop

>>> %timeit i2.weekday('Wed')
1000000 loops, best of 3: 393 ns per loop

>>> %timeit i3.weekday('Wed')
100000 loops, best of 3: 3.4 µs per loop

>>> %timeit i.weekday('A')
1000000 loops, best of 3: 285 ns per loop

>>> %timeit i2.weekday('A')
1000000 loops, best of 3: 724 ns per loop

>>> %timeit i3.weekday('A')
100000 loops, best of 3: 2.83 µs per loop

When the string passed to weekday has length >= the shortest string in in the weekday keys, this version is best. When it's <= the min, the cached version is best. The version we have now performs worst in all cases.

I'm not sure all the code paths for the common case, but I'm guessing that it's actually kinda rare to hit the case where weekday or month are passed 2-character strings, so this approach seems the best one to me.

@pganssle pganssle added this to the 2.7.0 milestone Dec 11, 2017
@pganssle pganssle merged commit 646a2a5 into dateutil:master Dec 11, 2017
@jbrockmendel
Copy link
Contributor Author

Yah, once we get to the point of trying to out-perform a dict lookup, it's time to go read a book.

@jbrockmendel jbrockmendel deleted the minperf branch December 11, 2017 22:59
@pganssle
Copy link
Member

I suppose we could have done self._weekdays.get(name.lower(), None) for weekdays.

@pganssle pganssle mentioned this pull request Mar 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants