1

I have created a 256MB RAM Disk using ImDisk, and compared reading/writing speeds of the HDD and Ram Disk with the simple benchmark software CrystalDiskMark. CrystalDiskMark shows the RAM Disk is 10 times faster than the HDD, as it would be expected.

However, I tried saving data programmatically to the RAM Disk and HDD, expecting the RAM Disk to be faster, and the time taken is the same for both.

I wrote the following code in Java, created a JAR executable, and ran the executable in the HDD and Ram Disk. This code creates a 16x16 black image, saves 100 different PNG file copies of it locally, and prints in the terminal the time taken to save each file, the average, and the total time.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class SaveImageSpeedTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_3BYTE_BGR);

        long n = 100;
        long total_time = 0;
        for(long i=0; i<n; i++) {
            try {
                long time = System.nanoTime();
                ImageIO.write(img, "PNG", new File(i+".png"));
                time = System.nanoTime()-time;
                total_time += time;
                System.out.println((i+1)+"/"+n+" - "+"Time: "+(time/1000000L)+" ms");
            } catch (Exception ex) {
                Logger.getLogger(SaveImageSpeedTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        long average_time = total_time/n;
        System.out.println();
        System.out.println("Total time: "+(total_time/1000000L)+" ms");
        System.out.println("Average time: "+(average_time/1000000L)+" ms");
    }
}

How is this possible? Why isn't it faster, if the benchmark shows the RAM Disk is 10 times faster? I also tried saving data with a C++ executable and again, the times were the same.

Edit 1: By changing RAM Disk's format from NTFS to FAT, I get almost twice more speed, but it should be 10 times more.

Edit 2: I tried different image sizes and even making random images (to avoid caché data). Always the same results: Ram DISK is barely twice as fast.

user2208447
  • 131
  • 2
  • 9

1 Answers1

3

When you write to a file, it does not yet mean that the data is actually written to a disk. Operating Systems typically use buffer cache to defer writes to the device.

So, your benchmark basically measures the performance of image encoding plus writing to a buffer cache. If you want to measure actual device I/O, the performance test should at least involve FileChannel.force (which calls fsync under the hood).

Besides that, exclude image encoding from the benchmark, as it also may take significant amount of time.

apangin
  • 92,924
  • 10
  • 193
  • 247