golang 切片和map查询比较
2024-08-27 23:05:03
package main
import (
"fmt"
"time"
)
var testTimeSlice = []string{"aa", "bb", "cc", "dd", "ee", "aa", "zz"}
var testTimeMap = map[string]bool{"aa": true, "bb": true, "cc": true, "dd": true, "ee": true, "ff": true, "zz": true}
//以上为第一组查询测试数据
var testTimeSlice2 = [] string{"aa", "bb", "cc", "dd", "ee", "aa", "aa", "bb", "cc", "dd", "ee", "aa", "aa", "bb", "cc", "dd", "ee", "aa", "aa", "bb", "cc", "dd", "ee", "aa", "i", "j", "l", "m", "n", "o", "p", "q", "k", "x", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "zz"}
var testTimeMap2 = map[string]bool{"aa": true, "bb": true, "cc": true, "dd": true, "ee": true, "ff": true, "qq": true, "ww": true, "rr": true, "tt": true, "zz": true, "uu": true, "ii": true, "oo": true, "pp": true, "lk": true, "kl": true, "jk": true, "kj": true, "hl": true, "lh": true, "fg": true, "gfdd": true, "df": true, "fd": true,
"i": true, "j": true, "l": true, "m": true, "n": true, "o": true, "p": true, "q": true, "k": true, "x": true, "y": true, "z": true,
"1": true, "2": true, "3": true, "4": true, "5": true, "6": true, "7": true, "8": true, "9": true, "10": true}
//以上为第二组查询测试数据
func testSlice(a []string) {
now := time.Now()
for j := 0; j < 100000; j++ {
for _, v := range a {
if v == "zz" {
break
}
}
}
finish1 := time.Since(now)
fmt.Println(finish1)
}
func testMap(a map[string]bool) {
now := time.Now()
for j := 0; j < 100000; j++ {
if ok := a["zz"]; ok{
continue
}
}
finish2 := time.Since(now)
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
fmt.Println(finish2)
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
}
func main(){
//slice查询是遍历方式,时间复杂度是O(n), map查询是hash映射
//当数据量小的时候切片查询比map快,但是数据量大的时候map的优势就体现出来了
testSlice(testTimeSlice) //999.8µs
testMap(testTimeMap) //4.9961ms
testSlice(testTimeSlice2) //5.0147ms
testMap(testTimeMap2) //3.0003ms
}
最新文章
- Object-C 1.0 第二章
- node.js环境
- .net中从GridView中导出数据到excel(详细)
- vim的常用命令
- 黄聪:阿里云Windows2012服务器IIS8实现wordpress完美伪静态(ISAPIRewritev)
- ASP.NET MVC进阶之路:深入理解Controller激活机制并使用Ioc容器创建对象
- oracle 之 内存—鞭辟近里(四)
- http层负载均衡之haproxy
- iOS之UIWebView无法获取web标题
- BZOJ 1022: [SHOI2008]小约翰的游戏John [SJ定理]
- linux打包压缩常用命令
- 「洛谷5300」「GXOI/GZOI2019」与或和【单调栈+二进制转化】
- Linux IO 模型
- Gym - 101490F:Endless Turning (半平面交)
- MVC和普通三层架构的区别
- pyqt 8行内就可以跑一个浏览器
- idea中pom文件需要添加的依赖
- Fast R-CNN论文阅读笔记
- js下拉框二级关联菜单效果代码具体实现
- STL学习笔记--关联式容器
热门文章
- mongodb存储引擎WiredTiger
- c标签页面进行解析json
- synchronized是什么,用法及原理
- Leetcode题 112 和 113. Path Sum I and II
- 面试题小议---BY gremount
- 近似最近邻算法-annoy解析
- httpPostedFile实现WEBAPI文件上传
- Mysql --- Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
- Docker学习知识图
- usb 设备 复位