Using pprof to profile your code we get this:
(pprof) top5
30.01s of 30.02s total ( 100%)
Dropped 4 nodes (cum <= 0.15s)
flat flat% sum% cum cum%
30s 99.93% 99.93% 30s 99.93% main.sommeDiviseursPropres
0.01s 0.033% 100% 30.01s 100% main.main
0 0% 100% 30.01s 100% runtime.goexit
0 0% 100% 30.01s 100% runtime.main
(pprof) list
Total: 30.02s
ROUTINE ======================== main.main in
/home/morwar/repos/amigos/main.go
10ms 30.01s (flat, cum) 100% of Total
. . 46: os.Exit(2)
. . 47: }
. . 48:
. . 49: var res []intTuple
. . 50: for a := 2; a <= limite; a++ {
. 22.90s 51: b := sommeDiviseursPropres(a)
. 7.10s 52: if b > a && sommeDiviseursPropres(b) == a {
. . 53: res = append(res, intTuple{a, b})
. . 54: }
. . 55: }
10ms 10ms 56: fmt.Println(res)
. . 57:}
ROUTINE ======================== main.sommeDiviseursPropres in
/home/morwar/repos/amigos/main.go
30s 30s (flat, cum) 99.93% of Total
. . 13:
. . 14:type intTuple struct {
. . 15: a, b int
. . 16:}
. . 17:
10ms 10ms 18:func sommeDiviseursPropres(nb int) int {
10ms 10ms 19: res := 1
30ms 30ms 20: sqrt := int(math.Sqrt(float64(nb)))
1.69s 1.69s 21: for div := 2; div <= sqrt; div++ {
26.96s 26.96s 22: if nb%div == 0 {
380ms 380ms 23: res += div + (nb / div)
. . 24: }
. . 25: }
. . 26: if sqrt*sqrt == nb {
. . 27: return res - sqrt
. . 28: }
920ms 920ms 29: return res
. . 30:}
. . 31:
. . 32:func main() {
. . 33: go func() {
. . 34: log.Println(http.ListenAndServe(":8080", nil))
so it looks like the mod is taking a while.
Em sábado, 25 de fevereiro de 2017 23:38:19 UTC-3, Éric Jacoboni escreveu:
>
> Hi,
>
> I'm just trying to learn Go and, as i do with other languages, i first try
> to code some trivial algorithms.
> Here, i want to implement a program which displays the amical numbers
> couples given a limit. I'm using the very same algorithm i'm using in
> Python, Java, Haskell, Scala, Nim, etc.)
>
> My Go code is visible here : http://sprunge.us/bSgW
>
> When running the executable created with go build main.go on a MacBook Pro
> and with a limit of 1000000, the result is processed in 8,815 secs ($ time
> ./main 1000000)
> The same program written in Java gives 2.6 secs (and it counts the first
> load of the jvm...), Nim gives 2.5 secs, Scala gives 5 secs. Python (with
> pypy) gives 10 secs.
> I've not tried with C but i'm sure it would be even faster than the Java
> or Nim versions.
>
> So, given all my programs are using the same algorithm, why the Go version
> is so slow comparing to Java/Scala ? I was expecting Go to be faster than a
> language running on the jvm.
>
> Any hints?
>
>
>
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.