github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/test/bench/go1/fmt_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package go1
     6  
     7  // benchmark based on fmt/fmt_test.go
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"testing"
    13  )
    14  
    15  func BenchmarkFmtFprintfEmpty(b *testing.B) {
    16  	var buf bytes.Buffer
    17  	for i := 0; i < b.N; i++ {
    18  		fmt.Fprintf(&buf, "")
    19  	}
    20  }
    21  
    22  func BenchmarkFmtFprintfString(b *testing.B) {
    23  	var buf bytes.Buffer
    24  	for i := 0; i < b.N; i++ {
    25  		buf.Reset()
    26  		fmt.Fprintf(&buf, "%s", "hello")
    27  	}
    28  }
    29  
    30  func BenchmarkFmtFprintfInt(b *testing.B) {
    31  	var buf bytes.Buffer
    32  	for i := 0; i < b.N; i++ {
    33  		buf.Reset()
    34  		fmt.Fprintf(&buf, "%d", 5)
    35  	}
    36  }
    37  
    38  func BenchmarkFmtFprintfIntInt(b *testing.B) {
    39  	var buf bytes.Buffer
    40  	for i := 0; i < b.N; i++ {
    41  		buf.Reset()
    42  		fmt.Fprintf(&buf, "%d %d", 5, 6)
    43  	}
    44  }
    45  
    46  func BenchmarkFmtFprintfPrefixedInt(b *testing.B) {
    47  	var buf bytes.Buffer
    48  	for i := 0; i < b.N; i++ {
    49  		buf.Reset()
    50  		fmt.Fprintf(&buf, "This is some meaningless prefix text that needs to be scanned %d", 6)
    51  	}
    52  }
    53  
    54  func BenchmarkFmtFprintfFloat(b *testing.B) {
    55  	var buf bytes.Buffer
    56  	for i := 0; i < b.N; i++ {
    57  		buf.Reset()
    58  		fmt.Fprintf(&buf, "%g", 5.23184)
    59  	}
    60  }
    61  
    62  func BenchmarkFmtManyArgs(b *testing.B) {
    63  	var buf bytes.Buffer
    64  	for i := 0; i < b.N; i++ {
    65  		buf.Reset()
    66  		fmt.Fprintf(&buf, "%2d/%2d/%2d %d:%d:%d %s %s\n", 3, 4, 5, 11, 12, 13, "hello", "world")
    67  	}
    68  }