views:

874

answers:

8

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,

  • implementing java.io.Serializable and using read/writeObject or
  • overriding toString() / toCSV(), adding a new constructor and reading/writing to text file?



I want to avoid binary stream at this stage.

+3  A: 

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.

Zach Scrivena
+1 On using CSV which will be far more flexible and maintainable in the future (and not tied to one version of your implementation: serialVersionUID woes etc).
Aaron Maenpaa
+2  A: 

I would suggest you to write a sample test and see which one is faster.

Bhushan
That’s probably way too easy. Let’s waste other peoples’ time instead by asking here! \o/
Bombe
+1for testing, either way is the same algorithmic speed, so the answer depends on the constants, which testing is the only way to check
Nick Fortescue
+1  A: 

Depending on the final application, you could also consider using XStream (or similar library) to export/import XML.

Mikezx6r
If you go the XML route (and it's usually not the fastest), then I strongly suggest JAXB. Mostly because it's standard (included in Java 6 SE), has excellent mapping abilities and doesn't mess up your code with generated code.
Joachim Sauer
This assumes Java6. Will be worth investigating when my project moves to Java6 (currently on 5 because of Websphere dependency)
Mikezx6r
+1  A: 

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.

fred-o
+2  A: 

There are plenty of alternatives here. You may want to consider using Protocol Buffers - they're extremely fast, as well as being platform-independent.

Jon Skeet
Unfortunately PB might not be a good fit, since it is strict schema bound, and question suggested that it's just pile of standard types (unless I misunderstood it). Also, one must use PB generated types, not POJOs/wrappers.
StaxMan
+1  A: 

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.

A: 

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.

Fortyrunner
A: 

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).

StaxMan