0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Goでベンチマークを取る

Posted at

Goでのベンチマークの取り方。
https://golang.org/pkg/testing/

package main

import (
	"fmt"
	"testing"
)

func makeSlice1(n int) []string {
	var r []string
	for i := 0; i < n; i++ {
		r = append(r, fmt.Sprintf("%03d hello", i))
	}
	return r
}

func makeSlice2(n int) []string {
	r := make([]string, n)
	for i := 0; i < n; i++ {
		r[i] = fmt.Sprintf("%03d hello", i)
	}
	return r
}

func BenchmarkMakeSlice1(b *testing.B) {
	b.ResetTimer()
	makeSlice1(b.N)
}

func BenchmarkMakeSlice2(b *testing.B) {
	b.ResetTimer()
	makeSlice2(b.N)
}

$ go test -bench .
goos: darwin
goarch: amd64
BenchmarkMakeSlice1-4   	 5000000	       285 ns/op
BenchmarkMakeSlice2-4   	10000000	       144 ns/op
PASS
ok  	_/go/to/path	3.383s
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?