初级会员
  • 第 6927 位会员
  • feifei435
  • fei435@qq.com
  • 2016-12-15 07:12:55
  • Offline
  • 19 90

最近发布的主题

    暂无

最近发布的文章

    暂无

最近分享的资源

    暂无

最近发布的项目

    暂无

最近的评论

  • ```bash way= 0 , len(s)= 820000 time of way(0)=1.836864086s way= 1 , len(s)= 820000 time of way(1)=766.44119ms way= 2 , len(s)= 820000 time of way(2)=1.017533572s way= 3 , len(s)= 820000 time of way(3)=3.305793765s way= 4 , len(s)= 820000 time of way(4)=777.359µs way= 5 , len(s)= 820000 time of way(5)=1.89599892s way= 6 , len(s)= 820000 time of way(6)=1.888661772s way 0=1.836864086s is 1.0 times of way 6 way 1=766.44119ms is 0.4 times of way 6 way 2=1.017533572s is 0.5 times of way 6 way 3=3.305793765s is 1.8 times of way 6 way 4=777.359µs is 0.0 times of way 6 way 5=1.89599892s is 1.0 times of way 6 way 6=1.888661772s is 1.0 times of way 6 ```
  • 基于上一位大佬的评论的完整代码 ```go package main import ( "bytes" "fmt" "strings" "time" ) func benchmarkStringFunction(n int, index int) (d time.Duration) { v := "ni shuo wo shi bu shi tai wu liao le a?" var s string var buf bytes.Buffer var t = []byte{} t0 := time.Now() for i := 0; i < n; i++ { switch index { case 0: // fmt.Sprintf s = fmt.Sprintf("%s[%s]", s, v) case 5: s = fmt.Sprintf("%s[%v]", s, v) case 6: s = fmt.Sprintf("%v[%v]", s, v) case 1: // string + s = s + "[" + v + "]" case 2: // strings.Join s = strings.Join([]string{s, "[", v, "]"}, "") case 3: // temporary bytes.Buffer b := bytes.Buffer{} b.Write(t) b.WriteString("[") b.WriteString(v) b.WriteString("]") //s = b.String() t = b.Bytes() case 4: // stable bytes.Buffer buf.WriteString("[") buf.WriteString(v) buf.WriteString("]") } if i == n-1 { if index == 4 { // for stable bytes.Buffer s = buf.String() } if index == 3 { s = string(t) } fmt.Println("way=", index, ", len(s)=", len(s)) // consume s to avoid compiler optimization } } t1 := time.Now() d = t1.Sub(t0) fmt.Printf("time of way(%d)=%v\n", index, d) return d } func main() { k := 7 d := [7]time.Duration{} for i := 0; i < k; i++ { d[i] = benchmarkStringFunction(20000, i) } for i := 0; i < k; i++ { fmt.Printf("way %d=%v is %6.1f times of way %d\n", i, d[i], float32(d[i])/float32(d[k-1]), k-1) } } ```
  • 这是因为你在AddConcurrentHandlers()中添加的handler没有主动调用msg.Finish()造成的,一个handler接收玩消息后应该及时调用Finish()否则别的goroutine取不到下一条消息,所以就阻塞了,跟nsq的设计理念有关.