Press enter or click to view image in full size
I’ll take you through the process of optimizing a Sudoku solver written in Rust. We’ll start with a simple, unoptimized version and apply a series of optimizations that will take the time to solve 100,000 puzzles from over 5 minutes down to just 33 seconds, and 20,000 of the hardest puzzles from over 2 minutes down to just 17 seconds.
The Setup
The project is a command-line Sudoku solver written in Rust. The puzzles are read from text files in the src/puzzle_banks directory, thanks to the github project Sudoku Exchange Puzzle Bank. Each line in these files represents a single puzzle in the following format:
0000847b216e 020900000048000031000063020009407003003080200400105600030570000250000180000006050 2.3The second part of the line is the puzzle itself, where 0 represents an empty cell.
The basic flow of the program is:
- Read the puzzle file.
- For each line, parse the puzzle string into a Board struct.
- Call the solve function on the board.
- Print the statistics.
.png)
