


What is the most efficient way to read a byte file into an int64 slice?
php editor Zimo is here to answer a common question: "What is the most effective way to read byte files into int64 slices?" When we need to convert bytes When a file is read into an int64 slice, the following method can be used: first, use the file_get_contents function to read the byte file, and then use the unpack function to unpack the byte file into an int64 slice. This method is simple and efficient, and can quickly convert byte files into int64 slices to meet our needs. Hope this method can help everyone!
Question content
I have several packed int64 files. I need them in memory as int64 slices. The problem is that the files combined exceed half the size of the machine's memory, so space is limited. Standard options in go look like:
a := make([]int64, f.Size()/8) binary.Read(f, binary.LittleEndian, a)
Unfortunately, the binary
package will immediately allocate a []byte
of size f.size()*8
and run out of memory.
It does work if I read each byte one at a time and copy it into the slice, but this is too slow.
The ideal situation would be to convert the []byte
directly to []int64
and just tell the compiler "ok, these are integers now", but obviously that won't work of. Is there any way to accomplish something similar? Maybe use an unsafe package or put in c when absolutely necessary?
Solution
I have several packed int64 files. I need them in memory as int64 slices. The problem is that the files combined exceed half the size of the machine's memory, so space is limited.
The standard options in go are similar to:
a := make([]int64, f.Size()/8) binary.Read(f, binary.LittleEndian, a)
Unfortunately, the binary package will immediately allocate a []byte of size f.size()*8 and run out of memory.
All functions use minimal memory.
// same endian architecture and data // most efficient (no data conversion). func readfileint64se(filename string) ([]int64, error) { b, err := os.readfile(filename) if err != nil { return nil, err } const i64size = int(unsafe.sizeof(int64(0))) i64ptr := (*int64)(unsafe.pointer(unsafe.slicedata(b))) i64len := len(b) / i64size i64 := unsafe.slice(i64ptr, i64len) return i64, nil }
For example, for maximum efficiency with amd64 (littleendian) architecture and littleendian data (no data conversion required), use readfileint64se
.
Byte order fallacy - rob pike
https://commandcenter.blogspot.com/2012/04/byte- order-fallacy.html
// littleendian in-place data conversion for any architecture func readfileint64le(filename string) ([]int64, error) { b, err := os.readfile(filename) if err != nil { return nil, err } const i64size = int(unsafe.sizeof(int64(0))) i64ptr := (*int64)(unsafe.pointer(unsafe.slicedata(b))) i64len := len(b) / i64size i64 := unsafe.slice(i64ptr, i64len) for i, j := i64size, 0; i <= len(b); i, j = i+i64size, j+1 { i64[j] = int64(binary.littleendian.uint64(b[i-i64size : i])) } return i64, nil }
// BigEndian in-place data conversion for any architecture func readFileInt64BE(filename string) ([]int64, error) { b, err := os.ReadFile(filename) if err != nil { return nil, err } const i64Size = int(unsafe.Sizeof(int64(0))) i64Ptr := (*int64)(unsafe.Pointer(unsafe.SliceData(b))) i64Len := len(b) / i64Size i64 := unsafe.Slice(i64Ptr, i64Len) for i, j := i64Size, 0; i <= len(b); i, j = i+i64Size, j+1 { i64[j] = int64(binary.BigEndian.Uint64(b[i-i64Size : i])) } return i64, nil }
The above is the detailed content of What is the most efficient way to read a byte file into an int64 slice?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explains Go's package import mechanisms: named imports (e.g., import "fmt") and blank imports (e.g., import _ "fmt"). Named imports make package contents accessible, while blank imports only execute t

This article explains Beego's NewFlash() function for inter-page data transfer in web applications. It focuses on using NewFlash() to display temporary messages (success, error, warning) between controllers, leveraging the session mechanism. Limita

This article details efficient conversion of MySQL query results into Go struct slices. It emphasizes using database/sql's Scan method for optimal performance, avoiding manual parsing. Best practices for struct field mapping using db tags and robus

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

This article details efficient file writing in Go, comparing os.WriteFile (suitable for small files) with os.OpenFile and buffered writes (optimal for large files). It emphasizes robust error handling, using defer, and checking for specific errors.

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization
