我在 Go 中解决了一个难题,它通过旋转 ASCII 字节值以匹配行方式(左->右)或列方式(顶部->底部)的字符串,在 2D 字节数组中找到一个字符串。我能够按顺序解决它,当它同时解决它时,我尝试启动一个 go-routine 来处理特定的输入组合,看看是否有任何可能的 25 个旋转可以找到匹配项。代码摘要如下
该FindByConcurrentRot方法采用 2D 字符数组输入并尝试在各种可能的输入组合中找到字符串的匹配项。
问题是 - 下面使用的并发方法是否有效?如何改进?将顺序例程“按原样”转换为并发程序的方法是否错误?即是否应该重写整个程序以充分利用并发特性?
// searchResult defines the information needed to pass to identify if a match has been identified during concurrent search
type searchResult struct {
row int
col int
rot int
found bool
}
// processSearchResults runs the gorotuine to perform the search for a particular rotation of input
func processSearchResults(wg *sync.WaitGroup, iter int, resultChan chan searchResult, table [][]byte, word string) {
// call goroutine end when the function returns
defer wg.Done()
if iter >= 1 {
rotate(table, iter)
}
x, y, match := present(table, word)
if match {
resultChan <- searchResult{row: x, col: y, rot: iter, found: true}
return
}
resultChan <- searchResult{found: false}
}
// doCopy creates a copy of the original table to passed for each iteration of the concurrent search
// This is an EXPENSIVE operation on a goroutine, because of memory copy operations
// The copy is needed for the goroutines to have their own control of data and not run into data
// races by passing the original data to each of them
func doCopy(table [][]byte) [][]byte {
copyTable := make([][]byte, len(table))
for i := range table {
copyTable[i] = make([]byte, len(table[i]))
copy(copyTable[i], table[i])
}
return copyTable
}
此 Go 操场链接上的完整 MVCE - https://go.dev/play/p/7YFAsAlFRUw
慕码人2483693
相关分类