示例代码:数独

sodoku.go:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Package godoku is a simple, brute-force,
// in-place sudoku solver
package godoku

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"strconv"
	"strings"
)

type Sudoku struct {
	board         Board
	solved        bool
	solutionCount int
	doPrint       bool
	dim           int
	solveAll      bool
	solution      Board
}

type Board [][]int

func (s *Sudoku) PrintBoard() {
	for _, row := range s.board {
		fmt.Println(row)
	}
}

// IsValidBoard iterates through all initial 
// values on the board and verifies that they indeed
// abide by the 3 laws of Sudoku
func (s *Sudoku) IsValidBoard() bool {
	if s.board == nil {
		return false
	}
	for i, row := range s.board {
		for j, val := range row {
			if val == 0 {
				continue
			}
			s.board[i][j] = 0
			if !s.ValidValueAtPosition(i, j, val) {
				s.board[i][j] = val
				return false
			}
			s.board[i][j] = val
		}
	}

	return true
}

// String returns either the unsolved board if the
// sudoku has not been solved, or the solution 
// if such a solution has been found
// by running one of the Solve* methods.
func (s *Sudoku) String() string {
	var buffer bytes.Buffer
	if s.solved {
		for _, row := range s.solution {
			buffer.WriteString(fmt.Sprintf("%v\n", row))
		}
		return buffer.String()
	}

	for _, row := range s.board {
		buffer.WriteString(fmt.Sprintf("%v\n", row))
	}
	return buffer.String()
}

// GetSolution returns the solution
// BUG(paddie): doesn't check if board is solved
func (s *Sudoku) GetSolution() Board {
	return s.solution
}

// Load a sudoku from a path and a dimension argument
func NewSudokuFromFile(path string, dim int) (*Sudoku, error) {
	s := new(Sudoku)
	var err error
	s.board, err = readBoardFromFile(path, dim)

	if err != nil {
		return nil, err
	}
	s.dim = dim

	return s, nil
}

// Loads a sudoku-board in a string-representation;
// The values are in a 9x9 matrix, using space " " as delimiters and '\n' as linebreaks
func NewSudokuFromString(path string, dim int) (*Sudoku, error) {
	s := new(Sudoku)
	var err error
	s.board, err = readBoardFromString(path, dim)

	if err != nil {
		return nil, err
	}
	s.dim = dim

	return s, nil
}

/* 
Returns the number of solutions found.
*/
func (s *Sudoku) GetSolutionsCount() int {
	return s.solutionCount
}

// registers the first solutions in the s.solution
// board, and prints if doPrint is set.
func (s *Sudoku) registerSolution() {
	s.solutionCount++
	if s.doPrint {
		s.PrintBoard()
	}

	if s.solved {
		return
	}

	s.solved = true
	s.solution = make(Board, 9, 9)

	for i, row := range s.board {
		s.solution[i] = make([]int, 9, 9)
		copy(s.solution[i], row)
	}
}

// Check if the solver has found a solution
func (s *Sudoku) IsSolved() bool {
	return s.solved
}

// The dimensions of the sudoku board
func (s *Sudoku) Dimension() int {
	return s.dim
}

// Solve and save the solution. Returns an error if no Sudoku has been loaded
func (s *Sudoku) Solve() error {

	s.solved = false

	if s.board == nil {
		return fmt.Errorf("No Board has been loaded..")
	}

	s.solveAll = false
	s.bruteforcePosition(0, 0)

	return nil
}

// Same as Solve(), but this one also prints
// the solution to stdin
func (s *Sudoku) SolveAndPrint() error {
	s.doPrint = true

	err := s.Solve()

	s.doPrint = false

	return err
}

// Same as Solve, but keeps running until it has all
// the solutions and keeps a count. It only saves the first solution
func (s *Sudoku) SolveAll() error {

	s.solved = false

	if s.board == nil {
		return fmt.Errorf("No Board has been loaded..")
	}

	s.solveAll = true

	s.bruteforcePosition(0, 0)

	return nil
}

