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