首先还是看一个用3中方式copy文件的测试Demo

分别是:普通Stream文件copy,BuffferedStream进行Copy 和Channel(nio)进行文件Copy的代码和性能测试报告:

 package com.ctyun.stream;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
*
* @Description 文件效率copy测试
* @author zhanghw@chinatelecom.com.cn
* @since 2015年9月11日
* @version V1.0
*/
public class FileCopyEfficientTest { //普通io流copy
public static void commonCopy(String source, String dest){
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(new File(source));
out = new FileOutputStream(new File(dest)); byte[] b = new byte[2048];
while(in.read(b) != -1 ){
out.write(b);
}
out.flush(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
if(out != null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
in = null;
out =null;
e.printStackTrace();
}
}
} //Streambuffer进行copy
public static void bufferCopy(String source, String dest){
FileInputStream in = null;
FileOutputStream out = null;
BufferedInputStream bufferedIn = null;
BufferedOutputStream bufferedOut = null; try {
in = new FileInputStream(new File(source));
out = new FileOutputStream(new File(dest)); bufferedIn = new BufferedInputStream(in);
bufferedOut = new BufferedOutputStream(out); byte[] b = new byte[2048];
while(bufferedIn.read(b) != -1 ){
bufferedOut.write(b);
}
bufferedOut.flush(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
if(out != null){
out.close();
}
if(bufferedIn != null){
bufferedIn.close();
}
if(bufferedOut != null){
bufferedOut.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
in = null;
out =null;
bufferedIn = null;
bufferedOut = null;
e.printStackTrace();
}
}
} //用Channel进行copy
public static void channelCopy(String source, String dest){
FileInputStream in = null;
FileOutputStream out = null;
FileChannel channelIn = null;
FileChannel channelOut = null; try {
in = new FileInputStream(new File(source));
out = new FileOutputStream(new File(dest));
channelIn = in.getChannel();
channelOut = out.getChannel(); channelIn.transferTo(0, channelIn.size(), channelOut); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
if(out != null){
out.close();
}
if(channelIn != null){
channelIn.close();
}
if(channelOut != null){
channelOut.close();
} } catch (IOException e) {
// TODO Auto-generated catch block
in = null;
out =null;
channelIn = null;
channelOut = null;
e.printStackTrace();
}
} } //测试因为会线程原因,造成时间不正确
/*public static void main(String[] args) {
String source = "d:/test.txt";
String dest1 = "d:/temp/test1.txt";
String dest2 = "d:/temp/test2.txt";
String dest3 = "d:/temp/test3.txt"; long l1 = System.currentTimeMillis();
commonCopy(source, dest1);
long l2 = System.currentTimeMillis();
System.out.println("普通Stream Copy用时:"+(l2-l1)); bufferCopy(source, dest2);
long l3 = System.currentTimeMillis();
System.out.println("buffer Stream Copy用时:"+(l3-l2)); channelCopy(source, dest3);
long l4 = System.currentTimeMillis();
System.out.println("channel Stream Copy用时:"+(l4-l3)); }*/ public static void main(String[] args) {
String source = "d:/test.txt";
String dest = "d:/temp/test1.txt"; long l1 = System.currentTimeMillis();
// commonCopy(source, dest);
bufferCopy(source, dest);
// channelCopy(source, dest);
long l2 = System.currentTimeMillis();
System.out.println("Copy用时:"+(l2-l1)); } }

性能测试结果及结论:

测试1:不同文件大小测试时间(在同一main函数中进行测试)

大小 普通Copy(毫秒)
BufferCopy(毫秒)
ChannelCopy(毫秒)
文件类型
1m
56
72 
文本
128m
2095
1132 
文本
512m
9215
10444
文本
1g
19989 
49358 
44809
文本
2g
69266
63989
文本
 
结论:通过观察发现此次测试结果之间差异非常大,并且笔者在观察文件copy时,并没有按照正常顺序进行copy,所以(猜测)可能是在同一main方法中因为线程资源不均衡问题,造成测试结果差异较大,不准确(copy文件之间存在干扰)。
为证实以上结论继续如下测试:

测试2:在同一main函数中进行测试
1024M多次测试结果

测试次数
大小 普通Copy(毫秒)
BufferCopy(毫秒)
ChannelCopy(毫秒)
文件类型
1
1g
17365 
34371
27906 
文本
2
1g
39903 
35203 
文本
3
1g
43865 
36720
文本
--------------------------------------
结论:每次性能差异很大,原因可能是(猜测):在同一个main方法中由于文件流操作时线程获取cup资源的时间不同造成(copy文件之间存在干扰),
为了保证测试的准确性,修改测试为没法方法在main方法中单独测试。

单独测试,在一个个main方法运行时,保证只有一个copy方法运行(最终测试方案,正确)(copy文件之间相互独立,不存在干扰)

大小 普通Copy(毫秒)
BufferCopy(毫秒)
ChannelCopy(毫秒)
文件类型
1m
62
31
文本
128m
1953
907
文本
512m
8126
3422
文本
1g
86944
87572
17703
文本
2g
66079
55298
文本
 