// Same as SolveAll but prints all the solutions
// to stdin
func (s *Sudoku) SolveAllAndPrint() error {
	s.doPrint = true

	err := s.SolveAll()

	s.doPrint = false

	return err
}

func (s *Sudoku) bruteforcePosition(row, col int) {
	// we use '0' to indicate a non-filled block
	if s.board[row][col] == 0 {
		for i := 1; i < 10; i++ {
			if s.ValidValueAtPosition(row, col, i) {
				// place the value and attempt to solve
				s.board[row][col] = i
				// attempt to solve the sudoku with placed value
				s.nextPosition(row, col)

				if s.solved && !s.solveAll {
					// if Solve() was used, we break
					// after first solution
					s.board[row][col] = 0
					return
				}
				// clean up after attempt
				s.board[row][col] = 0
			}
		}
	} else {
		s.nextPosition(row, col)
	}
}

// Does two things:
//
// 1) if the board is in a finished state, calls 
// registerSolution() and returns; 
// enables bruteforcePostion to exhaust every remaining permutation
// 
// 2) checks wether to move to next column or next row
func (s *Sudoku) nextPosition(row, col int) {
	// we run through the Board row by row
	// meaning we only change rows when we're in
	// the final column
	if col < 8 {
		s.bruteforcePosition(row, col+1)
	} else {
		// if we're in the final collumn in the final 
		// row; we have a solution
		// - else we iterate to next row and reset the collumn
		if row < 8 {
			s.bruteforcePosition(row+1, 0)
		} else {
			s.registerSolution()
		}
	}
}

// Verify that _val_ can be legally placed at (row,col)
// given restrictions in column, row and 3x3 square
func (s *Sudoku) ValidValueAtPosition(row, col, val int) bool {
	if s.ValidInSquare(row, col, val) &&
		s.ValidInColumnAndRow(row, col, val) {
		// validInRow(row, val, Board) {
		return true
	}

	return false
}

// Checks that the _val_ does not already occur in the
// active 3x3 square
func (s *Sudoku) ValidInSquare(row, col, val int) bool {
	row, col = int(row/3)*3, int(col/3)*3

	for i := row; i < row+3; i++ {
		for j := col; j < col+3; j++ {
			//fmt.Printf("row, col = %v, %v\n", i, j)
			if s.board[i][j] == val {
				return false
			}
		}
	}
	return true
}

// Checks if _val_ already occurs in either the row or the column.
func (s *Sudoku) ValidInColumnAndRow(row, col, val int) bool {
	for i := 0; i < 9; i++ {
		if s.board[row][i] == val ||
			s.board[i][col] == val {
			return false
		}
	}
	return true
}

func readBoardFromFile(path string, dim int) (Board, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	return readBoardFromString(string(content), dim)
}

func readBoardFromString(m string, dim int) (Board, error) {
	lines := strings.Split(m, "\n")

	if len(lines) < dim {
		return nil, fmt.Errorf("row count of input: %v does not match dim: %v", len(lines), dim)
	}

	Board := make(Board, dim, dim)

	for i := 0; i < dim; i++ {
		stringRows := strings.Split(lines[i], " ")

		if len(stringRows) < dim {
			return nil, fmt.Errorf("column count of input: %v does not match dim: %v", len(lines[i]), dim)
		}

		integerRow := make([]int, dim, dim)
		for j := 0; j < dim; j++ {
			str := stringRows[j]
			val, err := strconv.Atoi(str)
			if err != nil {
				return nil, err
			}
			integerRow[j] = val
		}
		Board[i] = integerRow
	}
	return Board, nil
}

soduku_test.go

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package godoku

import (
	// "fmt"
	"testing"
)

const solvable88 string = `0 7 0 0 0 0 0 8 0
0 3 0 7 6 2 0 0 1
0 0 1 9 8 0 0 0 0
1 0 0 0 0 0 0 0 0
8 0 3 0 0 0 0 0 2
0 0 6 0 0 0 0 0 8
0 0 0 0 3 1 6 0 0
5 0 0 2 4 9 0 1 0
0 1 0 0 0 0 0 9 0`

