Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Last active November 30, 2015 03:00
Show Gist options
  • Save jemygraw/293abcdb005ac10c4eba to your computer and use it in GitHub Desktop.
Save jemygraw/293abcdb005ac10c4eba to your computer and use it in GitHub Desktop.
go make efficiency
package main
import (
"fmt"
"time"
)
func main() {
msg := "i love golang, which will donimate the next generation of cloud service"
limit := 10000000
appendWithCap(msg, limit)
appendWithoutCap(msg, limit)
}
func appendWithoutCap(msg string, limit int) []string {
ts := time.Now()
s := make([]string, 0, 0)
fmt.Println("AppenWithoutCap:", cap(s))
for i := 0; i < limit; i++ {
s = append(s, msg)
}
fmt.Println("AppendWithoutCap:", time.Since(ts), len(s), cap(s))
return s
}
func appendWithCap(msg string, limit int) []string {
ts := time.Now()
s := make([]string, 0, limit)
fmt.Println("AppendWithCap:", cap(s))
for i := 0; i < limit; i++ {
s = append(s, msg)
}
fmt.Println("AppendWithCap:", time.Since(ts), len(s), cap(s))
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment