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
}

最新文章

  1. Object-C 1.0 第二章
  2. node.js环境
  3. .net中从GridView中导出数据到excel(详细)
  4. vim的常用命令
  5. 黄聪:阿里云Windows2012服务器IIS8实现wordpress完美伪静态(ISAPIRewritev)
  6. ASP.NET MVC进阶之路:深入理解Controller激活机制并使用Ioc容器创建对象
  7. oracle 之 内存—鞭辟近里(四)
  8. http层负载均衡之haproxy
  9. iOS之UIWebView无法获取web标题
  10. BZOJ 1022: [SHOI2008]小约翰的游戏John [SJ定理]
  11. linux打包压缩常用命令
  12. 「洛谷5300」「GXOI/GZOI2019」与或和【单调栈+二进制转化】
  13. Linux IO 模型
  14. Gym - 101490F:Endless Turning (半平面交)
  15. MVC和普通三层架构的区别
  16. pyqt 8行内就可以跑一个浏览器
  17. idea中pom文件需要添加的依赖
  18. Fast R-CNN论文阅读笔记
  19. js下拉框二级关联菜单效果代码具体实现
  20. STL学习笔记--关联式容器

热门文章

  1. mongodb存储引擎WiredTiger
  2. c标签页面进行解析json
  3. synchronized是什么,用法及原理
  4. Leetcode题 112 和 113. Path Sum I and II
  5. 面试题小议---BY gremount
  6. 近似最近邻算法-annoy解析
  7. httpPostedFile实现WEBAPI文件上传
  8. Mysql --- Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
  9. Docker学习知识图
  10. usb 设备 复位