bpo-33228: Use Random.choices in tempfile#6383
Conversation
Using Random.choices instead of Random.choice in the tempfile._RandomNameSequence class makes the code more readable and faster. This commit also updates the docstring of the class.
| c = self.characters | ||
| choose = self.rng.choice | ||
| letters = [choose(c) for dummy in range(8)] | ||
| letters = self.rng.choices(self.characters, k=8) |
There was a problem hiding this comment.
I don't think these kinds of trivial changes should be done. There should be a preference for code stability.
|
How much "faster" does it make the code? Do you have a use case for which you need to optimize tempfile? |
|
Well, _RandomNameSequence would get a bit more than 2x faster, but, of course, in the context of tempfile almost all public functions will be IO limited anyway, and the performance gain in this one class will be completely irrelevant. |
|
@methane, I split out the docstring change into a separate PR #6414 like you asked for. @rhettinger @1st1 if the remainder of this PR is not worth it, it can be closed without any loss now. |
Using Random.choices instead of Random.choice in
the tempfile._RandomNameSequence class makes the code more
readable and faster.
This commit also updates the docstring of the class.
https://bugs.python.org/issue33228