Help us understand the problem. What is going on with this article?

golangパフォーマンス2: slice vs array

More than 3 years have passed since last update.

テスト環境: go version go1.7.3 windows/amd64

ファイル構造:
rootファイル/02arraySlice.go
rootファイル/02arraySlice_test.go (効率テスト用)

02arraySlice.go

package go1_6

const capacity = 1024

//配列操作arrayはsliceにより早い
func array() [capacity]int {//配列処理テスト関数
    var d [capacity]int

    for i := 0; i < len(d); i++ {
        d[i] = 1
    }

    return d
}

func slice() []int {//slice処理テスト関数
    d := make([]int, capacity)

    for i := 0; i < len(d); i++ {
        d[i] = 1
    }

    return d
}

//パラメータとして渡す場合はsliceほうが早い (arrayはメモリ中にコピーされる)
func inArray(array [capacity]int) {//配列として関数パラメータ効率テスト

}

func inArray2(array *[capacity]int) {//配列ポインタとして関数パラメータ効率テスト

}

func inSlice(slice []int) {//sliceとして関数パラメータ効率テスト

}

//reArrayにより早い
func reSlice() []byte {//return slice場合効率テスト
    return []byte{1, 2, 3, 4}
}

func reArray() [4]byte {//return array場合効率テスト
    return [4]byte{1, 2, 3, 4}
}

02arraySlice_test.go

package go1_6

import (
    "testing"
)

func BenchmarkArray(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = array()
    }
}

func BenchmarkSlice(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = slice()
    }

}

func BenchmarkInArray(b *testing.B) {
    arr := array()
    for i := 0; i < b.N; i++ {
        inArray(arr)
    }
}

func BenchmarkInArray2(b *testing.B) {
    b.StopTimer()
    arr := array()
    b.StartTimer()

    parr := &arr
    for i := 0; i < b.N; i++ {
        inArray2(parr)
    }
}

func BenchmarkInSlice(b *testing.B) {
    b.StopTimer()
    sli := slice()
    b.StartTimer()

    for i := 0; i < b.N; i++ {
        inSlice(sli)
    }

}

func BenchmarkReArray(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = reArray()
    }
}

func BenchmarkReSlice(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = reSlice()
    }
}

テスト結果:
image

ポイント:
通常処理場合は配列がSliceにより早いですが、
配列は関数パラメータと関数戻し値として利用された場合は
メモリ中にコピーされるため逆に効率が落ちます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした