栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java有效地获取文件大小

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java有效地获取文件大小

好吧,我尝试使用下面的代码对其进行度量:

对于运行次数= 1和迭代次数= 1,URL方法大多数时候是最快的,其次是频道。我以大约十次的新鲜暂停运行。因此对于一次访问,使用URL是我想到的最快方法:

LENGTH sum: 10626, per Iteration: 10626.0CHANNEL sum: 5535, per Iteration: 5535.0URL sum: 660, per Iteration: 660.0

如果运行次数= 5,迭代次数= 50,则绘制的图片会有所不同。

LENGTH sum: 39496, per Iteration: 157.984CHANNEL sum: 74261, per Iteration: 297.044URL sum: 95534, per Iteration: 382.136

文件必须缓存对文件系统的调用,而通道和URL会有一些开销。

码:

import java.io.*;import java.net.*;import java.util.*;public enum FileSizeBench {    LENGTH {        @Override        public long getResult() throws Exception { File me = new File(FileSizeBench.class.getResource(         "FileSizeBench.class").getFile()); return me.length();        }    },    CHANNEL {        @Override        public long getResult() throws Exception { FileInputStream fis = null; try {     File me = new File(FileSizeBench.class.getResource(  "FileSizeBench.class").getFile());     fis = new FileInputStream(me);     return fis.getChannel().size(); } finally {     fis.close(); }        }    },    URL {        @Override        public long getResult() throws Exception { InputStream stream = null; try {     URL url = FileSizeBench.class  .getResource("FileSizeBench.class");     stream = url.openStream();     return stream.available(); } finally {     stream.close(); }        }    };    public abstract long getResult() throws Exception;    public static void main(String[] args) throws Exception {        int runs = 5;        int iterations = 50;        EnumMap<FileSizeBench, Long> durations = new EnumMap<FileSizeBench, Long>(FileSizeBench.class);        for (int i = 0; i < runs; i++) { for (FileSizeBench test : values()) {     if (!durations.containsKey(test)) {         durations.put(test, 0l);     }     long duration = testNow(test, iterations);     durations.put(test, durations.get(test) + duration);     // System.out.println(test + " took: " + duration + ", per iteration: " + ((double)duration / (double)iterations)); }        }        for (Map.Entry<FileSizeBench, Long> entry : durations.entrySet()) { System.out.println(); System.out.println(entry.getKey() + " sum: " + entry.getValue() + ", per Iteration: " + ((double)entry.getValue() / (double)(runs * iterations)));        }    }    private static long testNow(FileSizeBench test, int iterations) throws Exception {        long result = -1;        long before = System.nanoTime();        for (int i = 0; i < iterations; i++) { if (result == -1) {     result = test.getResult();     //System.out.println(result); } else if ((result = test.getResult()) != result) {      throw new Exception("variance detected!");  }        }        return (System.nanoTime() - before) / 1000;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483913.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号