Golang性能优化

2020-02-27 263浏览

  • 1.GO  性能优化   By @miraclesu 网络运营全流程解决方案供应商   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   数据驱动价值!——Enhancing  Data  Usability! 
  • 2.概要   • string  &  profiling   • slice & array • slice & map 初始化   • 并发   • 缓存 
  • 3.string  &  profiling 
  • 4.string  连接1   fmt VS "+"   BenchmarkFmt BenchmarkPlus 1000000 5000000 1617 ns/op 393 ns/op
  • 5.string  连接2   fmt VS "+"   BenchmarkFmt BenchmarkPlus 1000000 5000000 1324 ns/op 751 ns/op
  • 6.string 连接3   strings.join VS "+" 
  • 7.string 连接3   strings.join VS "+"   BenchmarkPlus BenchmarkJoin 500000 1000000 4659 ns/op 1491 ns/op
  • 8.string 连接4   strings.Join VS bytes.Buffer 
  • 9.string 连接4   strings.Join VS bytes.Buffer   BenchmarkJoin 1000000 BenchmarkBuffer 500000 1505 ns/op 2886 ns/op
  • 10.string 连接4-1   strings.Join VS bytes.Buffer 
  • 11.string 连接4-1   strings.Join VS bytes.Buffer   BenchmarkJoin 1000000 BenchmarkBuffer 1000000 1824 ns/op 2588 ns/op
  • 12.内个...内个,我对bytes.Buffer 情有独钟,能不能让Ta快点? 
  • 13.profiling • go  test  -c   • go test -test.bench=. -test.cpuprofile=cpu.prof • go tool pprof bench.test cpu.prof 
  • 14.string 连接4-2   strings.Join VS bytes.Buffer 
  • 15.string 连接4-2   strings.Join VS bytes.Buffer   BenchmarkJoin 1000000 BenchmarkBuffer 1000000 1500 ns/op 1482 ns/op
  • 16.string 连接4-3   strings.Join VS bytes.Buffer 
  • 17.string 连接4-3   strings.Join VS bytes.Buffer   BenchmarkJoin 1000000 BenchmarkBuffer 1000000 1791 ns/op 1162 ns/op
  • 18.string 和  []byte   • 如果可以的话,尽量用多[]byte,少用string • 尽可能少地在两者之间做转换 • append([]byte, string...) • copy([]byte, string)
  • 19.strconv   • func AppendBool(dst []byte, b bool) []byte • func AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []byte • func AppendInt(dst []byte, i int64, base int) []byte • func AppendUint(dst []byte, i uint64, base int) []byte • func FormatBool(b bool) string • func FormatFloat(f float64, fmt byte, prec, bitSize int) string • func FormatInt(i int64, base int) string • func FormatUint(i uint64, base int) string
  • 20.slice & array
  • 21.slice & array 
  • 22.slice & array   BenchmarkArray 200000 BenchmarkSlice 2000000 11101 ns/op 822 ns/op
  • 23.slice & array   • 数组是值传递   • slice是引用传递 
  • 24.slice 坑   
  • 25.slice 结构 
  • 26.make([]byte, 5) 
  • 27.s = s[2:4] 
  • 28.slice & map 初始化 
  • 29.slice  初始化   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 30.slice  初始化测试结果   BenchmarkSlice   BenchmarkSliceCap ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved    50000  100000    33351 ns/op  16432 ns/op 专业DSP解决方案 
  • 31.map 初始化   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 32.map 初始化测试结果   BenchmarkMap                  5000            277715  ns/ op   BenchmarkMapCap          10000            136396  ns/ op   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 33.slice or map? BenchmarkSlice            50000        33351  ns/op   BenchmarkMap            5000                  277715  ns/op   BenchmarkSliceCap      100000          16432  ns/op   BenchmarkMapCap      10000           136396  ns/op 
  • 34.slice & map Read ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 35.slice & map Read 测试结果   BenchmarkMapRead   ns/op   BenchmarkSliceRead   ns/op   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved    10000000                155    20000000                    86.8   专业DSP解决方案 
  • 36.并发 
  • 37.串行泡茶   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 38.总用时  26分钟   • 洗水壶  (1分)   • 烧开水  (15分)   • 洗茶壶  (2分)   • 拿茶叶  (1分)   • 泡茶  (5分)   • 洗茶杯  (2分)   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 39.如果我要泡4杯茶?   并行   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 40.©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 41.问题:   每26分钟生产一杯茶   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 42.©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 43.烧开水最费时间!那么并发他!   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 44.©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 45.泡茶(5分钟)已经成为瓶颈   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   专业DSP解决方案 
  • 46.©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 47.并发3   每3分钟一壶   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 48.并发大于并行,包含并行   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 49.缓存 
  • 50. 提前优化是万恶之源   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll Right Reserved 专业DSP解决方案 
  • 51.Q  &  A   苏创绩  @miraclesu-创绩   欢迎加入我们   网络运营全流程解决方案供应商   ©  广州舜飞信息科技有限公司  All  Right  ReservedAll  Right  Reserved   数据驱动价值!——Enhancing  Data  Usability!