Alright, before i come to my question i want to point out first that i know the difference between Serializable and Exernalizable so you do not need to give an explanation!
What i am basically trying to do is saving a class with all its data in a file. We already have come to the time where Java 9 is out and the JVM is very fast but there are still people (in whose opinions i belive) that using Serializable on a huge amount of data is very inefficient compared to using Exernalizable.
If i would have only like 10 fields which represent ordinary data types like integers or booleans i would definitely use Serializable.
But now i got a little bit more data to store and load, e.g. a 3-Dimensional byte array which contains around 3.3 Million fields and i think it would be very inefficient to save data like this via the reflection-way implemented by the Serializable class. But since i am not 100% sure about the Exernalizable way being more efficient in storing such huge amount of data i would like to ensure myself first before i start using my program because it does not need to save the data fast but load it very fast (and not only one time, it needs to do some calculations first and then load it during the programm multiple times because depending on what state the programm is at it needs to load different datasets). So basically my idea is that i would load the byte-array via asynchronous multithreading in the Externalizable#readExternal() function.
Please correct me if im wrong with my opinion that using Exernalizable here is not the more efficient way because i want the programm to run as fluent as possible when it is loading the data!
King Regards,
Fabian Schmidt!
Basically what i have done now was comparing the time it takes to save/load via reflection/my own implementation.
The code for the test:
Main Class (Comparision.class)
Serialization via reflections:
Seralization via my own implementation:
Basically i can agree with what @markspace said above ("I think the idea that Serializable is slow is fairly old. Modern JVMs like 7 and 8 implement a lot of speed-ups to help Serializable run much faster. I would start with that and only investigate further if it was in fact running slower than acceptable") and also what @EJP said ("I think @markspace is right on the money here. You don't need it to be as fast as possible, you need it to be fast enough. In the old days we had to make sort-merges fast enough so they didn't run into a second operator shift. Any faster than that there was really no payback.")
The problem of the test now is that the results are very confusing and also showing that i definitely will use Externalizable here.
Results from 3 Tests with the same values and exact sizes of datasets i will need later in my project:
What confuses me about this is that the reflection implementation is saving significantly faster than my own implementation but in the opposite it takes around 1 second longer to load the data.
The point now is that this 1 second is very significant for what i am planning to do since the saving does not really matter but the loading has to be done quick. So the outcome clearly shows me that i should use the Externalizable way here.
But can anyone here tell me why exactly the reflection way is saving faster and how i could improve my own implementation of saving the data?
Thanks to all!