Maison > développement back-end > Golang > le corps du texte

Go语言中的基准测试和性能比较

王林
Libérer: 2024-05-08 09:27:02
original
871 人浏览过

在 Go 语言中,通过使用 testing 包中的 BenchmarkXXX 函数,可以轻松编写基准测试来衡量代码性能。这些函数遵循标准语法,并接收 *testing.B 类型的指针作为参数,用于控制基准测试的运行。运行基准测试(go test -bench=BenchmarkName),可以输出结果表格,显示各种信息,如每个操作所花费的纳秒数、每秒执行的操作数、测试中运行的迭代次数和每秒传递的内存量等。通过比较不同的基准测试结果,可以找出效率低下的代码区域,从而改进应用程序的整体性能。

Go语言中的基准测试和性能比较

Go 语言中的基准测试和性能比较

简介

基准测试是衡量代码性能的重要工具。它可以帮助找出效率低下的代码区域,从而提高应用程序的整体性能。Go 语言提供了一个内置的 testing 包,使得在 Go 中编写基准测试变得非常容易。

语法

基准测试函数的语法如下:

func BenchmarkName(b *testing.B)
Copier après la connexion

其中:

  • b 是一个 *testing.B 类型的指针,它包含了一些用于基准测试的附加功能。

实战案例

让我们编写一个基准测试来比较两种不同的排序算法的性能:

package main

import (
    "testing"
    "bytes"
    "sort"
)

// 插入排序
func insertionSort(nums []int) {
    for i := 1; i < len(nums); i++ {
        key := nums[i]
        j := i - 1

        for j >= 0 && nums[j] > key {
            nums[j+1] = nums[j]
            j--
        }

        nums[j+1] = key
    }
}

// 快速排序
func quickSort(nums []int) {
    if len(nums) <= 1 {
        return
    }

    pivot := nums[len(nums)/2]
    var left, right []int

    for _, num := range nums {
        if num < pivot {
            left = append(left, num)
        } else if num > pivot {
            right = append(right, num)
        }
    }

    quickSort(left)
    quickSort(right)

    copy(nums, append(left, append([]int{pivot}, right...)...))
}

// 基准测试
func BenchmarkInsertionSort(b *testing.B) {
    var buf bytes.Buffer

    for i := 0; i < b.N; i++ {
        nums := []int{5, 2, 8, 3, 1, 9, 4, 7, 6}
        insertionSort(nums)
        buf.WriteString(bytes.Join(nums, " "))
    }
}

func BenchmarkQuickSort(b *testing.B) {
    var buf bytes.Buffer

    for i := 0; i < b.N; i++ {
        nums := []int{5, 2, 8, 3, 1, 9, 4, 7, 6}
        quickSort(nums)
        buf.WriteString(bytes.Join(nums, " "))
    }
}

func BenchmarkGoSort(b *testing.B) {
    var buf bytes.Buffer

    for i := 0; i < b.N; i++ {
        nums := []int{5, 2, 8, 3, 1, 9, 4, 7, 6}
        sort.Ints(nums)
        buf.WriteString(bytes.Join(nums, " "))
    }
}
Copier après la connexion

运行基准测试

要运行基准测试,请运行以下命令:

go test -bench=BenchmarkName
Copier après la connexion

其中 BenchmarkName 是您要运行的基准测试函数的名称。

结果解读

基准测试结果将以表格的形式输出,其中包含各种信息,例如:

  • ns/op:每个操作所花费的纳秒数。
  • op/s:每秒执行的操作数。
  • B:测试中运行的迭代次数。
  • MB/s:每秒传递的内存量。

比较排序算法

运行上面的基准测试后,您会看到以下结果(结果可能会因您的硬件和系统配置而异):

BenchmarkInsertionSort     20332432               62.5 ns/op         16 B/op               5.75 MB/s
BenchmarkQuickSort         11440808              104 ns/op          24 B/op              1.64 MB/s
BenchmarkGoSort            21864500               57.7 ns/op          32 B/op               4.77 MB/s
Copier après la connexion

从这些结果中,我们可以看到 插入排序 是最慢的,其次是 快速排序,最快的则是 sort.Ints

以上是Go语言中的基准测试和性能比较的详细内容。更多信息请关注PHP中文网其他相关文章!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers numéros
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!