Get the App
SLTechnology News&Howtos  ›  Development  › 

How to optimize go performance

Shulou Source: shulou.com Published: 2022-06-03 06:08:18 09月18日 Update

This article focuses on "how to optimize go performance", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to optimize go performance.

Record the optimization of the core processing logic

Because the range of decimal numbers for virtual ip is 168493058, 168558590, we change its type from int to uint64, not to uint32 for good calculation; secondly, the string concatenation method uses string.Join, and finally, we use arrays instead of slices.

The virtual IP segment corresponding to the string slice / / using the slice storage string join method is 10.11.0.2 10.11.255.253 the uint64const VIPMIN corresponding to the minimum and maximum allocable virtual IP, VIPMAX uint64 = 168493058, 168558590var fieldMaps = [4] uint64 {24,16,8,0} / / OctToVipArray array func OctToVipArray (vipOct uint64) (addr string) {if vipOct

< VIPMIN && vipOct >

VIPMAX {log.Fatal ("pass in error decimal integers Check if all virtual IP are assigned ") return} else {var vips [4] string for index, value: = range fieldMaps {vips [index] = strconv.FormatUint (vipOct > > value&0xff, 10)} return strings.Join (vips [:] ".")}} use array storage string join method to concatenate string array / / octToVipSlice slice func OctToVipSlice (vipOct uint64) (addr string) {if vipOct

< VIPMIN && vipOct >

VIPMAX {log.Fatal ("pass in error decimal integers Check if all virtual IP are assigned ") return} else {var vips [] string for _, value: = range fieldMaps {vips = append (vips, strconv.FormatUint (vipOct > > value&0xff, 10))} return strings.Join (vips,". ")}} benchmark

Test function

/ / benchmark func BenchmarkA (b * testing.B) {b.ResetTimer () for index: = uint64 (168507432); index

Tags: Test method character string array performance decimal decimal allocation memory content benchmark integer error storage learning check practical deeper maximum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn macOS Huawei MySQL