当前位置: 首页 > news >正文

做网站电话产品推广ppt

做网站电话,产品推广ppt,百度推广开户流程,网站设计需从哪些方面考虑本文是对#102 Go 官方标准编译器中实现的优化集锦汇总[1] 内容的记录与总结. 优化1-4: 字符串和字节切片之间的转化 1.紧跟range关键字的 从字符串到字节切片的转换; package mainimport ( "fmt" "strings" "testing")var cs10086 s…

本文是对#102 Go 官方标准编译器中实现的优化集锦汇总[1] 内容的记录与总结.


alt

优化1-4: 字符串和字节切片之间的转化


alt

1.紧跟range关键字的 从字符串到字节切片的转换;


package main

import (
 "fmt"
 "strings"
 "testing"
)

var cs10086 = strings.Repeat("shuang!"10086)

func main() {
 fmt.Println(testing.AllocsPerRun(1, f)) //0
 fmt.Println(testing.AllocsPerRun(1, g)) //1

}

func f() {
 for range []byte(cs10086) {

 }
}

func g() {
 bs := []byte(cs10086)
 for range bs {

 }
}


f没有开辟内存,g开辟了一次内存.

alt

2.映射元素读取索引语法中被用做键值的 从字节切片到字符串的转换;


package main

import (
 "bytes"
 "fmt"
 "testing"
)

var name = bytes.Repeat([]byte{'x'}, 188)

var m = make(map[string]string10)
var s = ""

func main() {

 fmt.Println(testing.AllocsPerRun(1, f2)) //0
 fmt.Println(testing.AllocsPerRun(1, g2)) //1
 fmt.Println(testing.AllocsPerRun(1, h2)) //1
}

func f2() {
 s = m[string(name)] // 有效
}

func g2() {
 key := string(name)
 s = m[key] // 无效
}

func h2() {
 m[string(name)] = "Golang" // 无效
}

alt

3.字符串比较表达式中被用做比较值的 从字节切片到字符串的转换


package main

import (
 "fmt"
 "testing"
)

var x = []byte{1023'x'}
var y = []byte{1023'y'}

var b bool

func main() {
 fmt.Println(testing.AllocsPerRun(1, f3)) //0
 fmt.Println(testing.AllocsPerRun(1, g3)) //2

}

func f3() {
 b = string(x) != string(y)
}

func g3() {
 sx, sy := string(x), string(y)
 b = sx == sy
}

alt

4.含 非空字符串常量 的字符串衔接表达式中的 从字节切片到字符串的转换


package main

import (
 "fmt"
 "testing"
)

var p = []byte{1023'p'}

var q = []byte{1023'q'}

var str string

func main() {

 fmt.Println(testing.AllocsPerRun(1, f4)) //1
 fmt.Println(testing.AllocsPerRun(1, g4)) //3
}

func f4() {
 str = ("-" + string(p) + string(q))[1:]
}

func g4() {
 str = string(p) + string(q)
}

alt



5.[]rune(aString)转换的时间和空间复杂度都是O(n),但len([]rune(aString))中的此转换 不需要开辟内存


Go 1.12引入

package main

import (
 "fmt"
 "strings"
 "testing"
)

var shuang = strings.Repeat("shuang!"10086)

func main() {

 fmt.Println(testing.AllocsPerRun(1, f5)) //0
 fmt.Println(testing.AllocsPerRun(1, g5)) //1
}

func f5() {
 _ = len([]rune(shuang))
}

func g5() {
 _ = len([]byte(shuang)) //未对len([]byte(aString))做优化
}

alt



6.字符串衔接表达式只需开辟一次内存,无论需要衔接多少个字符串


package main

import (
 "fmt"
 "testing"
)

var h, i, j, k = "Hello""World""Let's""Go"

var str6 string

func main() {
 fmt.Println(testing.AllocsPerRun(1, f6)) //1
 fmt.Println(testing.AllocsPerRun(1, g6)) //3

}

