Last active
November 30, 2015 03:00
-
-
Save jemygraw/293abcdb005ac10c4eba to your computer and use it in GitHub Desktop.
go make efficiency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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