第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 與 Java 的速度

Golang 與 Java 的速度

湖上湖 2021-07-10 14:01:51
我用 Java 編寫了一個程序,用 Go 編寫了一個等效程序。我的 Java 程序執(zhí)行時間約為 5.95 秒,而 Go 程序執(zhí)行時間約為 41.675789791 秒。雖然 Go 的速度與 C 或 C++ 相當(dāng),因為它像 C 一樣編譯,但為什么會存在如此大的性能差異?程序如下:圍棋程序package mainimport (    "math"    "fmt"    "time")func main() {    fmt.Printf("vvalue is %v", testFun(10, 16666611, 1000000000))}func fun(x float64) float64 {    return math.Pow(x, 2) - x}func testFun(first float64, second float64, times int) float64 {    var i = 0    var result float64 = 0    var dx float64    dx = (second - first) / float64(times)    for ; i < times; i++ {        result += fun(first + float64(i) * dx)    }    return result * dx}   Java程序public class Test {public static void main(String[] args) {    Test test = new Test();    double re = test.testFun(10, 16666611, 1000000000);    System.out.println(re);}private double testFun(double first, double second, long times) {    int i = 0;    double result = 0;    double dx = (second - first) / times;    for (; i < times; i++) {        result += fun(first + i * dx);    }    return result * dx;}private double fun(double v) {    return Math.pow(v, 2) - v;}}
查看完整描述

3 回答

?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊

我建議math.Pow(x,y)在 Go 中實際上沒有x^yywhile 的整數(shù)值進(jìn)行任何優(yōu)化,而Math.pow(x,y)只是x*xfor y==2。至少在兩個程序pow中用 simple替換時x*x,Java 為 6.5 秒,而 Go 為 1.4 秒。使用pow代替我仍然得到6.5秒為Java而29.4秒了圍棋。


查看完整回答
反對 回復(fù) 2021-07-14
?
喵喵時光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊

不要從其他語言翻譯。用 Go 編寫程序的 Go 版本。例如x*x - x,


package main


import (

    "fmt"

    "math"

    "time"

)


func main() {

    start := time.Now()

    v := testFun(10, 16666611, 1000000000)

    since := time.Since(start)

    fmt.Printf("value is %v\ntime is %v\n", v, since)

}


func fun(x float64) float64 {

    return x*x - x

}


func testFun(first float64, second float64, times int) float64 {

    sum := float64(0)

    dx := (second - first) / float64(times)

    for i := 0; i < times; i++ {

        sum += fun(first + float64(i)*dx)

    }

    return sum * dx

}

輸出:


$ go version

go version devel +5c11480631 Fri Aug 10 20:02:31 2018 +0000 linux/amd64

$ go run speed.go

value is 1.543194272428967e+21

time is 1.011965238s

$

你得到什么結(jié)果?


查看完整回答
反對 回復(fù) 2021-07-14
  • 3 回答
  • 0 關(guān)注
  • 289 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號