当前位置:网站首页>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")
}
}
边栏推荐
- 【深入理解TcaplusDB技术】TcaplusDB业务数据备份
- Add Icon before input of wechat applet
- Win the championship for 2 consecutive years! ZABBIX ranked first in a number of monitoring software in 2022
- Hot Recruitment! The second Tencent light · public welfare innovation challenge is waiting for you to participate
- AXI_Round_Robin_Arbiter 设计 - AW、W通道部分
- [untitled]
- What do you mean by waiting for insurance records? Where should I go for filing?
- 等保備案是什麼意思?應該去哪裏辦理備案?
- When pandas met SQL, a powerful tool library was born
- Intelligent digital signage solution
猜你喜欢

Error when Oracle enters sqlplus

Building Intel devcloud
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/c5/fe0c9333b46c25be15ed4ba42f7bf8.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

Vulnhub target os-hacknos-1

微信小程序之input前加图标
![[deeply understand tcapulusdb technology] one click installation of tmonitor background](/img/0a/742503e96a9b51735f5fd3f598b9af.png)
[deeply understand tcapulusdb technology] one click installation of tmonitor background

The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars

Distributed database uses logical volume to manage storage expansion

【深入理解TcaplusDB技术】Tmonitor后台一键安装

微信小程序之在wx:for中绑定事件
随机推荐
Xmake v2.6.8 release, compilation cache improvement
How to install the DTS component of SQL server2008r2 on win10 64 bit systems?
[deeply understand tcapulusdb technology] table management of document acceptance
Problems during MySQL uninstallation
Test article
Edge and IOT academic resources
知名人脸搜索引擎惹众怒:仅需一张照片,几秒钟把你扒得底裤不剩
Win10 64位系统如何安装SQL server2008r2的DTS组件?
DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中
Test article
Gold three silver four, busy job hopping? Don't be careless. Figure out these 12 details so that you won't be fooled~
Cause analysis and intelligent solution of information system row lock waiting
Building Intel devcloud
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
KS007基于JSP实现人个人博客系统
Xmake v2.6.8 发布,编译缓存改进
【深入理解TcaplusDB技術】TcaplusDB構造數據
How to correctly calculate the number of rows imported into EXCEL (poi/npoi)
Input adjustment of wechat applet
Pyqt5 designer making tables