代码顺序和性能 [英] Order of the code and performance

查看:14
本文介绍了代码顺序和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到哪个更快:结构与数组.所以我写了一个 GO 代码,其中我将 4 个 int 值(1、2、3 和 4)写入结构的成员,然后写入长度为 4 的数组.我试图找出写入所需的时间.

案例 1:首先,我将值写入结构,然后写入数组.在这里,我发现数组比结构更快.

包主进口 (fmt"时间")输入 abc 结构 {a, b, c, d 整数}功能主(){var obj abct1 := time.Now()目标.a = 1目标.b = 2对象.c = 3目标.d = 4t2 := time.Since(t1)fmt.Println("结构体访问时间::", t2)a := make([]int, 4)t3 := time.Now()[0] = 1[1] = 2[2] = 3[3] = 4t4 := time.Since(t3)fmt.Println("数组访问时间::", t4)}

案例 2:其次,我将值写入数组,然后写入结构.在这里,我发现结构比数组更快.

包主进口 (fmt"时间")输入 abc 结构 {a, b, c, d 整数}功能主(){var obj abca := make([]int, 4)t3 := time.Now()[0] = 1[1] = 2[2] = 3[3] = 4t4 := time.Since(t3)fmt.Println("数组访问时间::", t4)t1 := time.Now()目标.a = 1目标.b = 2对象.c = 3目标.d = 4t2 := time.Since(t1)fmt.Println("结构体访问时间::", t2)}

为什么性能取决于我先写什么?我写的第一个似乎更慢.为什么会这样?

解决方案

另一个答案解释了时序差异,让我们进入 struct 与 slice.

如果编译器可以在编译时确定切片足够大,访问切片和结构的元素将生成相同的代码.当然,实际上,编译器通常不知道切片有多大,并且会根据您使用的是结构体还是切片来应用完全不同的优化,因此为了衡量性能,您必须查看整体程序及其行为,而不仅仅是一个特定的操作.

I wanted to find which is faster: struct vs array. So I wrote a GO code in which I write 4 int values (1,2,3 and 4) to the members of a structure and then to an array of length 4. I tried to find the time it takes to write.

Case1: First, I write values to a structure and then to an array. Here I found array to be faster than structure.

package main

import (
    "fmt"
    "time"
)

type abc struct {
    a, b, c, d int
}

func main() {

    var obj abc

    t1 := time.Now()
    obj.a = 1
    obj.b = 2
    obj.c = 3
    obj.d = 4
    t2 := time.Since(t1)

    fmt.Println("Struct access time: : ", t2)

    a := make([]int, 4)
    t3 := time.Now()
    a[0] = 1
    a[1] = 2
    a[2] = 3
    a[3] = 4
    t4 := time.Since(t3)

    fmt.Println("Array access time: : ", t4)

}

Case2: Second, I write values to an array and then a structure. Here I found structure to be faster than array.

package main

import (
    "fmt"
    "time"
)

type abc struct {
    a, b, c, d int
}

func main() {

    var obj abc

    a := make([]int, 4)
    t3 := time.Now()
    a[0] = 1
    a[1] = 2
    a[2] = 3
    a[3] = 4
    t4 := time.Since(t3)

    fmt.Println("Array access time: : ", t4)

    t1 := time.Now()
    obj.a = 1
    obj.b = 2
    obj.c = 3
    obj.d = 4
    t2 := time.Since(t1)

    fmt.Println("Struct access time: : ", t2)

}

Why the performance depends on to what I write first? The one that I write to first appears to be slower. Why is it so?

解决方案

The other answer explained the timing difference, let's get into struct vs. slice.

If the compiler can figure out at compile time that the slice is big enough, accessing the elements of the slice and a struct will generate identical code. Of course, in reality, often the compiler won't know how big the slice is and completely different optimizations will be applied depending on if you're working with a struct or a slice, so for measuring performance you have to look at a whole program and its behavior, not just one particular operation.

这篇关于代码顺序和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