const badRow string = `0 7 0 0 0 0 0 8 0
0 3 0 7 6 2 0 0 1
0 0 1 9 8 0 0 0 0
1 0 0 0 0 0 0 0
8 0 3 0 0 0 0 0 2
0 0 6 0 0 0 0 0 8
0 0 0 0 3 1 6 0 0
5 0 0 2 4 9 0 1 0
0 1 0 0 0 0 0 9 0`

const badCol string = `0 7 0 0 0 0 0 8 0
0 3 0 7 6 2 0 0 1
0 0 1 9 8 0 0 0 0
1 0 0 0 0 0 0 0 0
8 0 3 0 0 0 0 0 2
0 0 6 0 0 0 0 0 8
0 0 0 0 3 1 6 0 0
5 0 0 2 4 9 0 1 0`

const hard string = `0 0 0 0 0 0 0 0 0
0 0 1 0 0 3 0 0 0
8 0 0 0 0 0 0 0 6
0 0 0 2 0 9 0 1 0
6 4 0 0 0 0 9 0 0
0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 7 3 0
7 0 0 6 4 0 0 0 0
0 0 0 8 0 0 0 0 0`

const invalidBoard string = `8 0 0 0 0 0 0 0 0
0 0 1 0 0 3 0 0 0
8 0 0 0 0 0 0 0 6
0 0 0 2 0 9 0 1 0
6 4 0 0 0 0 9 0 0
0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 7 3 0
7 0 0 6 4 0 0 0 0
0 0 0 8 0 0 0 0 0`

const badFormatting = `0 7 0 0 0 0 0 8 0
0 3 0 7 6 2 0 0 1    
0 0 1 9 8 0 0 0 0 6 
1 0 0 0 0 0 0 0 0 
8 0 3 0 0 0 0 0 2 7
0 0 6 0 0 0 0 0 8
0 0 0 0 3 1 6 0 0
5 0 0 2 4 9 0 1 0
0 1 0 0 0 0 0 9 0
`

const noSolution string = `7 0 8 0 0 0 7 0 0
0 0 0 0 6 0 0 5 0
0 0 0 9 0 0 0 2 4
1 6 7 0 0 8 0 0 5
0 0 0 6 3 0 9 0 0
9 3 0 7 1 4 2 0 0
8 0 0 1 5 2 4 6 3
5 0 6 0 4 9 8 1 7
3 1 4 8 7 0 5 9 2`

// solution output
const solution string = `[2 7 9 1 5 3 4 8 6]
[4 3 8 7 6 2 9 5 1]
[6 5 1 9 8 4 3 2 7]
[1 4 5 3 2 8 7 6 9]
[8 9 3 6 1 7 5 4 2]
[7 2 6 4 9 5 1 3 8]
[9 8 2 5 3 1 6 7 4]
[5 6 7 2 4 9 8 1 3]
[3 1 4 8 7 6 2 9 5]
`

// solve only one of the 88 solutions
func TestSolve1(t *testing.T) {
	s, err := NewSudokuFromString(solvable88, 9)
	if err != nil {
		t.Error(err)
	}
	s.Solve()

	if s.GetSolutionsCount() != 1 {
		t.Errorf("Expected 1 != Actual %v", s.GetSolutionsCount())
	}
	if s.String() != solution {
		t.Errorf("Expected\n%v != Actual \n%v", solution, s)
	}
}

// make sure that the solver gets all 88 solutions
func TestSolve88(t *testing.T) {
	s, err := NewSudokuFromString(solvable88, 9)
	if err != nil {
		t.Error(err)
	}
	s.SolveAll()

	if s.GetSolutionsCount() != 88 {
		t.Errorf("Expected 88 != Actual %v", s.GetSolutionsCount())
	}
	// should be the first solution and not the 88th solution
	// - same as testsolve01
	if s.String() != solution {
		t.Errorf("Expected\n%v != Actual \n%v", solution, s)
	}
}

// test that the package is resillient against trailing spaces etc.
func TestBadFormatting(t *testing.T) {
	s, err := NewSudokuFromString(badFormatting, 9)
	if err != nil {
		t.Error(err)
	}
	s.Solve()

	if s.GetSolutionsCount() != 1 {
		t.Errorf("Expected 1 != Actual %v", s.GetSolutionsCount())
	}
}

