Skip to content

Instantly share code, notes, and snippets.

@charleslparker
Last active July 26, 2020 20:09
Show Gist options
  • Save charleslparker/b381d7e39abdd691439e5426e6d80964 to your computer and use it in GitHub Desktop.
Save charleslparker/b381d7e39abdd691439e5426e6d80964 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class SerializeTest {
public static class AnArray implements Serializable {
private static final long serialVersionUID = 1L;
public double[][][] _elements;
public AnArray() {
_elements = null;
}
public AnArray(double[][][] elements) {
_elements = elements;
}
}
@Test
public void testSerialization()
throws JsonGenerationException, JsonMappingException, IOException,
ClassNotFoundException {
File jsonFile = new File("test.json");
File objFile = new File("test.obj");
double[][][] elements = new double[128][256][256];
for (int i = 0; i < elements.length; i++)
for (int j = 0; j < elements[0].length; j++)
for (int k = 0; k < elements[0][0].length; k++)
elements[i][j][k] = Math.random();
AnArray arr = new AnArray(elements);
long startJson = System.currentTimeMillis();
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer();
writer.writeValue(jsonFile, arr);
long jTime = System.currentTimeMillis() - startJson;
System.out.println("Write time to JSON: " + jTime / 1000.0);
long startObj = System.currentTimeMillis();
FileOutputStream fout = new FileOutputStream(objFile);
ObjectOutputStream outStream = new ObjectOutputStream(fout);
outStream.writeObject(arr);
outStream.close();
long oTime = System.currentTimeMillis() - startObj;
System.out.println("Write time to Object: " + oTime / 1000.0);
long startJsonRead = System.currentTimeMillis();
AnArray jArr = mapper.readValue(jsonFile, AnArray.class);
long jReadTime = System.currentTimeMillis() - startJsonRead;
System.out.println("Read time from JSON: " + jReadTime / 1000.0);
long startObjRead = System.currentTimeMillis();
FileInputStream fin = new FileInputStream(objFile);
ObjectInputStream inStream = new ObjectInputStream(fin);
AnArray oArr = (AnArray)inStream.readObject();
inStream.close();
long oReadTime = System.currentTimeMillis() - startObjRead;
System.out.println("Read time from Object: " + oReadTime / 1000.0);
for (int i = 0; i < elements.length; i++) {
for (int j = 0; j < elements.length; j++) {
for (int k = 0; k < elements.length; k++) {
assert elements[i][j][k] == jArr._elements[i][j][k];
assert elements[i][j][k] == oArr._elements[i][j][k];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment