Writing a number in .mat gives a different result than a text file

I measure the runtime of my system with ArrayList<Long> runtime. Various entries in the list that I get through System.nanoTime();, which returns a long one.

Now I am writing this ArrayList to a text file, simply by going through the list and using Long.toString();for each record.

So far so good, but now I also create a .mat file with JMatIO (Matlab file format). I do it as follows (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 from the text file (I use format long gin Matlab). For example, in Matlab I get 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
  }
}
+4

Source: https://habr.com/ru/post/1627475/


All Articles