Created
April 18, 2018 15:09
-
-
Save bxcodec/a601e4bc5e9790655ec4c2d5a4d7aeea to your computer and use it in GitHub Desktop.
cpu_profiling golang
This file contains hidden or 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" | |
| "log" | |
| "os" | |
| _runtime "runtime/pprof" | |
| ) | |
| var ( | |
| cpuProfile = "cpu.prof" | |
| ) | |
| func main() { | |
| // Create file to stored the profiling | |
| cpuProf, err := os.Create(cpuProfile) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer cpuProf.Close() | |
| _runtime.StartCPUProfile(cpuProf) | |
| defer _runtime.StopCPUProfile() | |
| ProcessBigArray() | |
| } | |
| func ProcessBigArray() { | |
| for i := 0; i < 10000; i++ { | |
| arr := bigArray() | |
| if arr == nil { | |
| fmt.Println("Array is Nil") | |
| } | |
| } | |
| } | |
| func bigArray() *[]int { | |
| s := make([]int, 1000000) | |
| return &s | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment