GOARCH amd64 vs 386

965 views
Skip to first unread message

danio...@gmail.com

unread,
Aug 2, 2015, 3:26:55 PM8/2/15
to golang-nuts
Hello Gophers

So it is late in the night here, and I was experimenting with the updated cross compilation functions of Go 1.5 (1.5beta2).
I wrote a triviel program, that calculates all the primes within a range that is defined with a flag parameter.
The source is as following.

package main

import (
    "flag"
    "fmt"
    "time"
)

func isPrime(number int) bool {
    if number%2 == 0 {
        return number == 2
    }
    for i := 3; (i * i) <= number; i += 2 {
        if number%i == 0 {
            return false
        }
    }
    return number != 1
}

func main() {
    iteration := flag.Int("m", 0, "loop length")
    flag.Parse()
    start := time.Now()
    for i := 0; i < *iteration; i++ {
        isPrime(i)
    }
    end := time.Since(start)
    fmt.Println(end.String())
}

I compiled this in two different versions, one for 32 bit (386) and one for 64 bit (amd64).

Then I ran them to see the performance difference, and I was quite surprised.


C:\Users\Daniel\Desktop>prime_32.exe -m=50000000
34.560019s



C:\Users\Daniel\Desktop>prime_64.exe -m=50000000
1m28.9043318s


Am I missing something (it is kind of late here) or is it normal for 32 bit applications to perform better?
I ran this test on both Windows and Linux, and the difference is the same.
 

Sokolov Yura

unread,
Aug 2, 2015, 10:38:12 PM8/2/15
to golang-nuts
Integer division (and modulo) is much slower with 64bit.
I expect, you'll see the same result with C and Java.

воскресенье, 2 августа 2015 г., 22:26:55 UTC+3 пользователь Daniel K написал:

Carlos Castillo

unread,
Aug 3, 2015, 5:45:53 AM8/3/15
to golang-nuts
On 32-bit architectures int is 32-bits (max value 2147483647) which overflows in your loop and thus produces a negative number, which exits early. Change the type from int to int64 to perform the same (and correct) calculations on a 32-bit machine as a 64-bit one.

Daniel K

unread,
Aug 3, 2015, 5:49:16 AM8/3/15
to golang-nuts
I have not tried with C or Java, but I have tried with C#, which does not have the same issue.
32bit and 64bit C# version, ran equal to each other.

Daniel K

unread,
Aug 3, 2015, 5:55:20 AM8/3/15
to golang-nuts
I forgot to write about my testing environment.
The tests were conducted on my machine:
I7 920
Windows 10 (64bit) and Linux Mint (64bit)

I don't think the value 50 million is enough to overflow a int32.

Carlos Castillo

unread,
Aug 3, 2015, 6:06:33 AM8/3/15
to Daniel K, golang-nuts
You are squaring the value in your isPrime loop. 50 million squared is 250 trillion (much larger than 2 billion), in fact any number greater than 46340 when squared will overflow. see: http://play.golang.org/p/V39tfQvOwm (note: the playground is 32-bit).

Your program should be written as follows to always do 64-bit math regardless of the value of GOARCH: http://play.golang.org/p/a_pujiwwzB 

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/A1tKxcpoRAs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel K

unread,
Aug 3, 2015, 6:30:17 AM8/3/15
to golang-nuts
Thanks, I corrected the overflow issue.
Reply all
Reply to author
Forward
0 new messages