type VectorInt struct {
data []int
size int
capacity int
}
func (this *VectorInt) Set(i int, v int) {
if i >= this.size {
panic("out of bound!")
}
this.data[i] = v
}
func (this *VectorInt) At(i int) int {
if i >= this.size {
panic("out of bound!")
}
return this.data[i]
}
func (this *VectorInt) Push(v int) {
if this.size < this.capacity {
this.data[this.size] = v
this.size++
return
}
this.data = append(this.data, v)
this.size++
this.capacity = len(this.data)
}
func (this *VectorInt) Length() int {
return this.size
}
func (this *VectorInt) Clear() {
this.size = 0
}
func NewVectorInt(capacity int) *VectorInt {
return &VectorInt{data: make([]int, capacity), size: 0, capacity: capacity}
}
//!!!BUG!!!
func BenchmarkVectorInt_Push(b *testing.B) {
b.N = 10000 * 10000
vec := NewVectorInt(b.N + 1)
for index := 0; index < b.N; index++ {
vec.Push(index)
}
}
func push(a []int, v int, index *int) {
if *index < len(a) {
a[*index] = v
*index = *index + 1
}
}
//1-2ns
func BenchmarkSliceAppend(b *testing.B) {
b.N = 10000 * 10000
s := make([]int, b.N+1)
i := 0
for index := 0; index < b.N; index++ {
push(s, index, &i)
}
}
here is my codes.
On Windows 7 64bit, go version go1.11.4 windows/amd64. the VectorInt.Push is very slow, it costs about 12ns.
but on Win10 about 2ns, Linux about 2ns
here is my codes.
On Windows 7 64bit, go version go1.11.4 windows/amd64. the VectorInt.Push is very slow, it costs about 12ns.
but on Win10 about 2ns, Linux about 2ns