I'm measuring the runtime of my system with an ArrayList<Long> runtime
. The different entries of the list I'm getting through System.nanoTime();
which returns a long.
I'm now writing this ArrayList to a text file by just looping over the list and using Long.toString();
for each entry.
So far so good, but now I also create a .mat file with JMatIO (Matlab file format). I do this in the following way (remember ArrayList<Long> runtime
):
// Convert ArrayList to array because JMatIO only accepts arrays
long[] runtimeArr = new long[runtime.size()];
int i = 0;
for (long a : runtime) {
runtimeArr[i] = a;
i++;
}
// Write to .mat file.
ArrayList<MLArray> list = new ArrayList<MLArray>();
MLInt64 m = new MLInt64("runtime", runtimeArr, 1);
list.add(m);
new MatFileWriter("runtime.mat", list); // Creates .mat
If I open the .mat file in Matlab the displayed numbers are slightly different than in the text file (I'm using format long g
in Matlab). For example in Matlab I'm getting 1988336630 and in the text file 1993496993.
Why is this?
Edit: Here is a complete example
public class Main {
public static void main(String[] args) {
ArrayList<Long> runtime = new ArrayList<Long>();
Thread.sleep(2000);
runtime.add(System.nanoTime());
File file = new File("filename.txt");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(Long.toString(runtime.get(0)));
bw.close();
// Convert ArrayList to array because JMatIO only accepts arrays
long[] runtimeArr = new long[runtime.size()];
int i = 0;
for (long a : runtime) {
runtimeArr[i] = a;
i++;
}
// Write to .mat file.
ArrayList<MLArray> list = new ArrayList<MLArray>();
MLInt64 m = new MLInt64("runtime", runtimeArr, 1);
list.add(m);
new MatFileWriter("runtime.mat", list); // Creates .mat
}
}