I had done that, it consumes 26%. That's why I asked if there is a better
way to implement it.
Total: 430ms 800ms (flat, cum) 26.40%
151 . 10ms func searchUID(r *bufio.Reader, uid [16]byte) (n int, Size int32, Type int32, err error) {
152 . . var seq [16]byte
153 . .
154 . . n = 0
155 280ms 410ms for seq != uid {
156 50ms 100ms copy(seq[:15], seq[1:])
157 30ms 190ms seq[15], err = r.ReadByte()
158 50ms 50ms if err != nil {
159 . . fmt.Println("Error during ReadByte (UID Search)", err)
160 . . return n, 0, 0, err
161 . . }
162 . .
163 . . // fmt.Printf("%x, %x, %v \n", uid, seq, n)
164 . . if n > 4096 {
165 . . fmt.Printf("UID not found, Err :Out of search range \n")
166 . . return n, 0, 0, errors.New("UID not found, Out of search range")
167 . . }
168 . . n = n + 1
169 . . }
170 . .
171 . . // fmt.Printf("Search end successfully in %d iterations! (%x vs %x)\n", n, uid, seq)
172 . . // Read the other parts of the header
173 . 10ms err = binary.Read(r, binary.LittleEndian, &Size)
174 20ms 20ms err = binary.Read(r, binary.LittleEndian, &Type)
175 . .
176 . . return n, Size, Type, nil
177 . 10ms }
On Thursday, 9 July 2015 18:48:34 UTC+2, Konstantin Shaposhnikov wrote:I would suggest to profile the app to identify the slow code path first.
Go makes profiling really easy, have a look at
http://blog.golang.org/profiling-go-programsOn Thursday, 9 July 2015 23:37:24 UTC+8, Marcel Farrés Franch wrote:
I am reading a TCP byte streaming connection and to start the decoding I
need to find the ID in the stream.
I solve the issue with the next function, but the performance is not very
good.
func searchUID(r *bufio.Reader, uid [16]byte) (n int, Size int32, Type
int32, err error) {
var seq [16]byte
n = 0
for seq != uid {
copy(seq[:15], seq[1:])
seq[15], err = r.ReadByte()
if err != nil {
fmt.Println("Error during ReadByte (UID Search)", err)
return n, 0, 0, err
}
// fmt.Printf("%x, %x, %v \n", uid, seq, n) // debug
if n > 4096 {
// fmt.Printf("UID not found, Err :Out of search range \n")
return n, 0, 0, errors.New("UID not found, Out of search range")
}
n = n + 1
}
// Read the other parts of the header
err = binary.Read(r, binary.LittleEndian, &Size)
err = binary.Read(r, binary.LittleEndian, &Type)
return n, Size, Type, nil
}
Is there a better way to solve the problem?
--
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+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.