java - Fastest way to read long[] from file? -
i have file contains 200,000 long values want read fast possible long[]. it's android app; function calls slow (so involving reading long @ time "for" loop super slow) , need loading fast. can use? @ seems read bytes fast.
i've used bytebuffer
, filechannel
nio package before , seems fast way load arrays of values files. however, cannot work out how use read data long[]. i've tried wrapping long[] longbuffer, cannot see way can feed data file longbuffer.
edit: whatever method use, need able use arrays.binarysearch
on long[]
array @ end.
there no way cast byte[]
long[]
. however, try use filechannel
read content bytebuffer
, hold of longbuffer
through bytebuffer.aslongbuffer
long[]
through longbuffer.array()
.
you try filechannel.map
mappedbytebuffer
of file. may faster going through filechannel.read
.
if doesn't work try use filechannel
read content bytebuffer
, access long
s inside it, using bytebuffer.getlong(index)
.
an alternative solution. (no method-calls in loop :-)
byte[] bytearray = new byte[longcount * 8]; fileinputstream fis = new fileinputstream("lotsoflongs"); fis.read(bytearray); fis.close(); (int = 0; < longcount; += 8) longarray[i >> 3] = ((long) bytearray[0+i] << 56) + ((long)(bytearray[1+i] & 255) << 48) + ((long)(bytearray[2+i] & 255) << 40) + ((long)(bytearray[3+i] & 255) << 32) + ((long)(bytearray[4+i] & 255) << 24) + ((bytearray[5+i] & 255) << 16) + ((bytearray[6+i] & 255) << 8) + ((bytearray[7+i] & 255) << 0);
i've benchmarked few solutions now, , 1 seems fastest way of doing it. also, note actual bytes read in fis.read(bytearray)
may less actual size of bytearray. thus, if should done properly, need put in loop iterates until bytes have been read.
Comments
Post a Comment