You are reporting all the allocations made in a BenchmarkFmt function which 
includes allocations made in the fmt.Println function. See 
https://golang.org/src/fmt/print.go.

Peter

On Thursday, October 6, 2016 at 12:01:16 AM UTC-4, 刘桂祥 wrote:
>
>
>
> package main
>
>
> import (
>     "fmt"
>     _ "net/http/pprof"
>     "testing"
> )
>
>
> // 8 Bytes/op   1 allocs/op
> func BenchmarkFmt1(b *testing.B) {   
>  b.ReportAllocs()
>     for i := 0; i < b.N; i++ {
>         fmt.Println(100)
>     }
> }
>
>
> // 16 Bytes/op   1 allocs/op
> func BenchmarkFmt2(b *testing.B) {
>     b.ReportAllocs()
>     for i := 0; i < b.N; i++ {
>         fmt.Println("hello")
>     }
> }
>
>
> // 32 Bytes/op   1 allocs/op
> func BenchmarkFmt3(b *testing.B) {
>     b.ReportAllocs()
>     for i := 0; i < b.N; i++ {
>         fmt.Println([]int{})
>   
>
>   }
> }
>
>
> // 48 Bytes/op   3 allocs/op
> func BenchmarkFmt4(b *testing.B) {
>     b.ReportAllocs()
>     for i := 0; i < b.N; i++ {
>         fmt.Println([]int{100})
>     }
> }
>
>
> // 64 Bytes/op   4 allocs/op
> func BenchmarkFmt5(b *testing.B) {
>     b.ReportAllocs()
>     for i := 0; i < b.N; i++ {
>         fmt.Println([]int{100, 200})
>     }
> }
>
>  go version 1.7
>  
>  Fmt1 : the runtime alloc 100 in heap
>  Fmt2:  the runtime alloc a string header (addr len) in heap 
>  Fmt3:  why the runtime alloc 32 bytes for slice header ??
>  Fmt4:  why have 3 allocs and 48 bytes ??
>  Fmt5:  why have 4 allocs and 64 bytes ??
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to