Try these versions: https://play.golang.org/p/HF6k0ovyoW

Changes:

1. Numbers big enough that the multiplies take some time.

2. Two styles of synchronization and work distribution.

Best case in this configuration is 3.9151x speedup on 4 core notebook:

$ go test -bench=.
testing: warning: no tests to run
BenchmarkLoop-8               10         115728469 ns/op
BenchmarkParLoop2-8           50          29559001 ns/op
BenchmarkParLoop3-8           50          31231335 ns/op

Michael Jones
michael.jo...@gmail.com

> On Jun 28, 2016, at 7:05 PM, Michael Jones <michael.jo...@gmail.com> wrote:
> 
> You should expect linear speedup. But you’ll need to work fot it. Problems in 
> this version:
> 
> 1. 10,000 parallel workers! Not very cache friendly.
> 2. You don’t wait for the goroutines to finish, so not very meaningful.
> 
> Look for the blog posts and videos about concurrently patterns. What you 
> likely want is a set of worker process (~ncpus or 2x that) doing the work and 
> a way to know when the work is done. 
> 
> Michael Jones
> michael.jo...@gmail.com <mailto:michael.jo...@gmail.com>
>> On Jun 28, 2016, at 5:01 PM, guyz...@gmail.com <mailto:guyz...@gmail.com> 
>> wrote:
>> 
>> Hi,
>> 
>> I'm attempting to optimize pair-wise operations over arrays of big integers 
>> ('vectorize'). Currently, I tried creating two versions of a point-wise 
>> multiplication function - one that is a simple serial for-loop, and another 
>> that uses go-routines to create a parallel (or more correctly - concurrent) 
>> for loop. The code is below along with the results, showing that the 
>> parallel for loop is actually ~10x slower.
>> 
>> My question is - is there a better way to achieve this, or is a naive serial 
>> for-loop the best one can hope for?
>> 
>> func BenchmarkLoop(b *testing.B) {
>>      var x, y, z [10000]big.Int
>>      for i := 0; i < 10000; i++ {
>>              x[i].SetInt64(892745680)
>>              y[i].SetInt64(353322197)
>>      }
>> 
>>      for n := 0; n < b.N; n++ {
>>              for i := 0; i < 10000; i++ {
>>                      z[i].Mul(&x[i], &y[i])
>>              }
>>      }
>> }
>> 
>> func BenchmarkParLoop1(b *testing.B) {
>>      var x, y, z [10000]big.Int
>>      runtime.GOMAXPROCS(4) // Tried playing with this value from 1-8
>>      for i := 0; i < 10000; i++ {
>>              x[i].SetInt64(892745680)
>>              y[i].SetInt64(353322197)
>>      }
>> 
>>      for n := 0; n < b.N; n++ {
>>              for i := 0; i < 10000; i++ {
>>                      go func(i int) {
>>                              z[i].Mul(&x[i], &y[i])
>>                      }(i)
>>              }
>>      }
>> }
>> 
>> Benchmarks: 
>> BenchmarkLoop-8               5000       350033 ns/op
>> 
>> BenchmarkParLoop1-8              500   2352201 ns/op
>> 
>> 
>> 
>> 
>> 
>> Thanks!
>> 
>> 
>> -- 
>> 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 
>> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 

-- 
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