Repository: go
Issue: cmd/compile: poor performance when accessing slices for numerical work (multiplying vectors)
Labels: Performance

by **dean.w.schulze**:

<pre>Running the enclosed code the best runtime is 6.45 seconds.  The same code in Java (JDK
1.7) runs in 4.8 seconds, a serious performance hit.

For simple slice (go) / array (Java) access I would expect similar runtime performance.

Go and Java code is below.


Which operating system are you using?
go version go1.1.2 linux/amd64
This is a VM running on OSX host.  Similar results on Windows 7 64-bit

============================================================

package main

import (
    &quot;fmt&quot;
    &quot;time&quot;
)


func main() {
    
    sz := 100000
    sl1 := make([]int32, sz)

    for j := range sl1 {

        sl1[j] = int32(j)
    }

    sl2 := make([]int32, sz)

    for j := range sl2 {

        sl2[j] = int32(j*2)
    }

    multiplySlices(sl1, sl2)
    multiplySlices_(sl1, sl2)
    multiplySlices__(sl1, sl2)
}



func multiplySlices(sl1 []int32, sl2 []int32) {

    start := time.Now()
    var num int64 = 0
    for _, n1 := range sl1 {

        var n int64 = 0
        for _, n2 := range sl2 {

            n +=  int64(n1 * n2)
        }

        num += n
    }

    et := time.Since(start)
    fmt.Println(&quot;&quot;)
    fmt.Println(&quot;multiplySlices&quot;)
    fmt.Println(et)
    fmt.Println(num)
}


func multiplySlices_(sl1 []int32, sl2 []int32) {

    start := time.Now()
    var num int64 = 0
    for _, n1 := range sl1 {

        for _, n2 := range sl2 {

            num +=  int64(n1 * n2)
        }
    }

    et := time.Since(start)
    fmt.Println(&quot;&quot;)
    fmt.Println(&quot;multiplySlices_&quot;)
    fmt.Println(et)
    fmt.Println(num)
}


func multiplySlices__(sl1 []int32, sl2 []int32) {

    start := time.Now()
    var num int64 = 0
    for i := range sl1 {

        n := sl1[i]
        for j := range sl2 {

            num +=  int64(n * sl2[j])
        }
    }

    et := time.Since(start)
    fmt.Println(&quot;&quot;)
    fmt.Println(&quot;multiplySlices__&quot;)
    fmt.Println(et)
    fmt.Println(num)
}


=======================================================================


package arraytest;

import java.util.Date;

public class Driver {
    public static void main(String[] args) {
        int sz = 100000;
        int[] arr1 = new int[sz];
        int[] arr2 = new int[sz];
        for (int i = 0; i &lt; sz; i++) {
            arr1[i] = i;
            arr2[i] = i * 2;
        }
        
        mutiplyArrays(arr1, arr2);
        mutiplyArrays_(arr1, arr2);
        
    }

    private static void mutiplyArrays(int[] arr1, int[] arr2) {
        
        int sz = arr1.length;
        Date start = new Date();
        long num = 0;
        for (int i = 0; i &lt; sz; i++) {
            int n = arr1[i];
            long acc = 0;
            for (int j = 0; j &lt; sz; j++) {
                acc += n * arr2[j];
            }
            
            num += acc;
        }
        Date end = new Date();
        long l = end.getTime() - start.getTime();
        System.out.println(l + &quot; ms.&quot;);
        System.out.println(num);
    }
    

    private static void mutiplyArrays_(int[] arr1, int[] arr2) {
        
        int sz = arr1.length;
        Date start = new Date();
        long num = 0;
        for (int i = 0; i &lt; sz; i++) {
            int n = arr1[i];
            for (int j = 0; j &lt; sz; j++) {
                num += n * arr2[j];
            }
        }
        Date end = new Date();
        long l = end.getTime() - start.getTime();
        System.out.println(l + &quot; ms.&quot;);
        System.out.println(num);
    }

}</pre>