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

> On Jun 28, 2016, at 5:01 PM, 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