美文网首页Go专题
几种读取文件方法速度比较

几种读取文件方法速度比较

作者: 右哼哼丨左哼哼 | 来源:发表于2020-02-19 14:25 被阅读0次
package main

import (
    "bufio"
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "time"
)

func read0(path string) string {
    f, err := ioutil.ReadFile(path)
    if err != nil {
        fmt.Printf("%s\n", err)
        panic(err)
    }
    return string(f)
}

func read1(path string) string {
    fi, err := os.Open(path)
    if err != nil {
        panic(err)
    }
    defer fi.Close()

    chunks := make([]byte, 1024, 1024)
    buf := make([]byte, 1024)
    for {
        n, err := fi.Read(buf)
        if err != nil && err != io.EOF {
            panic(err)
        }
        if 0 == n {
            break
        }
        chunks = append(chunks, buf[:n]...)
    }
    return string(chunks)
}

func read2(path string) string {
    fi, err := os.Open(path)
    if err != nil {
        panic(err)
    }
    defer fi.Close()
    r := bufio.NewReader(fi)

    chunks := make([]byte, 1024, 1024)

    buf := make([]byte, 1024)
    for {
        n, err := r.Read(buf)
        if err != nil && err != io.EOF {
            panic(err)
        }
        if 0 == n {
            break
        }
        chunks = append(chunks, buf[:n]...)
    }
    return string(chunks)
}

func read3(path string) string {
    fi, err := os.Open(path)
    if err != nil {
        panic(err)
    }
    defer fi.Close()
    fd, err := ioutil.ReadAll(fi)
    return string(fd)
}

func main() {

    file := "test.log"

    start := time.Now()

    read0(file)
    t0 := time.Now()
    fmt.Printf("Cost time %v\n", t0.Sub(start))

    read1(file)
    t1 := time.Now()
    fmt.Printf("Cost time %v\n", t1.Sub(t0))

    read2(file)
    t2 := time.Now()
    fmt.Printf("Cost time %v\n", t2.Sub(t1))

    read3(file)
    t3 := time.Now()
    fmt.Printf("Cost time %v\n", t3.Sub(t2))

}

运行结果:
Cost time 4.0105ms
Cost time 11.5043ms
Cost time 7.0042ms
Cost time 2.4983ms

Cost time 4.4925ms
Cost time 11.0053ms
Cost time 5.0082ms
Cost time 2.9992ms

Cost time 3.9866ms
Cost time 15.0085ms
Cost time 7.5054ms
Cost time 2.5035ms

Cost time 4.9989ms
Cost time 14.0112ms
Cost time 7.5045ms
Cost time 3.508ms

Cost time 3.0043ms
Cost time 15.0265ms
Cost time 8.9884ms
Cost time 2.0036ms

文章转载自:几种读取文件方法速度比较

相关文章

  • 几种读取文件方法速度比较

    运行结果:Cost time 4.0105msCost time 11.5043msCost time 7.004...

  • Python Pandas to_pickle()压缩文件

    本文将通过pandas to_pickle()方法压缩文件,并比较不同格式压缩文件的大小、写入速度、读取速度,对比...

  • 读取txt文件

    """ 读取txt文件txt文件使我们经常操作的文件类型,Python提供了以下几种读取txt文件的方法。read...

  • Golang读取文件和处理超大文件方案

    Golang 操作文件的读取的方法很多,适用的场景也是各不相同,在此我们将文件的读取分为如下几种 :文件整体读取文...

  • Java读取Properties文件的几种方式

    读取Properties文件的几种方式 读取 Properties 文件 1.在xml 配置文件中使用"${}" ...

  • maven工程读取resource目录下配置文件

    几种读取配置文件的方式比较(代码在src/main/java目录下,资源文件在src/main/resources...

  • 5.堆和栈

    栈空间比较小,但是读取速度快 堆空间比较大,但是读取速度慢

  • 数据清洗

    一、数据的读取与导出 ①. 读取数据:txt、csv和xlsx文件 · 速度最慢 · 中等速度 · 速度最快 ②....

  • Python读取csv文件

    Python读取csv文件数据的方法有多种,接下来给大伙介绍两种比较常用的。下面是csv数据文件: 方法一:读取c...

  • 2020-08-16

    python 获取配置文件的几种方法 1.以变量的方式读取 代码示例 配置文件 输出 2.python 2 中有个...

网友评论

    本文标题:几种读取文件方法速度比较

    本文链接:https://www.haomeiwen.com/subject/xzdafhtx.html