| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"flag" |
| 21 |
"fmt" |
| 22 |
"log" |
| 23 |
"math" |
| 24 |
"os" |
| 25 |
"runtime/pprof" |
| 26 |
) |
| 27 |
|
| 28 |
var cpuProfile = flag.String("cpuprofile", "", "where to write cpu profile") |
| 29 |
|
| 30 |
func main() { |
| 31 |
flag.Parse() |
| 32 |
f, err := os.Create(*cpuProfile) |
| 33 |
if err != nil { |
| 34 |
log.Fatal("could not create CPU profile: ", err) |
| 35 |
} |
| 36 |
if err := pprof.StartCPUProfile(f); err != nil { |
| 37 |
log.Fatal("could not start CPU profile: ", err) |
| 38 |
} |
| 39 |
defer pprof.StopCPUProfile() |
| 40 |
busyLoop() |
| 41 |
} |
| 42 |
|
| 43 |
func busyLoop() { |
| 44 |
m := make(map[int]int) |
| 45 |
for i := 0; i < 1000000; i++ { |
| 46 |
m[i] = i + 10 |
| 47 |
} |
| 48 |
var sum float64 |
| 49 |
for i := 0; i < 100; i++ { |
| 50 |
for _, v := range m { |
| 51 |
sum += math.Abs(float64(v)) |
| 52 |
} |
| 53 |
} |
| 54 |
fmt.Println("Sum", sum) |
| 55 |
} |