1

I'm looking at using ashmem to help a couple processes coordinate. Before I start spending the effort to use it, I thought I'd see how much of a speedup it offers over a plain 'ol file.

    try {

        Random random = new Random(System.currentTimeMillis());

        byte[] buf = new byte[4096];
        random.nextBytes(buf);

        MemoryFile memfil = new MemoryFile("testmem", LENGTH);

        long start = System.nanoTime();
        OutputStream memOs = memfil.getOutputStream();
        memOs.write(buf);
        Log.e(TAG, "Ashmem   writes took " + (System.nanoTime()-start));
        memfil.close();

        OutputStream fos = new FileOutputStream(new File(this.getFilesDir(), "testfil"));
        start = System.nanoTime();
        fos.write(buf);
        fos.flush();
        Log.e(TAG, "File     writes took " + (System.nanoTime() - start));
        fos.close();
    } catch (IOException e) {
        Log.e(TAG, "Failed to open ashmem: ", e);
    }

Here are some typical numbers

07-30 20:49:01.742    2994-2994/? E/AshmemTestActivity﹕ Ashmem   writes took 214531
07-30 20:49:01.742    2994-2994/? E/AshmemTestActivity﹕ File     writes took 68541

The numbers don't get much better even when I include the time to close the file. How could memory writes be slower than a file on disk? What am I doing wrong?

4

0 回答 0