golang中copy文件时,buffer设多大值合适,性能对比

在go语言中,copy文件时,大文件使用buffer缓冲,可以明显加快时间,

但这个值多大合适呢?

除了考虑计算机的硬件资源,还要考虑CP文件的大小。

如果都是100m之内的小文件,一次CP完就可以。

但如果文件大于1G,建设还是设置一个大一些的缓冲来操作。

copy.go

func Copy(src, dst string, BUFFERSIZE int64) error {
	sourceFileStat, err := os.Stat(src)
	if err != nil {
		return err
	}
	if !sourceFileStat.Mode().IsRegular() {
		return fmt.Errorf("%s is not a regular file.", src)
	}
	source, err := os.Open(src)
	if err != nil {
		return err
	}
	defer source.Close()
	_, err = os.Stat(dst)
	if err == nil {
		return fmt.Errorf("File %s already exists.", dst)
	}
	destination, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer destination.Close()
	if err != nil {
		panic(err)
	}
	buf := make([]byte, BUFFERSIZE)
	for {
		n, err := source.Read(buf)
		if err != nil && err != io.EOF {
			return err
		}
		if n == 0 {
			break
		}
		if _, err := destination.Write(buf[:n]); err != nil {
			return err
		}
	}
	return err
}

  

原文地址:https://www.cnblogs.com/aguncn/p/12080306.html

时间: 2024-08-03 04:32:08

golang中copy文件时,buffer设多大值合适,性能对比的相关文章

Golang中interface{}作为函数参数和函数返回值的使用

package main import (     "errors"     "fmt" ) type item struct {     Name string } func (i item) String() string {     return fmt.Sprintf("item name: %v", i.Name) } type person struct {     Name string     Sex  string } func

关于golang中IO相关的Buffer类浅析

io重要的接口 在介绍buffer之前,先来认识两个重要的接口,如下边所示: type Reader interface { Read(p []byte) (n int, err error) } type Writer interface { Write(p []byte) (n int, err error) } 上边两个接口在golang sdk安装目录src/io/io.go中定义.后边凡是涉及到io相关操作的,基本上都实现了这两个接口,如: 1. package bufio 中的Rea

对express中引入文件时提示Error: Cannot find module错误的理解

打算写个小demo,在引入一个routes文件时,一直提示Error: Cannot find module('./routes')的错误,经过一番了解. 如果要把整个文件夹下所有的模块都引进来  var routes = require('./routes')这时express会默认的去读取文件下的index.js文件,所以文件夹下所有的模块只需在index.js声明即可. 例如: 在routes文件下有user.js.count.js.index.js文件, 只需在index.js中 exp

JavaSE8基础 File createNewFile 在一个不存在的文件夹中创建文件时 会抛IO异常(绝对路径)

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 在编写代码时,javase8文件夹的情况截图. code: package jizuiku0; import java.io.File; import java.io.IOException; /* * @version V17.09 */ public class FileDemo_1 { public

Linux中打开文件时的O_EXCL有什么用

问题描述: open(pathname, O_RDWR | O_CREAT,0666);就不行吗?为什么好多资料上说要加O_EXCL,说是原子性操作,不加难道就不是了吗? 解答: 如果文件事先已经存在, open(pathname, O_RDWR | O_CREAT,0666);  打开成功,返回一个大于0的fd open(pathname, O_RDWR | O_CREAT | O_EXCL,0666); 打开失败,返回-1 O_EXCL表示的是:如果使用O_CREAT时文件存在,就返回错误信

Java中写入文件时换行符用"\r\n"、"\n"、"\r"?

Java是一个跨平台的语言,因为如果想写一个跨平台的软件,有些东西就需要考虑,例如换行. \r: 叫回车 Carriage Return \n: 叫新行 New Line 他们都会造成换行,那么我们如何确定使用哪个呢?  通常建议使用line.separator的系统属性 System.getProperty("line.separator")来获取当前OS的换行符,可以在调试的情况下看到! 不过如果你是在写一个网络程序或者服务器程序,则需要硬编码为"\r\n",而

自己对golang中各个文件的理解

models -> 里面写关于数据库方面数据controllers ->逻辑,请求方法等conf ->注册数据库信息routers ->初始化路由线路views ->测试界面 实际运行:1.通过models 来连接数据库,在models.go中注册models,每一个其余models(不包含models.go)都相当于数据库内的一张表,每一个 struct 里的数据都是列名 2.从views里面的web获取option,传到controllers里的client.go,在其中

matlab读路径中的文件时路径中字母不区分大小写

load('D:\假期 motion blur\20160102\Image\LPQ\LPQ_BIO_Random1\Acc_Plot_P2');load('D:\假期 motion blur\20160102\Image\LPQ\lpq_BIO_Random1\Acc_Plot_P2');load('d:\假期 motion blur\20160102\Image\LPQ\lpq_BIO_Random1\Acc_Plot_P2');load('D:\假期 motion blur\2016010

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

Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提供的 read 方法进行读取 下面通过代码来验证这三种方式的读取性能,并总结出我们平时应该使用的方案,以便我们可以写出最优代码: package main import ( "os" "io" "bufio" "io/ioutil"