做钓鱼网站用哪种编程语言,代理网站备案,wordpress禁止访问模版页面,网站建站平台源码Go语言中写文件有多种方式#xff0c;这里进行如下几种方式的速度对比#xff1a; 打开文件#xff0c;写入内容#xff0c;关闭文件。如此重复多次打开文件#xff0c;写入内容#xff0c;defer 关闭文件。如此重复多次打开文件#xff0c;重复多次写入内容#xff0c… Go语言中写文件有多种方式这里进行如下几种方式的速度对比 打开文件写入内容关闭文件。如此重复多次打开文件写入内容defer 关闭文件。如此重复多次打开文件重复多次写入内容defer 关闭文件在VMWare下的Ubuntu 14.04下运行的结果表明 方式1速度最慢但是慢的很稳定方式2比方式1略快但是重复次数多了后会报错应该是defer被压栈太多导致系统撑不了太多打开的文件方式3速度约是前两者的2倍因为减少了很多打开关闭文件的操作测试代码如下 package mainimport (fmtostime
)func benchmarkFileWrite(filename string, n int, index int) (d time.Duration) {v : ni shuo wo shi bu shi tai wu liao le a?os.Remove(filename)t0 : time.Now()switch index {case 0: // open file and write, then close, repeat n timesfor i : 0; i n; i {file, err : os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)if err ! nil {fmt.Println(index, i, open file failed., err.Error())break}file.WriteString(v)file.WriteString(\n)file.Close()}case 1: // open file and write, defer close, repeat n timesfor i : 0; i n; i {file, err : os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)if err ! nil {fmt.Println(index, i, open file failed., err.Error())break}defer file.Close()file.WriteString(v)file.WriteString(\n)}case 2: // open file and write n times, then close filefile, err : os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)if err ! nil {fmt.Println(index, open file failed., err.Error())break}defer file.Close()for i : 0; i n; i {file.WriteString(v)file.WriteString(\n)}}t1 : time.Now()d t1.Sub(t0)fmt.Printf(time of way(%d)%v\n, index, d)return d
}func main() {const k, n int 3, 1000d : [k]time.Duration{}for i : 0; i k; i {d[i] benchmarkFileWrite(benchmarkFile.txt, n, i)}for i : 0; i k-1; i {fmt.Printf(way %d cost time is %6.1f times of way %d\n, i, float32(d[i])/float32(d[k-1]), k-1)}
} 当n1000时测试结果如下 time of way(0)38.719386mstime of way(1)31.428677mstime of way(2)17.930829msway 0 cost time is 2.2 times of way 2way 1 cost time is 1.8 times of way 2 当n5000时测试结果如下因为方式1报错所以它的时间是错的 time of way(0)170.003521ms1 1021 open file failed. open benchmarkFile.txt: too many open filestime of way(1)32.388994mstime of way(2)77.777936msway 0 cost time is 2.2 times of way 2way 1 cost time is 0.4 times of way 2 转载于:https://www.cnblogs.com/journeyonmyway/p/4320987.html