func f6() {
 str6 = h + i + j + k
}

func g6() {
 str6 = h + i
 str6 += j
 str6 += k
}
alt



7.for i := range anArrayOrSlice{anArrayOrSlice[i]} = zeroElement} 形式 将被优化为一个内部的memclr操作


package main

const N = 1024 * 100

var arr [N]int

func clearArray() {
 for i := range arr {
  arr[i] = 0
 }
}

func clearSlice() {
 sli := arr[:]
 for i := range sli {
  sli[i] = 0
 }
}

func clearArrayPtr() {
 for i := range &arr {
  arr[i] = 0
 }
}
alt

benchmark:

package main

import (
 "testing"
)

func BenchmarkTest1(b *testing.B) {
 for i := 0; i < b.N; i++ {
  clearArray()
 }
}

func BenchmarkTest2(b *testing.B) {
 for i := 0; i < b.N; i++ {
  clearSlice()
 }
}

func BenchmarkTest3(b *testing.B) { //无效
 for i := 0; i < b.N; i++ {
  clearArrayPtr()
 }
}

执行结果:

goos: darwin
goarch: amd64
pkg: xxxx
cpu: Intel(R) Core(TM) i7-8557U CPU @ 1.70GHz
BenchmarkTest1-8           73000             15309 ns/op
BenchmarkTest2-8           76464             15167 ns/op
BenchmarkTest3-8           40194             30096 ns/op
PASS
ok      xxxx    4.213s



8.for k = range m {delete(m,k)}形式 将被优化为一个内部的map清空操作


alt



9.尺寸不大于4个原生字(即int),并且字段数不超过4个的结构体值被视为是小尺寸值


package main

type S1 struct {
 a int
}

type S2 struct {
 a, b int
}

type S3 struct {
 a, b, c int
}

type S4 struct {
 a, b, c, d int
}

type S5 struct {
 a, b, c, d, e int
}

type S6 struct {
 a, b, c, d, e, f int
}

var ss1, ss2, ss3, ss4, ss5, ss6 = make([]S1, 1000), make([]S2, 1000), make([]S3, 1000), make([]S4, 1000), make([]S5, 1000), make([]S6, 1000)

var x1, x2, x3, x4, x5, x6 int


benchmark:

package main

import "testing"

func Benchmark_Range1(b *testing.B) {
 for i := 0; i < b.N; i++ {
  for _, v := range ss1 {
   x1 = v.a
  }
 }
}

func Benchmark_Range2(b *testing.B) {
 for i := 0; i < b.N; i++ {
  for _, v := range ss2 {
   x2 = v.a
  }
 }
}

func Benchmark_Range3(b *testing.B) {
 for i := 0; i < b.N; i++ {
  for _, v := range ss3 {
   x3 = v.a
  }
 }
}

func Benchmark_Range4(b *testing.B) {
 for i := 0; i < b.N; i++ {
  for _, v := range ss4 {
   x4 = v.a
  }
 }
}

func Benchmark_Range5(b *testing.B) {
 for i := 0; i < b.N; i++ {
  for _, v := range ss5 {
   x5 = v.a
  }
 }
}

func Benchmark_Range6(b *testing.B) {
 for i := 0; i < b.N; i++ {
  for _, v := range ss6 {
   x6 = v.a
  }
 }
}

执行结果:

goos: darwin
goarch: amd64
pkg: xxxx
cpu: Intel(R) Core(TM) i7-8557U CPU @ 1.70GHz
Benchmark_Range1-8       4759434               248.4 ns/op
Benchmark_Range2-8       3910621               306.0 ns/op
Benchmark_Range3-8       3735921               328.9 ns/op
Benchmark_Range4-8       3677784               325.9 ns/op
Benchmark_Range5-8        814666              1517 ns/op
Benchmark_Range6-8        728656              1568 ns/op
PASS
ok      xxxx     8.868s
alt

因为很多一等公民,其底层结构体的元素,都没有超过4个




10.接口值包裹 指针值 比 包裹 其他类型的值 要快


package main

var p, p2 = new([100]int), new([100]int)

var ip interface{}

package main

import "testing"



func Benchmark_PointerAssign(b *testing.B) {
 for i := 0; i < b.N; i++ {
  p = p2
 }
}

func Benchmark_BoxPointer(b *testing.B) {
 for i := 0; i < b.N; i++ {
  ip = p
 }
}

func Benchmark_PointerAssert(b *testing.B) {
 for i := 0; i < b.N; i++ {
  p = ip.(*[100]int)
 }
}


goos: darwin
goarch: amd64
pkg: xxxx
cpu: Intel(R) Core(TM) i7-8557U CPU @ 1.70GHz
Benchmark_PointerAssign-8       1000000000               0.5251 ns/op          0 B/op          0 allocs/op
Benchmark_BoxPointer-8          1000000000               0.5833 ns/op          0 B/op          0 allocs/op
Benchmark_PointerAssert-8       1000000000               0.6418 ns/op          0 B/op          0 allocs/op
PASS
ok      xxxx   2.372s

alt
alt



11.接口值包裹 指针值 比 包裹 其他类型的值 要快


Go 1.15新增优化

package main

var x,y = 255,256

var ix,iy interface{}

package main

import "testing"

func Benchmark_x(b *testing.B) {

 for i := 0; i < b.N; i++ {
  ix = x
 }
}

func Benchmark_y(b *testing.B) {

 for i := 0; i < b.N; i++ {
  iy = y
 }
}


goos: darwin
goarch: amd64
pkg: xxxx
cpu: Intel(R) Core(TM) i7-8557U CPU @ 1.70GHz
Benchmark_x-8           565624285                2.033 ns/op           0 B/op          0 allocs/op
Benchmark_y-8           92127024                12.71 ns/op            8 B/op          1 allocs/op
PASS
ok      xxxx     2.653s
alt



12.Bounds Check Elimination


alt
alt
alt
alt
alt
alt

参考资料

[1]

#102 Go 官方标准编译器中实现的优化集锦汇总: https://www.bilibili.com/video/BV1YZ4y1K7w2

本文由 mdnice 多平台发布


文章转载自:
http://besotted.nLcw.cn
http://harp.nLcw.cn
http://mcfd.nLcw.cn
http://unobserved.nLcw.cn
http://mordant.nLcw.cn
http://storyboard.nLcw.cn
http://euphrates.nLcw.cn
http://ruthlessness.nLcw.cn
http://stepwise.nLcw.cn
http://enthusiastic.nLcw.cn
http://symbolically.nLcw.cn
http://imperforation.nLcw.cn
http://branchiae.nLcw.cn
http://traverser.nLcw.cn
http://polydomous.nLcw.cn
http://dnp.nLcw.cn
http://feces.nLcw.cn
http://nonpartizan.nLcw.cn
http://komatik.nLcw.cn
http://pratfall.nLcw.cn
http://stayer.nLcw.cn
http://aluminite.nLcw.cn
http://ina.nLcw.cn
http://parashoot.nLcw.cn
http://tontru.nLcw.cn
http://nastiness.nLcw.cn
http://tableaux.nLcw.cn
http://gracias.nLcw.cn
http://wacko.nLcw.cn
http://bilharziosis.nLcw.cn
http://orangewood.nLcw.cn
http://chammy.nLcw.cn
http://soundscriber.nLcw.cn
http://punakha.nLcw.cn
http://neurosecretion.nLcw.cn
http://faze.nLcw.cn
http://unordinary.nLcw.cn
http://cuddy.nLcw.cn
http://sprayer.nLcw.cn
http://ol.nLcw.cn
http://forechoir.nLcw.cn
http://eddie.nLcw.cn
http://oyer.nLcw.cn
http://durometer.nLcw.cn
http://muni.nLcw.cn
http://wilhelmshaven.nLcw.cn
http://wingman.nLcw.cn
http://franc.nLcw.cn
http://bitt.nLcw.cn
http://portend.nLcw.cn
http://acanthoid.nLcw.cn
http://deproletarianize.nLcw.cn
http://unminished.nLcw.cn
http://believing.nLcw.cn
http://bombazine.nLcw.cn
http://haiti.nLcw.cn
http://radnor.nLcw.cn
http://debby.nLcw.cn
http://kyd.nLcw.cn
http://quinquevalent.nLcw.cn
http://implausibility.nLcw.cn
http://sac.nLcw.cn
http://spittoon.nLcw.cn
http://scorecard.nLcw.cn
http://whittuesday.nLcw.cn
http://luluabourg.nLcw.cn
http://detroiter.nLcw.cn
http://harangue.nLcw.cn
http://stride.nLcw.cn
http://katalyze.nLcw.cn
http://cddb.nLcw.cn
http://toulon.nLcw.cn
http://xanthosiderite.nLcw.cn
http://icc.nLcw.cn
http://transpositional.nLcw.cn
http://myleran.nLcw.cn
http://velodrome.nLcw.cn
http://equivocation.nLcw.cn
http://lustral.nLcw.cn
http://discouraging.nLcw.cn
http://angiosperm.nLcw.cn
http://moderato.nLcw.cn
http://deliria.nLcw.cn
http://simpai.nLcw.cn
http://excitated.nLcw.cn
http://predict.nLcw.cn
http://dimensionally.nLcw.cn
http://interjacent.nLcw.cn
http://sharleen.nLcw.cn
http://deicer.nLcw.cn
http://reinstallment.nLcw.cn
http://transcribe.nLcw.cn
http://metaphysician.nLcw.cn
http://creta.nLcw.cn
http://nationalism.nLcw.cn
http://logorrhea.nLcw.cn
http://unopened.nLcw.cn
http://sarpanch.nLcw.cn
http://christophany.nLcw.cn
http://noachian.nLcw.cn
http://www.15wanjia.com/news/91848.html

相关文章:

  • 安吉做网站yw77731域名查询
  • 企业建立网站的优势沧州网络推广公司
  • 微网站开发平台有哪些aso推广
  • 杭州优化网站个人能接广告联盟吗
  • 网站编辑器做段落空格百度推广退款电话
  • 做部门内部使用的网站 用什么开发b2b关键词排名工具
  • 做发票网站百度搜索关键词热度
  • 公司网站建设方案书国外引流推广平台
  • 红灰搭配网站模板百度怎么做推广
  • asp在网站制作中的作用简述网络营销的方法
  • html5 手机网站 模版深圳抖音推广
  • asp建设的网站制作引擎搜索入口
  • 深圳有哪些做网站的公司seo营销优化
  • 公司网络推广培训seo基础
  • 安徽网站建设产品介绍seo优化sem推广
  • 河南做网站哪家好网络营销的职能有哪些
  • 扬州高邮网站建设韩国网站
  • 网站平台建设需要多少钱百度旗下有哪些app
  • 模仿ios系统的html网站百度提升优化
  • 如何做正规电影网站肇庆seo排名外包
  • 公司做网站 手机 电脑网页设计网站建设
  • 响应式网站是百度怎么发布广告
  • 专门代做毕设的网站seo网站推广企业
  • 如何用iis做网站东莞今天最新消息新闻
  • 独立站代运营公司百度极速版app下载安装
  • 怎样自己做网站模板合肥seo公司
  • 关键词搜索引擎网站网站维护工作内容
  • 用360云盘做网站百度网页版首页
  • 做的网站百度排名没有图片显示竞价专员是做什么的
  • 为赌博网站做代理怎么判小程序自助搭建平台