当前位置:网站首页>golang--判断字符串是否相等
golang--判断字符串是否相等
2022-06-23 13:53:00 【IT艺术家-rookie】
大小写敏感的比较
在对字符串进行大小写敏感的比较时,最基本的字符串比较操作符比使用 strings 包进行比较要快。
大小写不敏感的比较
对于大小写不敏感的比较,EqualFold(Strings 包)比较快。
使用 EqualFold 时先进行字符数量对比,速度会更快。
比较速度
strings.Compare < 运算符 “==” < strings.EqualFold
原因:
- 都解析了整个字符串。
- strings.Compare源码使用的也是运算符比较
- Equalfold 也会逐个字符进行解析,但是当它解析到两个字符串中不同的字符时,就会提前结束。
测试方法
compare.go
func compareOperators(a string, b string) bool {
if a == b {
return true
} else {
return false
}
}
// strings compare
func compareString(a string, b string) bool {
if strings.Compare(a, b) == 0 {
return true
}
return false
}
compare_test.go
import (
"testing"
)
func BenchmarkCompareOperators(b *testing.B) {
for n := 0; n < b.N; n++ {
compareOperators("This is a string", "This is a strinG")
}
}
func BenchmarkCompareString(b *testing.B) {
for n := 0; n < b.N; n++ {
compareString("This is a string", "This is a strinG")
}
}
20 万行的列表文件==>这个文件包含大量的字符串
compareTxt.go
// operator compare
func compareOperatorsTxt(a string) bool {
file, err := os.Open("names.txt")
result := false;
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
if strings.ToLower(a) == strings.ToLower(scanner.Text()) {
result = true
}else {
result = false
}
}
file.Close()
return result
}
// strings compare
func compareStringTxt(a string) bool {
file, err := os.Open("names.txt")
result := false;
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
if strings.Compare(strings.ToLower(a), strings.ToLower(scanner.Text())) == 0 {
result = true
}else {
result = false
}
}
file.Close()
return result
}
// EqualFold compare
func compareEFTxt(a string) bool {
file, err := os.Open("names.txt")
result := false;
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
if strings.EqualFold(a, scanner.Text()) {
result = true
}else {
result = false
}
}
file.Close()
return result
}
//字符串长度相同而字符不同
func compareByCountTxt(a string) bool {
file, err := os.Open("names.txt")
result := false;
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
if len(a) == len(scanner.Text()) && strings.EqualFold(a, scanner.Text()){
result = true
}else {
result = false
}
}
file.Close()
return result
}
compareTxt_test.go
func BenchmarkCompareOperatorsTxt(b *testing.B) {
for n := 0; n < b.N; n++ {
compareOperatorsTxt("Immanuel1234")
}
}
func BenchmarkCompareStringTxt(b *testing.B) {
for n := 0; n < b.N; n++ {
compareStringTxt("Immanuel1234")
}
}
func BenchmarkEqualFoldTxt(b *testing.B) {
for n := 0; n < b.N; n++ {
compareEFTxt("Immanuel1234")
}
}
func BenchmarkCompareByCountTxt(b *testing.B){
for n := 0; n < b.N; n++ {
compareByCountTxt("Immanuel1234")
}
}
边栏推荐
- Use xtradiagram Diagramcontrol for drawing and controlling process graphics
- 系统设计与分析-技术报告-定时清理验证码的一种解决方案
- Technology creates value and teaches you how to collect wool
- Quick view of wechat applet development process
- Auto - vérification recommandée! Les bogues MySQL ne font pas reculer les transactions, peut - être êtes - vous à risque!
- Technology sharing | do you understand the requirements of the tested project?
- How to install the DTS component of SQL server2008r2 on win10 64 bit systems?
- How to make food nutrition label
- Distributed database uses logical volume to manage storage expansion
- 一款自动生成单元测试的 IDEA 插件
猜你喜欢

【深入理解TcaplusDB技術】TcaplusDB構造數據

ICML 2022 𞓜 context integrated transformer based auction design neural network

MySQL installation

2022 soft science university professional ranking released! Xi'an electric AI ranked higher than Qingbei, and Nantah ranked first in the country!

Wechat applet pop up the optional menu from the bottom
![[deeply understand tcapulusdb technology] tmonitor background one click installation](/img/0a/742503e96a9b51735f5fd3f598b9af.png)
[deeply understand tcapulusdb technology] tmonitor background one click installation

The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine

Error when Oracle enters sqlplus

DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中

Soaring 2000+? The average salary of software testing in 2021 has come out, and I can't sit still
随机推荐
The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine
What does it mean for AI developers after 2022
系统设计与分析-技术报告-定时清理验证码的一种解决方案
How does activity implement lifecycleowner?
Use of pyqt5 tool box
[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?
Technology sharing | do you understand the requirements of the tested project?
ts封装请求
首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
NFNet:NF-ResNet的延伸,不用BN的4096超大batch size训练 | 21年论文
Soaring 2000+? The average salary of software testing in 2021 has come out, and I can't sit still
Kali use
Building Intel devcloud
等保備案是什麼意思?應該去哪裏辦理備案?
[in depth understanding of tcapulusdb technology] tcapulusdb construction data
【二级等保】过二级等保用哪个堡垒机品牌好?
Low grain prices hurt farmers, low wages hurt farmers!
How to correctly calculate the number of rows imported into EXCEL (poi/npoi)
Teach you how to build Tencent cloud server (explanation with pictures and pictures)
【深入理解TcaplusDB技术】如何实现Tmonitor单机安装