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. golang实现base64编解码

    golang中base64的编码和解码可以用内置库encoding/base64 package main import ( "encoding/base64" "fmt ...

  2. 我们不能把JavaScript作为一个全功能的编程语言。它缺乏以下重要特征

    客户端JavaScript不允许读或写文件.这已被保存的安全原因. JavaScript不能用于网络的应用,因为没有这样的支持. JavaScript没有任何多线程或多处理器的能力.

  3. android无后缀二进制执行文件替代apk实现程序功能

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha android无后缀二进制执行文件替代apk实现程序功能 实现将data/Android ...

  4. luogu4407 [JSOI2009]电子字典 字符串hash + hash表

    暴力枚举,然后\(hash\)表判断 复杂度\(O(26 * 20 * n)\) 具体而言 对于操作1:暴力枚举删除 对于操作2:暴力添加,注意添加不要重复 对于操作3:暴力替换,同样的注意不要重复 ...

  5. php在linux后台执行

    <?php ignore_user_abort();//后台运行 ini_set('default_socket_timeout', -1);//socket不超时 set_time_limit ...

  6. python模块整理30-uui模块

    http://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.htmlhttp://blog.csdn.net/zhaoweikid/article ...

  7. ISO 7816-4: Interindustry Commands for Interchange

    5. Basic Organizations 5.1 Data structures5.2 Security architecture of the card 5.3 APDU message str ...

  8. Android 菜单键和返回键互换

    打开RE管理器找到system/usr/keylayout/ 长按qwerty.kl选择以文本编辑器查看 将里面的MENU和BACK全部替换掉 保存,退出管理器,重启手机,菜单键和返回键的位置就调换过 ...

  9. JAVA基础知识要点

    MQ.dubbo.SpringCloud 1) 集合框架 2)线程 3)IO流 4)类和对象生命周期 5)JAVA的反射机制 6) JVM 7)数据结构和常用算法 8)设计模式 9)网络编程

  10. hadoop函数说明图