go 迭代string數組操作 go for string[]

go 迭代string數組,直接拷貝去用即可

package main
import (
"fmt"
)
func main() {
  subsCodes := []string{"aaaa", "vvvvv", "dddd", "eeeee", "gfgggg"}
  for _, s := range subsCodes {
    fmt.Println(s)
  }
}

補充:golang字符串string與字符數組[]byte高效轉換

string與[]byte的直接轉換是通過底層數據copy實現的

var a = []byte(“hello boy”)

var b = string(a)

這種操作在並發量達到十萬百萬級別的時候會拖慢程序的處理速度

通過gdb調試來看一下string和[]byte的數據結構

(gdb) l main.main
import (
  "fmt"
)
func main() {
  s := "hello, world!"
  b := []byte(s)
  fmt.Println(s, b)
(gdb) b 11
Breakpoint 1 at 0x487cd9: file /export/home/machao/src/test/strbytes.go, line 11.
(gdb) r
Starting program: /export/home/machao/src/test/test1 
Breakpoint 1, main.main () at /export/home/machao/src/test/strbytes.go:11
  fmt.Println(s, b)
(gdb) info locals
s = {
str = 0x4b8ccf "hello, world!level 3 resetload64 failednil stackbaseout of memorys.allocCount=srmount errorstill in listtimer expiredtriggerRatio=unreachable: value method xadd64 failedxchg64 failed nmidlelocked= on "..., len = 13}
b = {array = 0xc4200140e0 "hello, world!", len = 13, cap = 16}
(gdb) ptype s
type = struct string {
 uint8 *str;
 int len;
}
(gdb) ptype b
type = struct []uint8 {
 uint8 *array;
 int len;
 int cap;
}

轉換後 [ ]byte 底層數組與原 string 內部指針並不相同,以此可確定數據被復制。那麼,如不修改數據,僅轉換類型,是否可避開復制,從而提升性能?

從 ptype 輸出的結構來看,string 可看做 [2]uintptr,而 [ ]byte 則是 [3]uintptr,這便於我們編寫代碼,無需額外定義結構類型。如此,str2bytes 隻需構建 [3]uintptr{ptr, len, len},而 bytes2str 更簡單,直接轉換指針類型,忽略掉 cap 即可。

通過unsafe.Pointer(指針轉換)和uintptr(指針運算)實現轉換

package main
import (
  "fmt"
  "strings"
  "unsafe"
)
func str2bytes(s string) []byte {
  x := (*[2]uintptr)(unsafe.Pointer(&s))
  h := [3]uintptr{x[0], x[1], x[1]}
  return *(*[]byte)(unsafe.Pointer(&h))
}
func bytes2str(b []byte) string {
  return *(*string)(unsafe.Pointer(&b))
}
func main() {
  s := strings.Repeat("abc", 3)
  b := str2bytes(s)
  s2 := bytes2str(b)
  fmt.Println(b, s2)
}

沒有出現逃逸現象

package main
import (
 "testing"
 "io/ioutil"
 "time"
 "fmt"
)
var s, _ = ioutil.ReadFile("mydata4vipday.720.datx")
func test() {
 b := string(s)
 _ = []byte(b)
}
func test2() {
 b := bytes2str(s)
 _ = str2bytes(b)
}
func BenchmarkTest(b *testing.B) {
 t1 := time.Now()
 for i := 0; i < b.N; i++ {
   test()
 }
 fmt.Println("test", time.Now().Sub(t1), b.N)
}
func BenchmarkTestBlock(b *testing.B) {
 t1 := time.Now()
 for i := 0; i < b.N; i++ {
   test2()
 }
 fmt.Println("test block", time.Now().Sub(t1), b.N)
}

對比一下優化前後的性能差異

沒有額外開辟內存0B/op,執行效率:5億次耗時1.6秒,而不用unsafe.Pointer和uintptr轉換300次耗時久達到瞭1.1秒,效率對比高下立判

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: