Reputation: 4447
I have a large array of Java simple structs (consists of just primitive members) and I need to save and load them from and to a file
What would be faster,
I want to avoid binary stream at this stage.
Upvotes: 0
Views: 4014
Reputation: 116502
Why limit yourself to these choices? Obvious candidate would be JSON, using simple data binding with Jackson or Google-GSON. Quite readable, and faster than hand-written CSV; plus handles cases that CSV can not. Possibly faster than JDK serialization too, and more flexible (serialization is tricky if/when classes change in even smallest of ways).
Upvotes: 0
Reputation: 12782
I've done quite a lot with serialization that I could have done with CSV files.
The benefits of serialization are speed, type safety and interoperability (RMI, EHcache etc). The downsides are having to version objects and having to write viewers (to enable inspection).
CSVs are slower and can be larger in size. The upside is you can view them in text editors, Excel etc.
I'd go for CSV unless speed or size is an issue.
You could also use XStream and write them as XML or JSON.
Upvotes: 0
Reputation:
It probably doesn't matter. If the array is small enough to hold in memory, it can probably be written very quickly. If the application writes the data only once then I would just write whatever was easiest.
If, on the other hand, the application is long lived and constantly reads and writes data then you'll want to optimise.
Upvotes: 1
Reputation: 1500055
There are plenty of alternatives here. You may want to consider using Protocol Buffers - they're extremely fast, as well as being platform-independent.
Upvotes: 3
Reputation: 1352
Java Serialization does a lot of things under the hood that will probably make the actual deserialization step faster than reading from a file and instantiating your objects manually. However, there are a lot of subtle gotchas to take into account and if you are going to use serialization for 'serious' work you should probably consult Josh Bloch's 'Effective Java' first, as he devotes an entire chapter to this topic.
However, if you are doing this as a one-shot and don't expect the format of the serialized classes to change (no adding methods or fields for example) between executions you'll probably be fine.
I might have misunderstood you question, though. 'What would be faster' could also mean that you wonder which of the approaches would be faster to implement. Probably the one you're already most familiar with.
Upvotes: 1
Reputation: 16905
Depending on the final application, you could also consider using XStream (or similar library) to export/import XML.
Upvotes: 1
Reputation: 10987
I would suggest you to write a sample test and see which one is faster.
Upvotes: 4
Reputation: 29539
Serialization is usually faster. You save time parsing the file manually when reading it back in. However, the CSV approach would be more flexible over time and different implementations of your program.
Upvotes: 5