// test that provided sudoku has at least Dim x Dim dimension
func TestBadDims(t *testing.T) {
	_, err := NewSudokuFromString(badRow, 9)
	if err == nil {
		t.Error(err)
	}
	_, err = NewSudokuFromString(badCol, 9)
	if err == nil {
		t.Error(err)
	}
}

// test that it reports correctly if the sudoky does 
// not have any solutions
func TestFail(t *testing.T) {
	// load sudoku
	s, err := NewSudokuFromString(noSolution, 9)
	if err != nil {
		t.Error(err)
	}
	s.Solve()

	if s.IsSolved() != false {
		t.Errorf("Expected: 'false' != Actual: %v", s.IsSolved())
	}
}

// ones the board has been read, validate it for 
// erroneously positioned starting values
func TestInvalidBoard(t *testing.T) {
	// test that invalid board is invalid
	s, err := NewSudokuFromString(invalidBoard, 9)
	if err != nil {
		t.Error(err)
	}
	if s.IsValidBoard() {
		t.Errorf("Expected: 'false' != Actual: %v", s.IsValidBoard())
	}
	// test that valid board is not invalid
	s, err = NewSudokuFromString(solvable88, 9)
	if err != nil {
		t.Error(err)
	}
	if !s.IsValidBoard() {
		t.Errorf("Expected: 'true' != Actual: %v", s.IsValidBoard())
	}
}

// bench the solving of the hardest sudoku
func BenchmarkSolveHard(b *testing.B) {
	b.StopTimer()
	s, err := NewSudokuFromString(hard, 9)
	if err != nil {
		b.Error(err)
	}
	b.StartTimer()

	s.Solve()
}

// bench how long it takes to get a "no solution" answer
func BenchmarkSolveFail(b *testing.B) {
	b.StopTimer()
	s, err := NewSudokuFromString(noSolution, 9)
	if err != nil {
		b.Error(err)
	}
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		// s.Reset()
		s.Solve()
	}
}

main.go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main

import (
	"fmt"
	"github.com/paddie/godoku"
)

const solvable88 string = `0 7 0 0 0 0 0 8 0
0 3 0 7 6 2 0 0 1
0 0 1 9 8 0 0 0 0
1 0 0 0 0 0 0 0 0
8 0 3 0 0 0 0 0 2
0 0 6 0 0 0 0 0 8
0 0 0 0 3 1 6 0 0
5 0 0 2 4 9 0 1 0
0 1 0 0 0 0 0 9 0`

func main() {
	s, err := godoku.NewSudokuFromString(solvable88, 9)
	if err != nil {
		fmt.Println(err)
		return
	}

	// check that board is valid
	if !s.IsValidBoard() {
		fmt.Println("Invalid board")
		return
	}

	fmt.Println(s)

	// solve the board
	s.Solve()
	
	// print the solution
	fmt.Println(s)

	// checks the number of solutions; 1 in this case
	if s.GetSolutionsCount() != 1 {
		fmt.Printf("Expected 1 != Actual %v\n", s.GetSolutionsCount())
		return
	}

	fmt.Println("This sudoku has one solution!\n")
}

优化

Godoku 是一个 Go 编写的暴力破解数独的程序,逻辑比较简单,从上到下从左到右扫描每一个空格,从 1 到 9 开始填写数字,一旦数字无效(行冲突,列冲突或者 9 宫格冲突),那么就换一个数字,如果所有数字都换了还无效,那么就退回上一个格子,继续这个过程。

Step1

程序自带了测试和 Benchmark,所以我们先来生成一个 Profiling 文件,看看哪个地方开销最大。

1
2
$ go tool pprof godoku.test cpu.prof
$ go tool pprof -http=":8081" godoku.test cpu.prof

