conv

package
v1.0.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Conv functions

This package provides utility functions for converting golang data types with high performance. These functions pursuit maximum performance for building data-intensive applications, at the cost of versatility compared with golang native functions.

For example, golang native function like fmt.Sprintf or strconv.Itoa do typecast and may alloc slices inside, which is costly and not cache-line friendly.

In this package, you can find some specialized functions crafted with care. Although a little bit annoying when using because you need to prepare a byte array outside as buffer, when your program processing thousands of millions of tuples to an io.Writer, it enables to reuse that pre-allocated buffer, pressing the throughput to times over golang native functions.

You can find benchmarks at the bottom of each *_test.go file. To run the benchmark

go test -bench=. -benchmem -cpu=1

If your program only does conversion on a small number of samples, you probably don't need this package. Use golang native way is good enough. This package is beneficial for a program (1) processing a large number of data (2) performance is a priority

Example

var buff = [4]byte{}
for i := 0; i < 10000000; i++ {
    s := conv.Int8ToBytes(input[i], &buff)
    writer.Write(s)
}

Documentation

Index

Constants

View Source
const NaN = "NaN"

NaN is a string for "NaN"

Variables

View Source
var NaNb = []byte{'N', 'a', 'N'}

NaNb is byte slice for 'NaN'

Functions

func Float64ToBytes

func Float64ToBytes(n float64, prec int, buf *[39]byte) []byte

Float64ToBytes is an optimized implementation for converting float64 to byte array

func Float64ToString

func Float64ToString(n float64, prec int, buf *[39]byte) string

Float64ToString is an optimized implementation for converting float64 to string

func FormatMD5

func FormatMD5(hash [16]byte, buf *[32]byte)

FormatMD5 converts md5.Sum() result to byte slice

func Int16ToBytes

func Int16ToBytes(n int16, buf *[6]byte) []byte

Int16ToBytes is the fastest way to convert int16 into byte slice

func Int32ToBytes

func Int32ToBytes(n int32, buf *[11]byte) []byte

Int32ToBytes is the fastest way to convert int32 into byte slice

func Int64ToBytes

func Int64ToBytes(n int64, buf *[20]byte) []byte

Int64ToBytes is the fastest way to convert int64 into byte slice

func Int8ToBytes

func Int8ToBytes(n int8, buf *[4]byte) []byte

Int8ToBytes is the fastest way to convert int8 into byte slice

func UInt16ToBytes

func UInt16ToBytes(n uint16, buf *[5]byte) []byte

UInt16ToBytes is the fastest way to convert uint16 into byte slice

func UInt32ToBytes

func UInt32ToBytes(n uint32, buf *[10]byte) []byte

UInt32ToBytes is the fastest way to convert uint32 into byte slice

func UInt64ToBytes

func UInt64ToBytes(n uint64, buf *[20]byte) []byte

UInt64ToBytes is the fastest way to convert uint64 into byte slice

func UInt8ToBytes

func UInt8ToBytes(n uint8, buf *[3]byte) []byte

UInt8ToBytes is the fastest way to convert uint8 into byte slice

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL