On Tuesday, June 27, 2017 at 6:20:42 PM UTC-4, peterGo wrote:
>
> T L,
>
> For equivalency, why did you write,
>
> func Benchmark_CloneWithCopy(b *testing.B) {
>     for i := 0; i < b.N; i++ {
>         t := make([]Element, N)
>         copy(t, x)
>         y = t
>     }
> }
>
> instaed of
>
> func Benchmark_CloneWithCopy(b *testing.B) {
>     for i := 0; i < b.N; i++ {
>         y = make([]Element, N)
>         copy(y, x)
>     }
> }
>
> ?
>

The two have not big difference.
 

>
> Peter
>
> On Tuesday, June 27, 2017 at 5:59:25 PM UTC-4, peterGo wrote:
>>
>> T L,
>>
>> Why didn't you show us the output from 
>>
>> $ go tool compile -m -m clone_test.go
>>
>> ?
>>
>> Peter
>>
>> On Tuesday, June 27, 2017 at 11:33:30 AM UTC-4, T L wrote:
>>>
>>> Ok, I think there are two reasons why the copy version is slower:
>>> 1. the copy version initializes each element twice actutally.
>>> 2. alloc a new slice by using make is slow than by using append. But why 
>>> this?
>>>
>>> package main
>>>
>>> import (
>>>     "testing"
>>> )
>>>
>>> const N = 1024 * 1024 // number of elements
>>> type Element int64
>>> var x = make([]Element, N)
>>> var y []Element
>>>
>>> func Benchmark_AllocWithMake(b *testing.B) {
>>>     for i := 0; i < b.N; i++ {
>>>         y = make([]Element, N)
>>>     }
>>> }
>>>
>>> func Benchmark_AllocWithAppend(b *testing.B) {
>>>     for i := 0; i < b.N; i++ {
>>>         y = append([]Element(nil), x...)
>>>     }
>>> }
>>>
>>> func Benchmark_CloneWithCopy(b *testing.B) {
>>>     for i := 0; i < b.N; i++ {
>>>         t := make([]Element, N)
>>>         copy(t, x)
>>>         y = t
>>>     }
>>> }
>>>
>>> func Benchmark_CloneWithAppend(b *testing.B) {
>>>     for i := 0; i < b.N; i++ {
>>>         y = append([]Element(nil), x...)
>>>     }
>>> }
>>>
>>> Benchmark_AllocWithMake-4             1000       1223666 ns/op
>>> Benchmark_AllocWithAppend-4           2000        725361 ns/op
>>> Benchmark_CloneWithCopy-4             1000       1813321 ns/op
>>> Benchmark_CloneWithAppend-4           2000        735011 ns/op
>>>
>>> On Tuesday, June 27, 2017 at 10:58:28 PM UTC+8, T L wrote:
>>>>
>>>>
>>>> package main
>>>>
>>>> import (
>>>>     "testing"
>>>> )
>>>>
>>>> const N = 1024 * 1024 // number of elements
>>>> type Element int64
>>>> var x = make([]Element, N)
>>>> var y []Element
>>>>
>>>>
>>>> func Benchmark_CloneWithAppend(b *testing.B) {
>>>>     for i := 0; i < b.N; i++ {
>>>>         y = append([]Element(nil), x...)
>>>>     }
>>>> }
>>>>
>>>> func Benchmark_CloneWithCopy(b *testing.B) {
>>>>     b.ResetTimer()
>>>>     for i := 0; i < b.N; i++ {
>>>>         t := make([]Element, N)
>>>>         copy(t, x)
>>>>         y = t
>>>>     }
>>>> }
>>>>
>>>> /*
>>>> $ go test -bench=.
>>>> Benchmark_CloneWithAppend-4           2000        738281 ns/op
>>>> Benchmark_CloneWithCopy-4             1000       1822462 ns/op
>>>> */
>>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to