GolangNote

Golang笔记

Golang 相加的性能测试

Permalink

对 Golang 的加法做简单测试,使用不同的方式,看看其性能有多大差别

Golang 相加的性能测试

对两种不同形式的相加方法测试:

plaintext: 相加
1
2
i = i + 1
i += 1

测试代码:

Go: 测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import "fmt"
import "time"

func main() {

    var count int64
    var i int64

    t1 := time.Now()
    count = 0
    for i = 0; i < 9000000000; i++ {
        count += i
    }
    t2 := time.Now()
    fmt.Printf("cost:%d,count:%d\n", t2.Sub(t1), count)

    //

    t1 = time.Now()
    count = 0
    for i = 0; i < 9000000000; i++ {
        count = count + i
    }
    t2 = time.Now()
    fmt.Printf("cost:%d,count:%d\n", t2.Sub(t1), count)

}

输出:

plaintext: 输出
1
2
cost:7275918542,count:3606511848080896768
cost:7221007694,count:3606511848080896768

9000000000 次计算,i += 1i = i + 1 快50毫秒,可以忽略不计。

本文网址: https://golangnote.com/topic/29.html 转摘请注明来源

Related articles

Write a Comment to "Golang 相加的性能测试"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.17.1 Processed in 0ms