いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

Goメモ-206 (sliceにデータを追加する際のパフォーマンス比較について)

概要

たまに聞かれるのと、その都度ベンチマーク書いているような気がしたので、以下にメモメモ。。。

package main

import (
    "strconv"
    "testing"
)

func BenchmarkSliceLen0Append(b *testing.B) {
    var (
        s []string
    )

    b.StartTimer()
    for i := 0; i < b.N; i++ {
        //lint:ignore SA4010 ok
        s = append(s, strconv.Itoa(i))
    }
    b.StopTimer()
}

func BenchmarkSliceLenN(b *testing.B) {
    var (
        s = make([]string, b.N)
    )

    b.StartTimer()
    for i := 0; i < b.N; i++ {
        s[i] = strconv.Itoa(i)
    }
    b.StopTimer()
}

func BenchmarkSliceLenNAppend(b *testing.B) {
    var (
        s = make([]string, 0, b.N)
    )

    b.StartTimer()
    for i := 0; i < b.N; i++ {
        //lint:ignore SA4010 ok
        s = append(s, strconv.Itoa(i))
    }
    b.StopTimer()
}

Taskfile.yml は以下。

version: '3'

tasks:
  default:
    cmds:
      - task: bench
      - task: bench
      - task: bench
  bench:
    cmds:
      - go test -bench .

実行すると以下のような感じです。

gitpod /workspace/try-golang (master) $ task -d examples/singleapp/slice_performance_tips/
task: [bench] go test -bench .
goos: linux
goarch: amd64
pkg: github.com/devlights/try-golang/examples/singleapp/slice_performance_tips
cpu: AMD EPYC 7B13
BenchmarkSliceLen0Append-16      8756838               138.3 ns/op
BenchmarkSliceLenN-16           35395413                31.94 ns/op
BenchmarkSliceLenNAppend-16     42205850                33.12 ns/op
PASS
ok      github.com/devlights/try-golang/examples/singleapp/slice_performance_tips       4.720s

task: [bench] go test -bench .
goos: linux
goarch: amd64
pkg: github.com/devlights/try-golang/examples/singleapp/slice_performance_tips
cpu: AMD EPYC 7B13
BenchmarkSliceLen0Append-16      8088543               146.9 ns/op
BenchmarkSliceLenN-16           29672313                37.46 ns/op
BenchmarkSliceLenNAppend-16     33028478                40.53 ns/op
PASS
ok      github.com/devlights/try-golang/examples/singleapp/slice_performance_tips       7.678s

task: [bench] go test -bench .
goos: linux
goarch: amd64
pkg: github.com/devlights/try-golang/examples/singleapp/slice_performance_tips
cpu: AMD EPYC 7B13
BenchmarkSliceLen0Append-16      9610222               128.2 ns/op
BenchmarkSliceLenN-16           34091638                35.78 ns/op
BenchmarkSliceLenNAppend-16     31482225                35.17 ns/op
PASS
ok      github.com/devlights/try-golang/examples/singleapp/slice_performance_tips       4.724s

基本的にちゃんとサイズを確保した上で使う方が当然パフォーマンスが良い。(当然ですが)


過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。