测试次数
大小 普通Copy(毫秒)
BufferCopy(毫秒)
ChannelCopy(毫秒)
文件类型
1
1g
15281
8937
文本
2
1g
15485
7203
文本
3
1g
16068
6828
文本
 
结论:在通常情况下,nio的性能>buffered性能>普通Stream的性能,但是在小文件的时候Buffered性能会比nio好

1.深入分析_NIO性能分析的更多相关文章

  1. Java 性能分析工具 , 第 3 部分: Java Mission Control

    引言 本文为 Java 性能分析工具系列文章第三篇,这里将介绍如何使用 Java 任务控制器 Java Mission Control 深入分析 Java 应用程序的性能,为程序开发人员在使用 Jav ...

  2. Android APP性能分析方法及工具

    近期读到<Speed up your app>一文.这是一篇关于Android APP性能分析.优化的文章.在这篇文章中,作者介绍他的APP分析优化规则.使用的工具和方法.我觉得值得大家借 ...

  3. 性能分析神器VisualVM

    VisualVM 是一款免费的,集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量数据.跟踪内存泄漏.监控垃圾回 ...

  4. 【转载】推荐5款超实用的.NET性能分析工具

    来源:http://www.csdn.net/article/2012-11-23/2812174-5-Good-and-useful-.NET-Profilers 虽然.NET框架号称永远不会发生内 ...

  5. 11个Visual Studio代码性能分析工具

    软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...

  6. 高性能Linux服务器 第10章 基于Linux服务器的性能分析与优化

    高性能Linux服务器 第10章    基于Linux服务器的性能分析与优化 作为一名Linux系统管理员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行.但硬件问题.软件问题.网络环境等 ...

  7. 【转】一文掌握 Linux 性能分析之内存篇

    [转]一文掌握 Linux 性能分析之内存篇 前面我们已经学习了 CPU 篇,这篇来看下内存篇. 01 内存信息 同样在分析内存之前,我们得知到怎么查看系统内存信息,有以下几种方法. 1.1 /pro ...

  8. .NET 11 个 Visual Studio 代码性能分析工具

    原文地址 软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行 ...

  9. c# 推荐5款超实用的.NET性能分析工具

    虽然.NET框架号称永远不会发生内存泄漏,原因是引入了内存回收机制.但在实际应用中,往往我们分配了对象但没有释放指向该对象的引用,导致对象永远无法释放.最常见的情况就是给对象添加了事件处理函数,但当不 ...

随机推荐

  1. 关于java多线程理解到集群分布式和网络设计的浅析

    对于JAVA多线程的应用非常广泛,现在的系统没有多线程几乎什么也做不了,很多时候我们在何种场合如何应用多线程成为一种首先需要选择的问题, 另外关于java多线程的知识也是非常的多,本文中先介绍和说明一 ...

  2. Solr4.2 新特性 DocValues [转]

    原文地址http://wiki.apache.org/solr/DocValues DocValues从Lucene4.2和Solr4.2开始加入,通过建立字段的正排索引,提升sorting, fac ...

  3. Excel导出文件流下载

    Controller.cs @CrossOrigin(allowCredentials="true", allowedHeaders="*", methods= ...

  4. itertools库 combinations() 和 permutations() 组合 和 排列选项的方法

    combinations方法重点在组合,permutations方法重在排列. combinations和permutations返回的是对象地址,原因是在python3里面,返回值已经不再是list ...

  5. [ZZ]39条更好的软件开发方法

    1.重构是程序员的主力技能. 2.工作日志能提升脑容量. 3.先用profiler调查,才有脸谈优化. 4.注释贵精不贵多.杜绝大姨妈般的“例注”.漫山遍野的碎碎念注释,实际就是背景噪音. 5.普通程 ...

  6. 判断浏览器是否IE(IE11可用)

    function isIE() { //ie?         if (!!window.ActiveXObject || "ActiveXObject" in window) { ...

  7. 面向对象的轮播js

    1.自执行函数的前后要加分号 案例: ;(function(){})(); 2.面向对象的最大优势节省了许多内存 正式开写面向对象的轮播: <!DOCTYPE html> <html ...

  8. json--处理框架

    1.Android 中的Json解析工具fastjson .序列化.反序列化 2.Android Gson的使用总结 可以处理含有内部类的类,或包含集合内部类的类: 3.Android-JSONToo ...

  9. [UE4]优先级与相关性

    一.优先级 每个 Actor 都有一个名为 NetPriority 的浮点变量.这个变量的数值越大,Actor 相对于其他“同伴”的带宽就越多.和优先级为 1.0 的 Actor 相比,优先级是 2. ...

  10. 第10章 网络安全(4)_网络层安全IPSec

    5. 网络层安全IPSec 5.1 IPSec协议 (1)前面使用Outlook进行数字签名和数字加密是应用层实现的安全.安全套接字实现的安全是在应用层和传输层之间插入了一层来实现数据通信安全.而IP ...