對java和c的io速度的進一步比較

針對Java的Stream,BufferedStream,NIO,Scanner對文件的io效率作了一個實驗,同時結合c爲參考,廢話很少說,代碼以下:java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Scanner;

class TestIO {
	private InputStream ins;
	private OutputStream outs;
	public TestIO(){}
	public TestIO(InputStream ins,OutputStream outs){
		this.ins = ins;
		this.outs = outs;
	}
	public void setIns(InputStream is){
		this.ins = is;
	}
	public InputStream getIns(){
		 return ins;
	}
	public void setOuts(OutputStream outs){
		this.outs = outs;
	}
	public OutputStream getOuts(){
		 return outs;
	}
}

public class Main {
	
	final static String filename = "D:/testio.txt";
	
	public static void testio(TestIO tio){
		byte[] buffer = new byte[4096];
		try {
			long start = System.currentTimeMillis();
			while(tio.getIns().read(buffer)>0){
				tio.getOuts().write(buffer);
			}
			long end = System.currentTimeMillis();
			System.out.println("time cost is "+(end-start)+" ms");
			tio.getIns().close();
			tio.getOuts().close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try{
				tio.getIns().close();
				tio.getOuts().close();
			} catch(IOException e){
				e.printStackTrace();
			}
		}
	}
	
	public static void createInputFile(){
		try{
			OutputStream os = new BufferedOutputStream(new FileOutputStream(filename)); 
			for(int i=0;i<50000000;++i){
				os.write("A B C D E F G\n".getBytes());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
//		createInputFile();
		
		TestIO tio;
		
		try{
			//buffered stream
			tio = new TestIO(new FileInputStream(filename),new FileOutputStream(filename+".bak1"));
			tio.setIns(new BufferedInputStream(tio.getIns(),8192));
			tio.setOuts(new BufferedOutputStream(tio.getOuts(),8192));
			System.out.println("buffered stream method :");
			testio(tio);

			//stream
			tio = new TestIO(new FileInputStream(filename),new FileOutputStream(filename+".bak2"));
			System.out.println("normal stream method :");
			testio(tio);
			
			//nio
			long s = System.currentTimeMillis();
			FileChannel fci = new FileInputStream(filename).getChannel();
			FileChannel fco = new FileOutputStream(filename+".bak3").getChannel();
			ByteBuffer bb = ByteBuffer.allocateDirect(8192);
			while(fci.read(bb) != -1){
				bb.flip();
				fco.write(bb);
				bb.clear();
			}
			long e = System.currentTimeMillis();
			System.out.println("\nnio : "+(e-s)+" ms");
			
			//scanner
			s = System.currentTimeMillis();
			Scanner in = new Scanner(new FileInputStream(filename));
			while(in.hasNextLine()){
				in.nextLine();
			}
			e = System.currentTimeMillis();
			System.out.println("\nScanner : "+(e-s)+" ms");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
}
c:

#include<time.h>
#include<stdio.h>
main()
{
    long s = clock();
    FILE* fpr = fopen("D:/testio.txt","r");
    FILE* fpw = fopen("D:/testio.txt.bak_c","w");
    char buff[8192];
    while(fgets(buff,sizeof buff,fpr)!=NULL){
        fputs(buff,fpw);
    }
    long e = clock();
    printf("%f\n",1.0*(e-s)/CLOCKS_PER_SEC);
}
運行結果不貼了,有興趣的話能夠跑跑看,這裏只把結論說一下。

Java的BufferedStream, Stream, NIO的速度差異很小,都與C同樣快;可是JAVA在用着三種方式IO的時候CPU佔用率極低,而C幾乎獨佔了一個CPU。測試

Java的Scanner就沒什麼可說的了,超級慢,並且也超級消耗CPU資源。this

看來前面的那個測試對Java有些誤解,sorry啦。code

相關文章
相關標籤/搜索