基准测试 用benchmark进行性能测试 字符串拼接 接口类型

 

 

入参为接口类型对函数执行效率的影响

const test_host = "https://filehelper.weixin.qq.com/"

func ImgUrl_interface(i interface{}) (string, []string) {
    switch i.(type) {
    default:
        return "", nil
    case string:
        t := i.(string)
        if t == "" {
            return "", nil
        }
        return utils.ConcatStr(test_host, t), nil
    case []string:
        imgs := i.([]string)
        ans := []string{}
        for j := range imgs {
            ans = append(ans, utils.ConcatStr(test_host, imgs[j]))
        }
        return "", ans
    }
}

func ImgUrl_str(i string) string {
    if i == "" {
        return ""
    }
    return utils.ConcatStr(test_host, i)
}

func ImgUrl_slice(i []string) []string {
    if i == nil {
        return nil
    }
    ans := []string{}
    for j := range i {
        ans = append(ans, utils.ConcatStr(test_host, i[j]))
    }
    return ans
}

 

 


func BenchmarkImgUrl_interface(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ImgUrl_interface("a.png")
    }
}

func BenchmarkImgUrl_str(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ImgUrl_str("a.png")
    }
}

func BenchmarkImgUrl_interface_slice(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ImgUrl_interface([]string{"a.png", "b.png", "a1.png", "b1.png", "a2.png", "b2.png", "a3.png", "b3.png"})
    }
}

func BenchmarkImgUrl_slice(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ImgUrl_slice([]string{"a.png", "b.png", "a1.png", "b1.png", "a2.png", "b2.png", "a3.png", "b3.png"})
    }
}
 
func ConcatStr(l ...string) string {
    b := strings.Builder{}
    for i := range l {
        b.WriteString(l[i])
    }
    return b.String()
}
 

go test -run=XXX -bench=.
goos: windows
goarch: amd64
pkg: WebSvc/internal/presentationLayer
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkImgUrl_interface-12 25598641 47.23 ns/op
BenchmarkImgUrl_str-12 25240561 45.78 ns/op
BenchmarkImgUrl_interface_slice-12 1725375 737.5 ns/op
BenchmarkImgUrl_slice-12 1620464 700.1 ns/op
PASS
ok WebSvc/internal/presentationLayer 8.077s

 

 

 

 

Benchmark functions in Golang - Golang Docs https://golangdocs.com/benchmark-functions-in-golang

How to write benchmarks in Go | Dave Cheney https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go

 

import (
    "bytes"
    "strings"
)
func ConcatStrV0(l ...string) string {
    var buf bytes.Buffer
    for i := range l {
        buf.WriteString(l[i])
    }
    // To build strings more efficiently, see the strings.Builder type.
    //  Go\src\bytes\buffer.go
    return buf.String()
}

func ConcatStr(l ...string) string {
    b := strings.Builder{}
    for i := range l {
        b.WriteString(l[i])
    }
    return b.String()
}
 
性能测试
func BenchmarkConcatStr(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ConcatStr([]string{"1", "a", ""}...)
    }
}

func BenchmarkConcatStrV0(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ConcatStrV0([]string{"1", "a", ""}...)
    }
}

var test_l = []string{"1", "a", "", "b := SelectBuilder{builder: &strings.Builder{}} b := SelectBuilder{builder: &strings.Builder{}} b := SelectBuilder{builder: &strings.Builder{}} b := SelectBuilder{builder: &strings.Builder{}} b := SelectBuilder{builder: &strings.Builder{}} b := SelectBuilder{builder: &strings.Builder{}}"}

func BenchmarkConcatStr_1(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ConcatStr(test_l...)
    }
}

func BenchmarkConcatStrV0_1(b *testing.B) {
    // run the  function b.N times
    for n := 0; n < b.N; n++ {
        ConcatStrV0(test_l...)
    }
}
 
在 WebSvc/utils目录执行

go test -run=XXX -bench=.
goos: windows
goarch: amd64
pkg: WebSvc/utils
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkConcatStr-12 34178004 30.38 ns/op
BenchmarkConcatStrV0-12 16447819 62.20 ns/op
BenchmarkConcatStr_1-12 9675211 110.0 ns/op
BenchmarkConcatStrV0_1-12 5310603 224.8 ns/op
PASS
ok WebSvc/utils 4.824s

 

 

小结:

1、

内存分配的基准测试
go test -run=^$ -bench=. bufio

% go test -run=XXX -bench=. -cpuprofile=c.p bytes
% go tool pprof c.p
 
 

基准测试 

package main

 
type S [12]int64
var sX = make([]S, 1000)
var sY = make([]S, 1000)
var sZ = make([]S, 1000)
var sumX, sumY, sumZ int64

func Loop() {
 		sumX = 0
		for j := 0; j < len(sX); j++ {
			sumX += sX[j][0]
		}
 }

func Range_OneIterVar() {
 		sumY = 0
		for j := range sY {
			sumY += sY[j][0]
		}
 }

func Range_TwoIterVar() {
 		sumZ = 0
		for _, v := range sZ {
			sumZ += v[0]
		}
 }

  

package main

import "testing"

 
func Benchmark_Loop(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sumX = 0
		for j := 0; j < len(sX); j++ {
			sumX += sX[j][0]
		}
	}
}

func Benchmark_Range_OneIterVar(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sumY = 0
		for j := range sY {
			sumY += sY[j][0]
		}
	}
}

func Benchmark_Range_TwoIterVar(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sumZ = 0
		for _, v := range sZ {
			sumZ += v[0]
		}
	}
}

 

Running tool: C:\Program Files\Go\bin\go.exe test -benchmem -run=^$ -coverprofile=C:\Users\XLE~1.FEN\AppData\Local\Temp\vscode-gos5y16I\go-code-cover -bench . A

goos: windows
goarch: amd64
pkg: A
cpu: Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
Benchmark_Loop-4                  720507        1722 ns/op         0 B/op        0 allocs/op
Benchmark_Range_OneIterVar-4      725149        1843 ns/op         0 B/op        0 allocs/op
Benchmark_Range_TwoIterVar-4      300584        4271 ns/op         0 B/op        0 allocs/op
PASS
coverage: 0.0% of statements
ok    A 4.179s

 1. Go 性能调优之 —— 基准测试 - SegmentFault 思否 https://segmentfault.com/a/1190000016354758

 

posted @ 2022-03-22 11:17  papering  阅读(79)  评论(0编辑  收藏  举报