duanpin9531 2018-08-22 18:53
浏览 50
已采纳

Go中最快的数据加载方式

I need to regularly load over 300'000 rows x 78 columns of data into my Go program.

Currently I use (import github.com/360EntSecGroup-Skylar/excelize):

xlsx, err := excelize.OpenFile("/media/test snaps.xlsm")
if err != nil {
    fmt.Println(err)
    return
}

//read all rows into df
df := xlsx.GetRows("data")

It takes about 4 minutes on a decent PC using Samsung 960 EVO Series - M.2 Internal SSD.

Is there a faster way to load this data? Currently it takes me more time to read the data than processing it. I'm also opened to other file formats.

  • 写回答

1条回答 默认 最新

  • doupi7619 2018-08-22 20:11
    关注

    As suggested in the comments, instead of using the XLS format, use a custom, fast data format for reading and writing your table.

    In the most basic case, just write the number of columns and rows to a binary file, then write all the data in one go. This will be very fast, I have created a little example here which just writes 300.000 by 40 float32s to a file and reads them back. On my machine this takes about 400ms and 250ms (notice that the file is hot in cache after writing it, may take longer on initial read).

    package main
    
    import (
        "encoding/binary"
        "os"
    
        "github.com/gonutz/tic"
    )
    
    func main() {
        const (
            rowCount = 300000
            colCount = 40
        )
        values := make([]float32, rowCount*colCount)
        func() {
            defer tic.Toc()("write")
            f, _ := os.Create("file")
            defer f.Close()
            binary.Write(f, binary.LittleEndian, int64(rowCount))
            binary.Write(f, binary.LittleEndian, int64(colCount))
            check(binary.Write(f, binary.LittleEndian, values))
        }()
        func() {
            defer tic.Toc()("read")
            f, _ := os.Open("file")
            defer f.Close()
            var rows, cols int64
            binary.Read(f, binary.LittleEndian, &rows)
            binary.Read(f, binary.LittleEndian, &cols)
            vals := make([]float32, rows*cols)
            check(binary.Read(f, binary.LittleEndian, vals))
        }()
    }
    
    func check(err error) {
        if err != nil {
            panic(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格