Giter VIP home page Giter VIP logo

Comments (9)

lee501 avatar lee501 commented on July 18, 2024

from go-patterns.

Felyne avatar Felyne commented on July 18, 2024

请看这里
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7}
//删除第2个元素,从0开始算
s = append(s[:2], s[2+1:]...)
printInfo(s)
//结果:
//[0 1 3 4 5 6 7]
//len: 7 cap: 8
}

func printInfo(s []int) {
fmt.Println(s)
fmt.Println("len:", len(s), "cap:",cap(s))
}

from go-patterns.

lee501 avatar lee501 commented on July 18, 2024

请看这里
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7}
//删除第2个元素,从0开始算
s = append(s[:2], s[2+1:]...)
printInfo(s)
//结果:
//[0 1 3 4 5 6 7]
//len: 7 cap: 8
}

func printInfo(s []int) {
fmt.Println(s)
fmt.Println("len:", len(s), "cap:",cap(s))
}

前后s的地址是不同的,通过append操作后,实际上新开辟一块地址空间在s[:2]上进行扩容,原来部分会被go的垃圾回收处理, 参考以下代码
i := []int{1,2,3}
fmt.Printf("append前地址%p\n", i)
i = append(i, 4)
fmt.Printf("append前地址%p\n", i)

from go-patterns.

Felyne avatar Felyne commented on July 18, 2024

你这个代码是增加元素,不是删除,超出容量当然要重新开辟一个空间

from go-patterns.

Felyne avatar Felyne commented on July 18, 2024

你看这里:

func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7}
fmt.Printf("%p\n", s)
//删除第2个元素,从0开始算
s = append(s[:2], s[2+1:]...)
fmt.Printf("%p\n", s)
fmt.Println(s)
fmt.Println("len:", len(s), "cap:",cap(s))
//结果:
//0xc0000a6000
//0xc0000a6000
//[0 1 3 4 5 6 7]
//len: 7 cap: 8
}

from go-patterns.

lee501 avatar lee501 commented on July 18, 2024

你看这里:

func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7}
fmt.Printf("%p\n", s)
//删除第2个元素,从0开始算
s = append(s[:2], s[2+1:]...)
fmt.Printf("%p\n", s)
fmt.Println(s)
fmt.Println("len:", len(s), "cap:",cap(s))
//结果:
//0xc0000a6000
//0xc0000a6000
//[0 1 3 4 5 6 7]
//len: 7 cap: 8
}
append我理解错误了,是只有超过cap的时候底层数组重现分配内存。
重现创建一个slice放剩下元素的代码能粘贴一下吗,在做少量删除的时候直接append时内存的计算时间耗时会少些

from go-patterns.

lee501 avatar lee501 commented on July 18, 2024

https://github.com/golang/go/wiki/SliceTricks

from go-patterns.

Felyne avatar Felyne commented on July 18, 2024

s := []string{"a", "b"}
d := "b"
var s2 []string
for _,v := range s {
if v != d {
s2 = append(s2, v)
}
}

from go-patterns.

lee501 avatar lee501 commented on July 18, 2024

s := []string{"a", "b"}
d := "b"
var s2 []string
for _,v := range s {
if v != d {
s2 = append(s2, v)
}
}

空间换时间吧, 少量删除结果还是直接在原来基础上更快些

func main() {
s := []string{"a", "b", "c", "d"}
begin := time.Now()
s = append(s[:1], s[2:]...)
end := time.Now()
fmt.Println(end.UnixNano() - begin.UnixNano()) //0
m := []string{"a", "b", "c", "d"}
begin = time.Now()
var s2 []string
for _, v := range m {
if v != "b" {
s2 = append(s2, v)
}
}
end = time.Now()
fmt.Println(end.UnixNano() - begin.UnixNano()) //1000~ 7000
}

from go-patterns.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.