哇塞,这几种Java文件读写性能差距居然这么大?

引言

这是一篇性能比较的文章,不分析实现原理。主要是对比Java几种常见的文件写入方式

一、测试代码

主要分析Stream、Stream+Buffer和mmap三种方式,对应的大致代码如下

    public static void testBasicFileIO(List<Persona> list, String path) throws Exception {
        File file = new File(path);
        if (file.exists()){
            file.delete();
        }
        FileOutputStream out = new FileOutputStream(file);

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
            out.write(list.get(i).toString().getBytes());
        }
        long costTime = System.currentTimeMillis()-startTime;
        System.out.println("testBasicFileIO cost:"+costTime);
    }
    
    public static void testBufferedFileIO(List<Persona> list, String path) throws Exception {
        File file = new File(path);
        if (file.exists()){
            file.delete();
        }
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));


        long startTime = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
            out.write(list.get(i).toString().getBytes());
        }
        long costTime = System.currentTimeMillis()-startTime;
        System.out.println("testBufferedFileIO cost:"+costTime);
    }
    
    public static void testMmapWrite(List<Persona> list, String path) throws Exception {
        File file = new File(path);
        if (file.exists()){
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(path, "rw");
        FileChannel rafchannel = raf.getChannel();
        //mmap 使得jvm堆和pageCache有一块映射空间
        MappedByteBuffer map = rafchannel.map(FileChannel.MapMode.READ_WRITE, 0, 4096 * 1024 * 250);  // 1000M的pageCache大小

        long startTime = System.currentTimeMillis();

        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i).toString().getBytes());
        }
        long costTime = System.currentTimeMillis()-startTime;
        System.out.println("mmap cost:"+costTime);
    }

二、效果比较

写1M数据效果如下
在这里插入图片描述

写10M数据效果如下
在这里插入图片描述

写100M效果如下
在这里插入图片描述

三、总结

通过比较能看到在高频写数据到磁盘时,mmap的性能非常显著,有类似的场景时可以优先考虑mmap。当然最好是自己做下基准测试

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot应用程序中,可以使用 `application.yml` 或 `application.properties` 文件来配置应用程序属性。如果您正在使用 `application.yml` 文件,您可以使用 `@PropertySource` 注释和 `@ConfigurationProperties` 注释来从 `application.yml` 文件中加载属性。 以下是将 `application.yml` 文件中的属性加载到 Spring Boot 应用程序中的示例: 1. 在 `application.yml` 文件中设置您的属性: ``` myproperty: name: "my name" age: 25 ``` 2. 创建一个类来保存您的属性: ``` @Component @ConfigurationProperties(prefix = "myproperty") public class MyProperty { private String name; private int age; // getters and setters } ``` 3. 在您的应用程序中使用 MyProperty 类: ``` @RestController public class MyController { @Autowired private MyProperty myProperty; @GetMapping("/myproperty") public String getMyProperty() { return "Name: " + myProperty.getName() + ", Age: " + myProperty.getAge(); } } ``` 在上面的示例中,`@ConfigurationProperties` 注释设置了属性的前缀,因此 Spring Boot 将自动从 `application.yml` 文件中加载属性。在 `MyController` 中,我们将 `MyProperty` 类注入并使用其中的属性。 注意事项:如果您使用的是 `application.properties` 文件,则需要将 `@PropertySource` 注释添加到您的类中,并指定属性文件的位置。例如: ``` @Component @PropertySource("classpath:myproperties.properties") @ConfigurationProperties(prefix = "myproperty") public class MyProperty { // ... } ``` 其中,`classpath:myproperties.properties` 是 `application.properties` 文件的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏洛克·林

有钱的捧个钱💰场或人场~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值