dping1968 2019-03-10 06:38
浏览 40

带有和不带有空白标识符的Golang中的计时

I am playing around with some elementary timing in Go and have a question. I want to time Golang in taking the square root of each element of an array but I get two somewhat different answers whether or not I keep the output. Here is the my first version:

package main

import ("fmt"
        "time" 
        "math"
        "math/rand"
)

// A random array of integers 
func randomArray(max int ,len int) []int {
    a := make([]int, len)
    for i := 0; i <= len-1; i++ {
        a[i] = rand.Intn(max)
    }
    return a
}

// Taking square root of each element in array
func sqrt_array(arr [] int) [] float64 {
    var len_arr = len(arr)
    a := make([]float64, len_arr)
    for i, v := range arr {
    a[i] = math.Sqrt(float64(v))
    }
    return a
}


func main() {

    arr := randomArray(100, 10e6)
    sqrt := make([]float64, len(arr))
    start := time.Now()
    sqrt = sqrt_array(arr)
    end := time.Now()

    fmt.Println("time taken: ", end.Sub(start), sqrt[0])
}

gives on average around 36ms:

time taken:  36.542019ms 9

Now when I replace the output "sqrt" with the blank identifier, I get something much slower. Specifically, I replace main() with

func main() {

    arr := randomArray(100, 10e6)
    // sqrt := make([]float64, len(arr))
    start := time.Now()
    _ = sqrt_array(arr)
    end := time.Now()

    fmt.Println("time taken: ", end.Sub(start))
}

and get on average something like 92ms

time taken:  92.121481ms

Would someone be able to explain what is happening? I feel that if I understood I might learn something about Go.

Incidentally I find that the same computation in Python was around 20ms if broadcasted and several hundred ms if in a loop.

  • 写回答

2条回答 默认 最新

  • duanjiao7440 2019-03-10 08:17
    关注

    If you want the same results, do the same thing! Obviously, allocations are not going to be free! Don't comment out an allocation!

    For example,

    Output:

    time taken:  77.726729ms 9
    time taken:  78.213242ms 0
    

    main.go:

    package main
    
    import (
        "fmt"
        "math"
        "math/rand"
        "testing"
        "time"
    )
    
    // A random array of integers
    func randomArray(max int, len int) []int {
        a := make([]int, len)
        for i := 0; i <= len-1; i++ {
            a[i] = rand.Intn(max)
        }
        return a
    }
    
    // Taking square root of each element in array
    func sqrt_array(arr []int) []float64 {
        var len_arr = len(arr)
        a := make([]float64, len_arr)
        for i, v := range arr {
            a[i] = math.Sqrt(float64(v))
        }
        return a
    }
    
    func main1() {
    
        arr := randomArray(100, 10e6)
        sqrt := make([]float64, len(arr))
        start := time.Now()
        sqrt = sqrt_array(arr)
        end := time.Now()
    
        fmt.Println("time taken: ", end.Sub(start), sqrt[0])
    }
    
    func main2() {
    
        arr := randomArray(100, 10e6)
        // sqrt := make([]float64, len(arr))
        sqrt := make([]float64, len(arr))
        start := time.Now()
        _ = sqrt_array(arr)
        end := time.Now()
    
        fmt.Println("time taken: ", end.Sub(start), sqrt[0])
    }
    
    func main() {
        main1()
        main2()
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探