Golang 中三种读取文件发放性能对比
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.584882ms2017/05/11 19:23:46 read bufio cost time 11.857878ms2017/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.374922ms2017/05/11 21:59:11 read bufio cost time 12.155643ms2017/05/11 21:59:11 read ioutil cost time 27.193033ms |
再多运行几次,发现 readCommon 和 readIOUtil 运行时间相差不大,通过查看源代码发现 io/ioutil 包其实就是封装了 io 包中的方法,故他们两没有性能优先之说;但是不管你运行多少次,readBufio 耗费时间是他们两各自所耗费时间一半左右。
由此可见性能最好的是通过带有缓冲的 io 流来读取文件性能最佳。
Golang 中三种读取文件发放性能对比的更多相关文章
- Go_18: Golang 中三种读取文件发放性能对比
Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...
- java中三种for循环之间的对比
普通for循环语法: for (int i = 0; i < integers.length; i++) { System.out.println(intergers[i]); } foreac ...
- iOS开发UI篇—iOS开发中三种简单的动画设置
iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...
- 深入了解三种针对文件(JSON、XML与INI)的配置源
深入了解三种针对文件(JSON.XML与INI)的配置源 物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonCon ...
- 【】VMware vSphere中三种磁盘规格的解释说明
在VMware vSphere中,不管是以前的5.1版本,或者是现在的6.5版本,创建虚拟机时,在创建磁盘时,都会让选择磁盘的置备类型,如下图所示,分为: 厚置备延迟置零 厚置备置零 Thin Pro ...
- C#中三种定时器对象的比较
·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...
- 转-Web Service中三种发送接受协议SOAP、http get、http post
原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...
- C#中三种定时器对象的比较 【转】
https://www.cnblogs.com/zxtceq/p/5667281.html C#中三种定时器对象的比较 ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.W ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
随机推荐
- JavaQuery
1.初识jQuery <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
- python functools.wraps
我们在使用装饰器的时候,有些函数的功能会丢失,比如func.__name__,func.__doc__,func.__module__ 比如下面这个例子: In [16]: def logged(fu ...
- 502 解决:[WARNING] fpm_children_bury
查过网上的资源,基本都是认为是php线程打开文件句柄受限导致的错误.具体的解决的办法如下: 1.提升服务器的文件句柄打开打开 /etc/security/limits.conf : (增加) * ...
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...
- Codeforces Beta Round #37 A. Towers 水题
A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...
- Illegal instruction错误的定位---忽略编译期警告的代价
在原计算机的linux c++程序可以正确跑起来,但是换了一台机器运行时出现致命错误,程序直接当掉,错误提示如下: Illegal instruction (core dumped) 造成改错的主要原 ...
- PUSH MESSAGE 云控等交互类测试业务的自动化
针对的业务: 1. PUSH消息,即由云端服务向客户端推送消息 2. MESSAG消息,即用户间消息.用户群消息和聊天室消息 上干货,框架见下图:
- [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080
相信如果用谷歌浏览器做移动端页面的时候 用touch事件的时候应该遇到过这个东东吧 documet.addEventListener("touchstart",function() ...
- 多进程多线程GDB调试 (转)
多进程多线程GDB调试 一.线程调试指南: 1. gdb attach pid 挂载到调试进程 2. gdb$ set scheduler-locking on 只执行当前选定线程的 ...
- java基础学习总结——开篇
java是我学习的第一门编程语言,当初学习java基础的时候下了不少功夫,趁着这段时间找工作之际,好好整理一下以前学习java基础时记录的笔记,当作是对java基础学习的一个总结吧,将每一个java的 ...