In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "string len = = 0 and string = =" what is the difference ", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn the" string len = = 0 and string = = "what are the differences?
In the test method, we declared the Test1 and Test2 methods, respectively:
Func Test1 () bool {var v string if v = = "" {return true} return false} func Test2 () bool {var v string if len (v) = 0 {return true} return false}
Only a simple variable type declaration is made inside the method, based on the string = = "" and the string len = = 0, respectively.
Test case
Write Benchmark for two methods for subsequent performance testing:
Func BenchmarkTest1 (b * testing.B) {for I: = 0; I < b.N; iTunes + {Test1 ()}} func BenchmarkTest2 (b * testing.B) {for I: = 0; I < b.N; iTunes + {Test2 ()}}
Result analysis
$go test-bench=. -benchmem goos: darwin goarch: amd64 BenchmarkTest1-4 1000000000 0.305 ns/op 0 B/op 0 allocs/op BenchmarkTest2-4 1000000000 0.305 ns/op 0 B/op 0 allocs/op PASS ok _ / Users/eddycjy/go-application/awesomeProject/tests 0.688s
From the results of many tests, the two are compared:
There is almost no difference in performance, and it can even be exactly the same.
All of them do not involve memory request and operation, and are all 0/op. Indicates that the variable is not declared, there is an initialization action, this Go compiler has to do optimization.
It turns out to be the same. According to Cao Da's hint, we can take a closer look at the assembly code of the two to see what the specific difference is:
$go tool compile-S main.go "" .main STEXT nosplit size=1 args=0x0 locals=0x0 0x0000 00000 (main.go:3) TEXT ".main (sb), NOSPLIT | ABIInternal, $0-0 0x0000 00000 (main.go:3) FUNCDATA $0, gclocals 33cdeccccebe80329f1fdbee7f5874cb (sb) 0x0000 00000 (main.go:3) FUNCDATA $1, gclocals 33cdeccccebe80329f1fdbee7f5874cb (sb) 0x0000 00000 (main.go:5) RET 0x0000 c3. Go.cuinfo.packagename. SDWARFINFO dupok size=0 0x0000 6d 61 69 6e main "... init task SNOPTRDATA size=24 0x0000 0000 0000 0000. 0x0010 00 00 00. Gclocals ·33cdeccccebe80329f1fdbee7f5874cb SRODATA dupok size=8 0x0000 01 0000 0000 0000 00
Whether it is len (v) = = 0, or v = = "", the compiled assembly code is exactly the same. It is clear that the Go compiler has made a clear optimization in this block, and the probability is that it will be compared directly.
Therefore, in the Go language, the determination of the string len = = 0 and the string = = "" can be regarded as indistinguishable.
Thank you for your reading, the above is the "string len = = 0 and string = =" what is the difference between the content, after the study of this article, I believe you on the string len = = 0 and string = = "" what is the difference between this problem has a more profound understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.