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

用Java逐行读取和写入大文件的最快方法

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

用Java逐行读取和写入大文件的最快方法

我怀疑您的真正问题是您的硬件有限,而您所做的只是软件不会带来太大变化。如果您有足够的内存和CPU,可以使用更高级的技巧,但是如果由于文件未缓存而仅在硬盘上等待,则不会有太大的不同。

BTW:10秒内500 MB或50 MB /秒是HDD的典型读取速度。

尝试运行以下命令以查看系统何时无法有效地缓存文件。

public static void main(String... args) throws IOException {    for (int mb : new int[]{50, 100, 250, 500, 1000, 2000})        testFileSize(mb);}private static void testFileSize(int mb) throws IOException {    File file = File.createTempFile("test", ".txt");    file.deleteonExit();    char[] chars = new char[1024];    Arrays.fill(chars, 'A');    String longLine = new String(chars);    long start1 = System.nanoTime();    PrintWriter pw = new PrintWriter(new FileWriter(file));    for (int i = 0; i < mb * 1024; i++)        pw.println(longLine);    pw.close();    long time1 = System.nanoTime() - start1;    System.out.printf("Took %.3f seconds to write to a %d MB, file rate: %.1f MB/s%n", time1 / 1e9, file.length() >> 20, file.length() * 1000.0 / time1);    long start2 = System.nanoTime();    BufferedReader br = new BufferedReader(new FileReader(file));    for (String line; (line = br.readLine()) != null; ) {    }    br.close();    long time2 = System.nanoTime() - start2;    System.out.printf("Took %.3f seconds to read to a %d MB file, rate: %.1f MB/s%n", time2 / 1e9, file.length() >> 20, file.length() * 1000.0 / time2);    file.delete();}

在具有大量内存的Linux机器上。

Took 0.395 seconds to write to a 50 MB, file rate: 133.0 MB/sTook 0.375 seconds to read to a 50 MB file, rate: 140.0 MB/sTook 0.669 seconds to write to a 100 MB, file rate: 156.9 MB/sTook 0.569 seconds to read to a 100 MB file, rate: 184.6 MB/sTook 1.585 seconds to write to a 250 MB, file rate: 165.5 MB/sTook 1.274 seconds to read to a 250 MB file, rate: 206.0 MB/sTook 2.513 seconds to write to a 500 MB, file rate: 208.8 MB/sTook 2.332 seconds to read to a 500 MB file, rate: 225.1 MB/sTook 5.094 seconds to write to a 1000 MB, file rate: 206.0 MB/sTook 5.041 seconds to read to a 1000 MB file, rate: 208.2 MB/sTook 11.509 seconds to write to a 2001 MB, file rate: 182.4 MB/sTook 9.681 seconds to read to a 2001 MB file, rate: 216.8 MB/s

在具有大量内存的Windows计算机上。

Took 0.376 seconds to write to a 50 MB, file rate: 139.7 MB/sTook 0.401 seconds to read to a 50 MB file, rate: 131.1 MB/sTook 0.517 seconds to write to a 100 MB, file rate: 203.1 MB/sTook 0.520 seconds to read to a 100 MB file, rate: 201.9 MB/sTook 1.344 seconds to write to a 250 MB, file rate: 195.4 MB/sTook 1.387 seconds to read to a 250 MB file, rate: 189.4 MB/sTook 2.368 seconds to write to a 500 MB, file rate: 221.8 MB/sTook 2.454 seconds to read to a 500 MB file, rate: 214.1 MB/sTook 4.985 seconds to write to a 1001 MB, file rate: 210.7 MB/sTook 5.132 seconds to read to a 1001 MB file, rate: 204.7 MB/sTook 10.276 seconds to write to a 2003 MB, file rate: 204.5 MB/sTook 9.964 seconds to read to a 2003 MB file, rate: 210.9 MB/s


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

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

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