我不知道为什么在我的情况下,JavaScript 和 Go 之间没有明显的性能差异。

huangapple go评论103阅读模式
英文:

I don't know why there's no big performance difference between JS and Go in my case

问题

我刚刚在欧拉计划中解决了#12问题,涉及到约数和三角数,分别用Go和JavaScript实现了一次。我以为Go的代码会比JS的代码快得多,因为JS在运行时执行代码,而Go是构建代码。我知道构建代码并不直接意味着良好的性能,但结果是,即使在不同条件下,JS的代码有时也比Go的代码快。

我使用的代码如下。

https://gist.github.com/noraesae/675e40477e177f9f63f9

我在我的MacBook上进行了测试,规格如下。

处理器:2.6 GHz 英特尔 Core i5
内存:8 GB 1600 MHz DDR3

我使用以下命令运行它们。

$ #js
$ node euler12.js

$ #go
$ go build euler12.go
$ ./euler12

我做错了什么吗?如果没有,为什么它们之间没有区别?我还用Python进行了测试,Go和Python之间有很大的差距。提前感谢。

英文:

I've just solved the #12 problem in Project Euler, about divisor and triangle number, once in Go and once in JavaScript. I thought the code in Go would be much faster than one in JS, cause JS runs the code in runtime and Go builds the code. I know that building the code doesn't directly mean good performance, but the result was that even the code in JS was sometimes faster in different conditions.

The code I used is like below.

https://gist.github.com/noraesae/675e40477e177f9f63f9

I tested them in my macbook, and spec is like below.

Processor: 2.6 GHz Intel Core i5
Memory: 8 GB 1600 MHz DDR3

I ran them with commands below.

> $ #js
> $ node euler12.js
>
> $ #go
> $ go build euler12.go
> $ ./euler12

Did I do something wrong? If not, why there's no difference between them? I also tested with Python and there was a big gap between Go and Python. Thanks in advance.

答案1

得分: 1

很有趣,在我的机器上(macbookpro 2013年底,i7 2.3ghz),JS代码比Go代码快得多:

JS:

time node test.js
842161320

real    0m4.437s
user    0m4.900s
sys     0m0.150s

Go:

time GOMAXPROCS=8 ./test
842161320

real    0m7.345s
user    0m7.470s
sys     0m0.010s

然而,在对Go代码进行快速优化之后:

Go:

time GOMAXPROCS=8 ./test
842161320

real    0m1.760s
user    0m11.610s
sys     0m0.230s

我所说的快速优化(非常简单):并行化计算:

package main

import (
        "fmt"
        "os"
        "runtime"
)

func numberOfDevisor(n int64) int64 {
        var result int64

        var i int64 = 1
        for true {
                if n%i == 0 {
                        opposite := n / i
                        if opposite == i {
                                result++
                                return result
                        } else if opposite > i {
                                result += 2
                        } else {
                                return result
                        }
                }
                i++
        }
        return result
}

func main() {
        var acc int64
        var i int64 = 1

        maxRoutines := runtime.NumCPU()
        c := make(chan struct{}, maxRoutines)
        for i := 0; i < maxRoutines; i++ {
                c <- struct{}{}
        }
        for true {
                <-c
                acc += i
                go func(acc int64) {
                        defer func() { c <- struct{}{} }()
                        if numberOfDevisor(acc) > 1000 {
                                fmt.Println(acc)
                                os.Exit(0)
                        }
                }(acc)
                i++
        }
}
英文:

Pretty interesting, on my machine (macbookpro late 2013, i7 2.3ghz), the JS code is much faster than the Go one:

JS:

time node test.js
842161320

real    0m4.437s
user    0m4.900s
sys     0m0.150s

Go:

time GOMAXPROCS=8 ./test
842161320

real    0m7.345s
user    0m7.470s
sys     0m0.010s

However, after a quick optimization of the Go code:

Go:

time GOMAXPROCS=8 ./test
842161320

real    0m1.760s
user    0m11.610s
sys     0m0.230s

The quick optimization I am talking about (very naive): parallelize your computation:

package main

import (
        &quot;fmt&quot;
        &quot;os&quot;
        &quot;runtime&quot;
)

func numberOfDevisor(n int64) int64 {
        var result int64

        var i int64 = 1
        for true {
                if n%i == 0 {
                        opposite := n / i
                        if opposite == i {
                                result++
                                return result
                        } else if opposite &gt; i {
                                result += 2
                        } else {
                                return result
                        }
                }
                i++
        }
        return result
}

func main() {
        var acc int64
        var i int64 = 1

        maxRoutines := runtime.NumCPU()
        c := make(chan struct{}, maxRoutines)
        for i := 0; i &lt; maxRoutines; i++ {
                c &lt;- struct{}{}
        }
        for true {
                &lt;-c
                acc += i
                go func(acc int64) {
                        defer func() { c &lt;- struct{}{} }()
                        if numberOfDevisor(acc) &gt; 1000 {
                                fmt.Println(acc)
                                os.Exit(0)
                        }
                }(acc)
                i++
        }
}

huangapple
  • 本文由 发表于 2014年10月29日 22:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/26632627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定