Micro optimization of grid.select_random_empty_cell#3214
Merged
Conversation
|
Performance benchmarks:
|
This comment was marked as outdated.
This comment was marked as outdated.
Member
Author
|
If I use timeit with Schelling, on a 50X50 grid, a fixed seed, and a density of 0.9, the results are for just 3.4.2: 3.12 μs ± 64 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) |
EwoutH
approved these changes
Jan 27, 2026
Member
EwoutH
left a comment
There was a problem hiding this comment.
Looks clean, and I like the green!
Member
Author
|
It was a no-brainer that I came across while microbenchmarking stuff for mesa-rust. |
Member
|
20% in Schelling is quite a lot |
Member
Author
|
Large schelling is in essence just a test for how fast |
|
Performance benchmarks:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a micro optimization of
grid.select_random_empty_cell. Currently, this calls cellcollection.select_random_cell, which in turn doesrandom.choice(cells). This indirection can be avoided by just having a_celllistinsidegridand usingrandom.choicedirectly on it. Then, to top it off, we assignself.randomandself._celllistto local variables in the method to further improve performance. This trick avoids the Python calling overhead of attribute lookup. Since this for loop runs 50 times in the worst case, this micro stuff adds up a lot.