Android的方式加快从文件中读取数 [英] Android ways to speed up few readings from files

查看:174
本文介绍了Android的方式加快从文件中读取数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序需要加载一些文件,应用程序启动时。我有6个不同的文件,其中内容INT数组,我需要尽可能快地阅读。其中3含量80K整数,其他200K整数

In my application I need to load some files, when the app starts. I have 6 different files, which content int arrays and I need to read them as fast as possible. 3 of them content 80K ints, other 200K ints

下面是方法,我使用读取文件int数组

Here is method that I'm using to read int array from file

public static int[] loadByMappedBuffer(Context context, String filename) throws IOException{
    FileInputStream fis = context.openFileInput(filename);
    FileChannel ch = fis.getChannel();

    MappedByteBuffer mbuff = ch.map(MapMode.READ_ONLY, 0, ch.size());
    IntBuffer ibuff = mbuff.asIntBuffer();

    int[] array = new int[ibuff.limit()];
    ibuff.get(array);

    fis.close();
    ch.close();

    return array;
}

正如你看到我使用NIO读阵列。
下面是测试结果,当我雷丁单个文件,其中内容80K整数

As you see I'm using nio to read arrays. Here is result of testing, when I'm reding single file, which content 80K ints


  • 索尼爱立信Experia还为104 ms的最佳时间和最差的270毫秒

  • 的Nexus 7的20.14毫秒和48.36
  • 的wors最佳时机
  • 三星Galaxy Note 10.1(2014版)以1.16 ms的最佳时间和最坏的5.82

  • Sony Ericsson Experia with best time of 104 ms and worst of 270 ms
  • Nexus 7 with best time of 20.14 ms and wors of 48.36
  • Samsung Galaxy Note 10.1 (2014 Edition) with best time of 1.16 ms and worst of 5.82

因此​​,对于单个读它工作正常,但是当我在读的所有6个文件(我上面提到的),结果将是继

So for single reading it works fine, but when I'm reading all 6 files (which I mentioned above) the results will be following


  • 索尼爱立信与Experia还1396 ms的最佳时间和最差的3324毫秒

  • 三星Galaxy Note 10.1(2014年版)为58 ms的最佳时间和最差的115毫秒

首先我不明白为什么缓存不踢在一读之后,就像在PC上。

First of all I don't understand why cache don't kicks in after first reading, like on PC.

这时我有2个想法


  1. 找到一个更好的方法来读取文件(但NIO是从文件中读取元的最快方法,因为据我所知)

  2. 使用MT编程,并把文件读线程,这将加快这一点,但仅限于设备,具有与多个核心的CPU,我是正确的?

我heared即异步任务是这样的线程,它可以蜜蜂有助于我的目的是什么?

I had heared that Async Task is something like threads, can it bee helpful for my purpose?

任何想法将AP preciated。在此先感谢

Any ideas will be appreciated. Thanks in advance

推荐答案

线程不仅为并行计算,他们已经习惯了更好的UX,因此用户仍然可以与应用程序进行交互,而它做的东西在后台运行。
AsyncTask的是非常好的,但如果你是短的时间正确地写了一个简单的线程应该会更快。

threads are not only for parallel computing, they're used for better UX, so the user can still interact with the app while it's doing stuff in the background. AsyncTask is very nice, although if you're short on time a simple thread written correctly should be faster.

现在,如果应用程序加载的文件,你可以做到这一点的部件(例如每个文件的10%),显示这些部件,并继续加载,其余的背景。

now, if the app loads files, you can do it in parts (like 10% of every file), show those parts, and continue to load the rest in the background.

我想尝试读取速度是错误的方向在这里,因为这是非常依赖于硬件。有些设备会比较慢,无论你怎么努力..想想UX这对所有的情况确定。

i think trying to read faster is the wrong direction here, because that's very hardware dependent. some devices would be slow no matter how hard you try.. think about a UX that's OK for all cases.

希望帮助

这篇关于Android的方式加快从文件中读取数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