go 迭代string數(shù)組操作 go for string[]
go 迭代string數(shù)組,直接拷貝去用即可
package main import ( "fmt" ) func main() { subsCodes := []string{"aaaa", "vvvvv", "dddd", "eeeee", "gfgggg"} for _, s := range subsCodes { fmt.Println(s) } }
補(bǔ)充:golang字符串string與字符數(shù)組[]byte高效轉(zhuǎn)換
string與[]byte的直接轉(zhuǎn)換是通過底層數(shù)據(jù)copy實(shí)現(xiàn)的
var a = []byte("hello boy")
var b = string(a)
這種操作在并發(fā)量達(dá)到十萬百萬級(jí)別的時(shí)候會(huì)拖慢程序的處理速度
通過gdb調(diào)試來看一下string和[]byte的數(shù)據(jù)結(jié)構(gòu)
(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; }
轉(zhuǎn)換后 [ ]byte 底層數(shù)組與原 string 內(nèi)部指針并不相同,以此可確定數(shù)據(jù)被復(fù)制。那么,如不修改數(shù)據(jù),僅轉(zhuǎn)換類型,是否可避開復(fù)制,從而提升性能?
從 ptype 輸出的結(jié)構(gòu)來看,string 可看做 [2]uintptr,而 [ ]byte 則是 [3]uintptr,這便于我們編寫代碼,無需額外定義結(jié)構(gòu)類型。如此,str2bytes 只需構(gòu)建 [3]uintptr{ptr, len, len},而 bytes2str 更簡單,直接轉(zhuǎn)換指針類型,忽略掉 cap 即可。
通過unsafe.Pointer(指針轉(zhuǎn)換)和uintptr(指針運(yùn)算)實(shí)現(xiàn)轉(zhuǎn)換
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) }
沒有出現(xiàn)逃逸現(xiàn)象
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) }
對(duì)比一下優(yōu)化前后的性能差異
沒有額外開辟內(nèi)存0B/op,執(zhí)行效率:5億次耗時(shí)1.6秒,而不用unsafe.Pointer和uintptr轉(zhuǎn)換300次耗時(shí)久達(dá)到了1.1秒,效率對(duì)比高下立判
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Golang channel關(guān)閉的實(shí)現(xiàn)示例
channel關(guān)閉不當(dāng)或不關(guān)閉會(huì)引發(fā)很多問題,本文主要介紹了Golang channel關(guān)閉的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Go標(biāo)準(zhǔn)庫-ServeMux的使用與模式匹配深入探究
這篇文章主要為大家介紹了Go標(biāo)準(zhǔn)庫-ServeMux的使用與模式匹配深入探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01golang使用tail實(shí)現(xiàn)追蹤文件變更
這篇文章主要為大家介紹了golang如何借助 github.com/hpcloud/tail ,實(shí)現(xiàn)實(shí)時(shí)追蹤文件變更,達(dá)到類似shell命令tail -f的效果,感興趣的小伙伴可以了解一下2023-08-08基于golang channel實(shí)現(xiàn)的輕量級(jí)異步任務(wù)分發(fā)器示例代碼
這篇文章主要給大家介紹了關(guān)于基于golang channel實(shí)現(xiàn)的輕量級(jí)異步任務(wù)分發(fā)器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Golang WaitGroup實(shí)現(xiàn)原理解析
WaitGroup是Golang并發(fā)的兩種方式之一,一個(gè)是Channel,另一個(gè)是WaitGroup,下面這篇文章主要給大家介紹了關(guān)于golang基礎(chǔ)之waitgroup用法以及使用要點(diǎn)的相關(guān)資料,需要的朋友可以參考下2023-02-02