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

Golang 泛型性能初识

编程时,我们通常需要编写“泛型”函数,其中确切的数据类型并不重要。例如,您可能想编写一个简单的函数来对数字进行求和。Go 直到最近才有这个概念,但最近才添加了它(从1.18版开始)。...

Golang Range 的性能提升Tip

Go 语言里使用 range 可以方便遍历数组(array)、切片(slice)、字典(map)和信道(chan)。这里主要关注他们的性能。...

Golang phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

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

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