Java中復(fù)制文件的效率測(cè)試
項(xiàng)目中用到了圖片的上傳,對(duì)于上傳過(guò)程中,圖片的復(fù)制項(xiàng)目組用了兩種方法,一種是以java的IO流,另外一種是用org.apache.commons.io.FileUtils的工具類,今天我測(cè)試了一下,單純考慮文件的復(fù)制效率,apache的工具類的效率是普通io流讀取的3倍。
下面是測(cè)試源碼:
public class ImageTest { public static void main(String[] args) throws IOException { IOTest(); } public static void fileUtilsTest() throws IOException { // 趨近13毫秒后,就保持這個(gè)數(shù)值 File srcFile = new File("D:/1.apk"); File destFile = new File("E:/2.apk"); long sum = 0; for (int i = 0; i < 10; i++) { long startTime = System.currentTimeMillis(); FileUtils.copyFile(srcFile, destFile); long endTime = System.currentTimeMillis(); sum += (endTime - startTime); } long average = sum / 10; System.out.println("耗時(shí)" + average + "豪秒"); } public static void IOTest() throws IOException { // 50毫秒 File srcFile = new File("D:/1.apk"); File destFile = new File("E:/2.apk"); long sum = 0; for (int i = 0; i < 10; i++) { long startTime = System.currentTimeMillis(); InputStream is = new FileInputStream(srcFile); // 把圖片寫(xiě)入到上面設(shè)置的路徑里 OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[400]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); long endTime = System.currentTimeMillis(); sum += (endTime - startTime); } long average = sum / 10; System.out.println("耗時(shí)" + average + "豪秒"); } } |
posted on 2014-01-24 16:06 順其自然EVO 閱讀(188) 評(píng)論(0) 編輯 收藏