In the Go language, you can easily write benchmark tests to measure code performance by using the BenchmarkXXX functions in the testing package. These functions follow the standard syntax and receive a pointer of type *testing.B as argument, which controls the running of the benchmark. Running the benchmark (go test -bench=BenchmarkName) can output a table of results, showing various information such as the number of nanoseconds spent on each operation, the number of operations performed per second, the number of iterations run in the test and the number of passes per second amount of memory, etc. By comparing the results of different benchmarks, you can identify inefficient code areas and thereby improve the overall performance of your application.
Benchmarks and Performance Comparisons in the Go Language
Introduction
Benchmarks Tests are an important tool for measuring code performance. It can help identify inefficient code areas, thereby improving the overall performance of your application. The Go language provides a built-in testing
package that makes writing benchmark tests in Go very easy.
Syntax
The syntax of the benchmark function is as follows:
func BenchmarkName(b *testing.B)
Where:
b
Is a pointer of type *testing.B
that contains some additional functionality for benchmarking. Practical Case
Let’s write a benchmark to compare the performance of two different sorting algorithms:
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, " ")) } }
Running the Benchmark
To run the benchmark, run the following command:
go test -bench=BenchmarkName
where BenchmarkName
is the name of the benchmark function you want to run.
Interpretation of results
The benchmark results will be output in the form of a table, containing various information, such as:
Comparison sorting algorithm
After running the above benchmark, you will see the following results (results may vary depending on your hardware and system configuration ):
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
From these results, we can see that insertion sort
is the slowest, followed by quicksort
, and the fastest is sort.Ints
.
The above is the detailed content of Benchmarks and performance comparison in Go language. For more information, please follow other related articles on the PHP Chinese website!