微基准输出高波动

发布于 2025-01-06 03:10:18 字数 725 浏览 0 评论 0原文

我正在尝试对将二进制文件从一个位置保存到另一个位置所需的速度进行基准测试。

    FileInputStream fis = new FileInputStream("/path/to/binary/file");
    BufferedInputStream in = new BufferedInputStream(fis);

    FileOutputStream fos = new FileOutputStream("/path/to/save/new/binary/file");
    BufferedOutputStream out = new BufferedOutputStream(fos);

    long before = System.currentTimeMillis();
    int data = 0;

    while ((data = in.read()) != -1) {
        out.write(data);
    }

    in.close();
    out.close();

    int seconds = (int) (System.currentTimeMillis() - before / 1000) % 60;

    System.out.println("Took " + seconds);

无论是缓冲还是非缓冲,输出的时间范围为 3 到 64 ms。我本来期望更接近的范围,比如 40-50 或 10-20,或 30-40。造成如此高波动的原因是什么?

I'm trying to benchmark how fast or slow it would take to save a binary file from one location to another.

    FileInputStream fis = new FileInputStream("/path/to/binary/file");
    BufferedInputStream in = new BufferedInputStream(fis);

    FileOutputStream fos = new FileOutputStream("/path/to/save/new/binary/file");
    BufferedOutputStream out = new BufferedOutputStream(fos);

    long before = System.currentTimeMillis();
    int data = 0;

    while ((data = in.read()) != -1) {
        out.write(data);
    }

    in.close();
    out.close();

    int seconds = (int) (System.currentTimeMillis() - before / 1000) % 60;

    System.out.println("Took " + seconds);

Buffered or unbuffered, the output is anywhere from 3 to 64 ms. I would have expected a closer range, say 40-50 or 10-20, or 30-40. What's the cause of this high fluctuation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2025-01-13 03:10:18

您不能通过仅运行单个测试来期望结果的一致性,特别是当它涉及系统调用和文件 I/O 时。

通过采取以下步骤来提高测量的意义。

You can't expect consistency in your results by running only a single test, especially if it involves system calls and file I/O.

Improve the meaningfulness of your measurements by taking the following steps.

他不在意 2025-01-13 03:10:18

文件 I/O 可能涉及很多“随机性”:

  • 这是一个系统调用,因此您基本上要等待 IO 调度程序决定处理您的请求
  • 它可能位于硬盘驱动器上,这需要时间来读取
  • 它可能缓存在内存中,速度很快(呃)
  • 某些其他进程可能正在执行繁重的 I/O
  • 某些其他进程可能正在独占 CPU

查看 cheeken 关于如何处理此问题的建议。

File I/O can have a lot of "randomness" involved:

  • This is a system call, so you're basically waiting until the IO scheduler decides to deal with your request
  • It may be on the hard drive, which takes time to read from
  • It may be cached in memory, which is fast(er)
  • Some other process may be doing heavy I/O
  • Some other process may be monopolizing the CPU

Look at cheeken's suggestions for how to deal with this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文