Hi, I have this program (https://play.golang.org/p/qHPzjj2uj3) that take a decent amount of time for computing. Let’s open pprof and see what it spent its time on. Type: cpu Time: Nov 15, 2017 at 5:48pm (CET) Duration: 9.92s, Total samples = 9.78s (98.54%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) list main.main Total: 9.78s ROUTINE ======================== main.main in c:\Users\lemoussel\go\src\C-Libraries\main.go 80ms 9.78s (flat, cum) 100% of Total . . 170: bufferHexHash, err := HexToUint32(hash, binary.BigEndian) . . 171: /*for i, val := range bufferHexHash { . . 172: fmt.Printf("bufferHexHash[%d]: %08x \n", i, val) . . 173: }*/ . . 174: startTime = time.Now() 20ms 20ms 175: for i := 0; i < 10000000; i++ { 60ms 9.76s 176: result = findHex(bufferHexSearch, bufferHexHash) . . 177: } . . 178: fmt.Printf("findHex() Find: %t Time : %.2f sec.\n", result, time.Now().Sub(startTime).Seconds()) . . 179:}
Ok, so it seems we spent most of the time in findHex, makes sense! (pprof) list main.findHex Total: 9.78s ROUTINE ======================== main.findHex in c:\Users\lemoussel\go\src\C-Libraries\main.go 9.52s 9.52s (flat, cum) 97.34% of Total . . 72: {0x00000fff, 0xfffffff0, 0x0}, // 5 . . 73: {0x000000ff, 0xffffffff, 0x0}, // 6 . . 74: {0x0000000f, 0xffffffff, 0xf0000000}, // 7 . . 75:} . . 76: 20ms 20ms 77:func findHex(bufferHexSearch [8][3]uint32, bufferHexHash []uint32) bool { . . 78: var bFind = false . . 79: var i, j, k int . . 80: var lenBufferHexHash = len(bufferHexHash) . . 81: var lenBufferHexSearch = len(bufferHexSearch) . . 82: var lenMaskHex = len(maskHex) . . 83:LoopSearch: 300ms 300ms 84: for i = 0; i < lenBufferHexHash; i++ { 410ms 410ms 85: for j = 0; j < lenBufferHexSearch; j++ { 2.53s 2.53s 86: for k = 0; k < lenMaskHex; k++ { 6.16s 6.16s 87: if bufferHexSearch[j][0] == bufferHexHash[i]&maskHex[k][0] && 100ms 100ms 88: bufferHexSearch[j][1] == bufferHexHash[i+1]&maskHex[k][1] { . . 89: bFind = true . . 90: break LoopSearch . . 91: } . . 92: } . . 93: } Oh ! most of the time is in the line if bufferHexSearch[j][0] == bufferHexHash[i]&maskHex[k][0] && I don't understand why? Should I use goroutine to compute concurrently? I'm interested in any recommendations to optimize this program. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.