Reputation: 11741
I want to get an idea (ballpark) of how much time it takes to read a large file (50MB to 100MB) stored on Android's SD card. I'm using the following code on Android 2.3.3 on a Google Nexus One. Will this give me a reasonable estimate ? Is there a standard method that I should be using ? Also, will using native code improve my file I/O performance ?
Thanks.
public void readFileFromSDCard() throws IOException
{
long start = System.currentTimeMillis();
long size = 0;
File file = new File(OVERLAY_PATH_BASE + "base-ubuntu.vdi");
try {
InputStream in = new FileInputStream(file);
try {
byte[] tmp = new byte[4096];
int l;
while ((l = in.read(tmp)) != -1) {
size = size + 4096;
}
} finally {
in.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
Log.e(LOG_TAG,
"Reading file " + file.getAbsolutePath() + " with " + size + "bytes took " + (end-start) + " ms.");
}
Upvotes: 0
Views: 560
Reputation: 2482
The easiest thing to do for this code would be to time how long each while loop iteration takes and use that to estimate how long is left. For example, if you process 0.1Mb in one iteration in one second and you have 1Mb in total to do, you can guess that you have about 9 seconds to go.
SD card performance varies a lot between different phones and the speed can be unpredictable even on the same phone (e.g. I get large random delays on my phone for small files). I don't think it's worth doing anything more sophisticated than the above myself, such as benchmarking the phone before processing the file. A percent bar is probably enough for most uses.
Upvotes: 1