0

I am developing an app where time-cost of an algorithm matters a lot.

In the algorithm, I need to get path string to a file in assets folder. And I got answer from this question.

The file is a configuration file, which is ~400 bytes in size. The library I used requires path, but not some Java string.

My code is like:

    public static File getCacheFile(String path, Context context) throws IOException {
        File cacheFile = new File(context.getCacheDir(), path);
        try {
            InputStream inputStream = context.getAssets().open(path);
            try {
                FileOutputStream outputStream = new FileOutputStream(cacheFile);
                try {
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0) {
                        outputStream.write(buf, 0, len);
                    }
                } finally {
                    outputStream.close();
                }
            } finally {
                inputStream.close();
            }
        } catch (IOException e) {
            throw new IOException("Could not open file", e);
        }
        return cacheFile;
    }

If I run the algorithm for the first time since I start my app, it will cost ~900ms.

If I run the algorithm again without restarting the app, it will cost ~400ms.

So I guess the time difference is that this function attempts to load the file into cache and read path from the cache? Maybe the file is already in the cache and that is why it is faster.

Is there any way to make it faster? E.g. preload this file to cache in onCreate(), maybe?


Edit: I tried to preload this file in onCreate() and it does not work.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            getCacheFile("a.properties", getApplicationContext());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Edit2: Not sure whether it matters, but my algorithm is posted here.

TaihouKai
  • 133
  • 11
  • Read more than 1024 bytes at a time. – CommonsWare May 16 '21 at 21:48
  • @CommonsWare The file is only ~400 bytes in size. – TaihouKai May 16 '21 at 21:53
  • Rather than fuss with an asset *or* a file, would it make more sense for these values to be Java `static` fields? In other words, does `PairingFactory.getPairingParameters()` *have* to do disk I/O? – CommonsWare May 16 '21 at 22:02
  • @CommonsWare I checked the library documentation (see updated question body) and it seems like it is the only way to implement such configuration. See here: http://gas.dia.unisa.it/projects/jpbc/docs/pairing.html#initializing – TaihouKai May 16 '21 at 22:06
  • `Edit: I tried to preload this file in onCreate() and it does not work.` That is because the dime difference does not come from loading from assets but from creating and writing a file. – blackapps May 17 '21 at 11:56
  • @blackapps In that case, I guess the reason is that in `onCreate()`, the file I created in cache is NOT the file expected by the algorithm. However, after the algorithm being run once, the file already exists in cache and thus it is much faster. Am I correct? If this is the case, is there any way to read the specific file I pre-load into cache in `onCreate()` in the algorithm? – TaihouKai May 17 '21 at 15:51
  • I do not understand a word. – blackapps May 17 '21 at 16:08
  • @blackapps Sorry. I mean, you mentioned that `time difference does not come from loading from assets but from creating and writing a file`. If I have already created such file in `onCreate()`, it does not have to be created/write again if I run the algorithm. However, the fact that this does not work denotes that such file created in `onCreate()` is not used by the algorithm so that the algorithm create/write it again during its first launch. – TaihouKai May 17 '21 at 16:23

0 Answers0