很明显,ValidInSquare这个函数开销很大,这个函数是检测一个数字在九宫格里面存不存在,作者的实现如下。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
func (s *Sudoku) ValidInSquare(row, col, val int) bool {
	row, col = int(row/3)*3, int(col/3)*3

	for i := row; i < row+3; i++ {
		for j := col; j < col+3; j++ {
			//fmt.Printf("row, col = %v, %v\n", i, j)
			if s.board[i][j] == val {
				return false
			}
		}
	}
	return true
}

循环判断有没有这个数,逻辑很简单,但是 Profiling 告诉我们,这里成了性能瓶颈,每一次测试数字都要调用这个方法,而这个方法内部是一个循环,调用如此频繁的方法采用循环肯定是不行的。

Step2

这里我们采用经典的 空间换时间 思路,使用另外一个结构存储九宫格内的状态信息,使得查询一个数字在九宫格内有没有可以通过简单的数组访问得到。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
s.regionInfo = make([]int, s.dim * s.dim / 9)

func (s *Sudoku) updateRegion(row, col, val, delta int) {
	region := (row/3)*3 + col/3
	key := region*9 + val - 1
	s.regionInfo[key] += delta
}

func (s *Sudoku) checkRegion(row, col, val int) bool {
	region := (row/3)*3 + col/3
	key := region*9 + val - 1
	return s.regionInfo[key] == 1
}

我们使用一个额外的 regionInfo slice 来存储九宫格里的情况,每一次设置数独中格子的值时,我们更新一下 regionInfo 的信息。当要检查某个数在某个九宫格中是否已经存在时,直接查询 regionInfo 即可。

1
2
3
func (s *Sudoku) ValidInSquare(row, col, val int) bool {
	return !s.checkRegion(row, col, val)
}

再运行一次测试,看看性能改善了多少。

很好!CPU 开销已经由 9770ms 降低到了 5460ms,性能提高 79%。现在程序的性能瓶颈已经是 ValidInColumnAndRow 这个函数了。

Step3

作者 ValidInColumnAndRow 函数的实现仍然是直观简单的循环。

1
2
3
4
5
6
7
8
9
func (s *Sudoku) ValidInColumnAndRow(row, col, val int) bool {
	for i := 0; i < 9; i++ {
		if s.board[row][i] == val ||
			s.board[i][col] == val {
			return false
		}
	}
	return true
}

我们使用同样的策略来优化 ValidInColumnAndRow 这个函数,使用额外的数据结构存储每一行和每一列的数字状态信息。这样查询时可以马上返回,而不需要做任何循环比较。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func (s *Sudoku) updateRowAndCol(row, col, val, delta int) {
	rowKey := row*9 + val - 1
	colKey := col*9 + val - 1
	s.rowInfo[rowKey] += delta
	s.colInfo[colKey] += delta
}

func (s *Sudoku) checkRowOrCol(row, col, val int) bool {
	rowKey := row*9 + val - 1
	colKey := col*9 + val - 1
	return s.rowInfo[rowKey] == 1 || s.colInfo[colKey] == 1
}
func (s *Sudoku) ValidInColumnAndRow(row, col, val int) bool {
	return !s.checkRowOrCol(row, col, val)
}

我们再来看看 Profiling 数据。

性能再次得到了提升,由 5460ms 降低到了 3610ms。初步看来,已经没有了明显可以优化的地方了。到此为止,我们的程序性能已经得到了 170% 的提升!我们并没有怎么努力,只不过是生成了 Profiling 文件,一眼看出问题在哪儿,然后针对性地优化而已。

感谢 Golang 提供了这套超赞的 pprof 工具,性能调优变得如此轻松和愉悦。这里我所举的只是 pprof 功能的冰山一角,pprof 的强大功能远不止这些。比如可以使用 list 指令查看函数的源码中每一行代码的开销以及使用 weblist 指令查看函数汇编以后每一句汇编指令的开销等等。不仅是 CPU Profiling,pprof 同样支持 Memory Profiling,可以帮助你检查程序中内存的分配情况。总之,在 pprof 的帮助下,程序的开销信息变得一清二楚,优化自然变得轻而易举。

转载:https://cjting.me/2016/11/14/use-pprof-to-optimize-go/