Log*_*cat 6 java mapping performance android file
我试图理解我从MappedByteBuffer类的get()方法获得的性能是否正常.我的代码如下:
private byte[] testBuffer = new byte[4194304];
private File sdcardDir, filepath;
private FileInputStream inputStream;
private FileChannel fileChannel;
private MappedByteBuffer mappedByteBuffer;
// Obtain the root folder of the external storage
sdcardDir = Environment.getExternalStorageDirectory();
// Create the reference to the file to be read
filepath = new File(sdcardDir, "largetest.avi");
inputStream = new FileInputStream(filepath);
fileChannel = inputStream.getChannel();
mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (4194304));
Log.d("GFXUnpack", "Starting to read");
mappedByteBuffer.position(0);
mappedByteBuffer.get(testBuffer, 0, (4194304));
Log.d("GFXUnpack", "Ended to read");
mappedByteBuffer.rewind();
Run Code Online (Sandbox Code Playgroud)
由于我是初学者,并且我需要以最快的方式从SD卡读取数据,我查找了文档,我发现在许多情况下,文件映射被认为是从文件中读取的最快方法.但是,如果我运行上面的代码,虽然缓冲区已正确填充,但性能是如此之慢(或者可能不是?你决定!!)我可以在近5秒内读取这些4194304字节,即每秒不到1MB.我正在使用Eclipse直接连接到我的Optimus Dual智能手机; 即使我将读取操作置于循环中,读取也需要相同的时间(如果执行多次读取,则可能不会进行开销初始化...不是这种情况).
如果我减小或使文件变大,则此文件大小 - 时间关系不会改变:将在近9秒内读取8兆,在2秒内读取2兆,依此类推.我已经读过,即使是速度慢的SD卡也能以每秒至少5 MB的速度读取...请注意,4194304是2值的幂,因为我读过这会提高性能.请告诉我你的意见:现代智能手机的实际性能是每秒1MB,还是我的代码有问题?谢谢
归档时间: |
|
查看次数: |
1736 次 |
最近记录: |