Golang 中读取文件大概有三种方法,分别为:

    1. 通过原生态 io 包中的 read 方法进行读取

    2. 通过 io/ioutil 包提供的 read 方法进行读取

    3. 通过 bufio 包提供的 read 方法进行读取

  下面通过代码来验证这三种方式的读取性能,并总结出我们平时应该使用的方案,以便我们可以写出最优代码:

package main

import (
"os"
"io"
"bufio"
"io/ioutil"
"time"
"log"
) func readCommon(path string) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close() buf := make([]byte, 1024)
for {
readNum, err := file.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if 0 == readNum {
break
}
}
} func readBufio(path string) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close() bufReader := bufio.NewReader(file)
buf := make([]byte, 1024) for {
readNum, err := bufReader.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if 0 == readNum {
break
}
}
} func readIOUtil(path string) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
_, err = ioutil.ReadAll(file)
} func main() {
//size is 26MB
pathName := "/Users/mfw/Desktop/shakespeare.json"
start := time.Now()
readCommon(pathName)
timeCommon := time.Now()
log.Printf("read common cost time %v\n", timeCommon.Sub(start)) readBufio(pathName)
timeBufio := time.Now()
log.Printf("read bufio cost time %v\n", timeBufio.Sub(timeCommon)) readIOUtil(pathName)
timeIOUtil := time.Now()
log.Printf("read ioutil cost time %v\n", timeIOUtil.Sub(timeBufio))
}

  以上代码运行结果打印如下:

1
2
3
2017/05/11 19:23:46 read common cost time 25.584882ms
2017/05/11 19:23:46 read bufio  cost time 11.857878ms
2017/05/11 19:23:46 read ioutil cost time 35.033003ms

  再运行一次打印的结果如下:

1
2
3
2017/05/11 21:59:11 read common cost time 32.374922ms
2017/05/11 21:59:11 read bufio  cost time 12.155643ms
2017/05/11 21:59:11 read ioutil cost time 27.193033ms

  再多运行几次,发现 readCommon 和 readIOUtil 运行时间相差不大,通过查看源代码发现 io/ioutil 包其实就是封装了 io 包中的方法,故他们两没有性能优先之说;但是不管你运行多少次,readBufio 耗费时间是他们两各自所耗费时间一半左右。

  由此可见性能最好的是通过带有缓冲的 io 流来读取文件性能最佳。

Golang 中三种读取文件发放性能对比的更多相关文章

  1. Go_18: Golang 中三种读取文件发放性能对比

    Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...

  2. java中三种for循环之间的对比

    普通for循环语法: for (int i = 0; i < integers.length; i++) { System.out.println(intergers[i]); } foreac ...

  3. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

  4. 深入了解三种针对文件(JSON、XML与INI)的配置源

    深入了解三种针对文件(JSON.XML与INI)的配置源 物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonCon ...

  5. 【】VMware vSphere中三种磁盘规格的解释说明

    在VMware vSphere中,不管是以前的5.1版本,或者是现在的6.5版本,创建虚拟机时,在创建磁盘时,都会让选择磁盘的置备类型,如下图所示,分为: 厚置备延迟置零 厚置备置零 Thin Pro ...

  6. C#中三种定时器对象的比较

    ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...

  7. 转-Web Service中三种发送接受协议SOAP、http get、http post

    原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...

  8. C#中三种定时器对象的比较 【转】

    https://www.cnblogs.com/zxtceq/p/5667281.html C#中三种定时器对象的比较 ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.W ...

  9. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

随机推荐

  1. 总结html

    1.初识html W3C : 万维网联盟!(World Wide Web Consortium )   创建于1994年,是web技术领域最权威最具有影响力的标准机构!           W3C规定 ...

  2. [python]一个关于默认参数的老问题和一个有关优化的新问题

    一个老问题: def func(defau=[]): defau.append(1) return defau print(func())#print[1] print(func())#print[1 ...

  3. java的注解

    本文转载自:http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html 一.概念 Annontation是Java5开始引入的新特征 ...

  4. 【洛谷】2144:[FJOI2007]轮状病毒【高精度】【数学推导??(找规律)】

    P2144 [FJOI2007]轮状病毒 题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个n轮状基由圆环上n个不同的基原子和圆心的一个核原子构成.2个原子之间的边表示这2个原子之间 ...

  5. 体感设备:因特尔 Intel RealSense R200,乐视LeTV Pro Xtion和Orb奥比中光bec Astra比较

    最近调试三个个厂家的体感设备,第一个是Intel的RealSense R200(参数规格:分辨率:1080p,深度有效距离:0.51-4,USB3.0),第二个是乐视LeTV Pro Xtion(参数 ...

  6. String对象池的作用

    我们知道得到String对象有两种办法:String str1="hello";String str2=new String("hello");     这两种 ...

  7. Android Path Time ScrollBar(Path 时间轴)

    版本号:1.0 日期:2014.4.22 版权:© 2014 kince 转载注明出处   这是仿Path2.0UI的一个demo的截图,我最早是在农民伯伯的这篇博客中看到的[Andorid X 项目 ...

  8. 【我所认知的BIOS】—&gt; uEFI AHCI Driver(5) — 第一个protocol最终要開始安装了

    [我所认知的BIOS]-> uEFI AHCI Driver(5) - 第一个protocol最终要開始安装了 LightSeed 4/28/2014 文章对EFI_DRIVER_BINDING ...

  9. 通过adb把apk安装到系统分区

    通过adb把apk安装到系统分区 以谷歌拼音为例:GooglePinyin1.4.2.apk提取出so文件libjni_googlepinyinime_4.solibjni_googlepinyini ...

  10. USB Mass Storage Class – Bulk Only Transport - Error Handling

    6.4 Device Error Handling The device may not be able to fully satisfy the host's request. At the poi ...