I believe this is simply due to the 64 bits integer division.

1. Contrary to Java or most C implementations int is 64 bits with Go on 
AMD64, and not 32 bits. Integer division on 64 bits is more expensive than 
for 32 bits.
2. When array_length is known at compile time, the compiler can replace the 
integer division done for % with cheaper operations.

Regards,
Didier.

Le jeudi 25 mars 2021 à 20:02:52 UTC+1, Isaac Gouy a écrit :

> (
> Yeah, it's a tiny tiny program from someone's language comparison 
> <http://www.hildstrom.com/projects/langcomp/index.html#:~:text=Results%20Table%20%20%20%20%20%20,%20%20225.9%20%2016%20more%20rows%20>
>  
> but this is a hugely bigger slowdown than gcc or java?
>
> The code change is compile time constant —
>     array_length int = 100000000    
>
> — changed to run time value —
>     array_length int = 1000000 * iterations 
> )
>
>
> package main
> import "os"
> import "fmt"
> import "strconv"
> func main() {
>     var (
>        element, iteration, iterations, innerloop int
>        sum float64
>     )
>
>     if len(os.Args) > 1 {
>         iterations,_ = strconv.Atoi(os.Args[1])
>     }
>     fmt.Printf("iterations %d\n", iterations)
>     
>     var (
>         array_length int = 100000000    
>         array []float64 = make([]float64, array_length)
>     )    
>     
>     for element = 0; element < array_length; element++ {
>         array[element] = float64(element)
>     }
>     for iteration = 0; iteration < iterations; iteration++ {
>         for innerloop = 0; innerloop < 1000000000; innerloop++ {
>             sum += array[(iteration + innerloop) % array_length]
>         }
>     }
>     fmt.Printf("sum %E\n", sum)
>     array = nil
> }
>
> $ /opt/src/go1.16/go/bin/go build -o out test.go
> $ time ./out 100
> iterations 100
> sum 5.000000E+18
>
> real    3m3.225s
>
> ====
>
> package main
> import "os"
> import "fmt"
> import "strconv"
> func main() {
>     var (
>        element, iteration, iterations, innerloop int
>        sum float64
>     )
>
>     if len(os.Args) > 1 {
>         iterations,_ = strconv.Atoi(os.Args[1])
>     }
>     fmt.Printf("iterations %d\n", iterations)
>     
>     var (
>         array_length int = 1000000 * iterations    
>         array []float64 = make([]float64, array_length)
>     )    
>     
>     for element = 0; element < array_length; element++ {
>         array[element] = float64(element)
>     }
>     for iteration = 0; iteration < iterations; iteration++ {
>         for innerloop = 0; innerloop < 1000000000; innerloop++ {
>             sum += array[(iteration + innerloop) % array_length]
>         }
>     }
>     fmt.Printf("sum %E\n", sum)
>     array = nil
> }
>
> $ /opt/src/go1.16/go/bin/go build -o out test.go
> $ time ./out 100
> iterations 100
> sum 5.000000E+18
>
> real    18m20.737s
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/f235e967-1fb7-44d7-afa5-e0dd16d87798n%40googlegroups.com.

Reply via email to