【Go】スライスに要素を追加する処理のベンチマークテスト比較

更新日:2025/3/13/(木) 12:38

タグ:Go

概要

スライスに要素を追加するappendインデックスを直接指定する方法のパフォーマンスが気になったので、ベンチマークテストの比較を行ってみた。

環境

計測方法

サンプルコード

package main import "testing" const size = 10000000 // ベンチマーク関数: append の場合 func BenchmarkAppend(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { var slice []int for j := 0; j < size; j++ { slice = append(slice, j) } } } // ベンチマーク関数: slice[i] = int の場合 func BenchmarkIndexAssignment(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { slice := make([]int, size) for j := 0; j < size; j++ { slice[j] = j } } }

計測結果

goos: darwin goarch: arm64 pkg: performance cpu: Apple M1 BenchmarkAppend-8 40 29059970 ns/op 492000713 B/op 51 allocs/op BenchmarkIndexAssignment-8 264 4503033 ns/op 80003085 B/op 1 allocs/op PASS ok performance 3.679s

※ベンチマークテストでは内部で複数回の計測を実施しており、テストを数回実行しても結果のばらつきはほとんどなかったので1回分のみを記載

append(BenchmarkAppend-8)

インデックス指定(BenchmarkIndexAssignment-8)

比較

appendインデックス指定
実行回数40回264回
平均実行時間29,059,970 ns/op(約29.1ms/1回)4,503,033 ns/op(約4.5ms/1回)
メモリ使用量492,000,713 B/op(約492MB/1回)80,003,085 B/op(約80MB/1回)
メモリアロケーション回数51回1回

結論