Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution that is about 4x faster using itertools.cycle #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

TomorrowToday
Copy link

This new script uses itertools.cycle() and a slightly different approach to stepping through and checking solutions at each time step. Shifting references around instead of calculating state at each time step for each position turns out to be a bit faster. In comparison to fast_mode.py, which runs consistently at ~5seconds on my system, this runs at ~1.2seconds.

Zipping through a list of itertools.cycle() objects is much faster for managing state of scanners than recalculating the scanner position each time. On my laptop I consistently saw times of 1.2 seconds for this script versus 5 seconds for fast_mode.py
itertools cycle ftw: a bit faster than fast_mode
Major refactor of the code to help keep concepts clear as I added in firewall optimizing. The 'offset scanner' approach means scanners of the same size can be merged together to a single scanner and small scanners that are exactly half, a third, a quarter, etc. the size of another scanner can be expanded and merged as well. With the example firewall, 43 scanners in the firewall are reduced to 5 using these techniques. From the previous build, this doubles performance. From fast_mode.py, this is ~11x faster and easily runs under one second.
@TomorrowToday
Copy link
Author

This'll be my last update. By optimizing the firewall, performance gain is now ~11x from fast_mode.py. This was a fun exercise. Thanks for the putting your code on github and sharing your talk on youtube. Cheers, Steven

@jiffyclub
Copy link
Owner

This is really interesting, thanks for sharing this back!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants