c# - Optimising binary serialization for multi-dimensional generic arrays -
i have class need binary serialize. class contains 1 field below:
private t[,] m_data;
these multi-dimensional arrays can large (hundreds of thousands of elements) , of primitive type. when tried standard .net serialization on object file written disk large , think .net storing lot of repeated data element types , possibly not efficiently done.
i have looked around custom serializers have not seen deal multi-dimensional generic arrays. have experimented built-in .net compression on byte array of memory stream following serializing success, not quick / compressed had hoped.
my question is, should try , write custom serializer optimally serialize array appropriate type (this seems little daunting), or should use standard .net serialization , add compression?
any advice on best approach appreciated, or links resources showing how tackle serialization of multi-dimensional generic array - mentioned existing examples have found not support such structures.
here's came with. code below makes int[1000][10000] , writes out using binaryformatter 2 files - 1 zipped , 1 not.
the zipped file 1.19 mb (1,255,339 bytes) unzipped 38.2 mb (40,150,034 bytes)
int width = 1000; int height = 10000; list<int[]> list = new list<int[]>(); (int = 0; < height; i++) { list.add(enumerable.range(0, width).toarray()); } int[][] bazillionints = list.toarray(); using (filestream fsz = new filestream("c:\\temp_zipped.txt", filemode.create)) using (filestream fs = new filestream("c:\\temp_notzipped.txt", filemode.create)) using (gzipstream gz = new gzipstream(fsz, compressionmode.compress)) { binaryformatter f = new binaryformatter(); f.serialize(gz, bazillionints); f.serialize(fs, bazillionints); }
i can't think of better/easy way this. zipped version pretty damn tight.
i'd go binaryformatter + gzipstream. making custom not fun @ all.
[edit mg] hope won't offended edit, uniform repeated range(0,width) skewing things vastly; change to:
int width = 1000; int height = 10000; random rand = new random(123456); int[,] bazillionints = new int[width, height]; for(int = 0 ; < width;i++) (int j = 0; j < height; j++) { bazillionints[i, j] = rand.next(50000); }
and try it; you'll see temp_notzipped.txt
@ 40mb, temp_zipped.txt
@ 62mb. not appealing...
Comments
Post a Comment