void Compression(StreamWriter writer, Chunk chunk, int blockTypes, int probabiltyOfChange) { const int iters = 100; ChunkBlocks blocks = chunk.Blocks; // Initialize the block array. Padded area contains zeros, the rest is random { Random r = new Random(0); ushort type = (ushort)r.Next(0, blockTypes); int index = 0; for (int y = 0; y < Env.CHUNK_SIZE; ++y) { for (int z = 0; z < Env.CHUNK_SIZE; ++z) { for (int x = 0; x < Env.CHUNK_SIZE; ++x, ++index) { int prob = r.Next(0, 99); if (prob < probabiltyOfChange) { type = (ushort)r.Next(0, blockTypes); } blocks.SetRaw(index, new BlockData(type)); } } } } if (s_verifyBlocks == null) { s_verifyBlocks = new ChunkBlocks(null, chunk.SideSize); } s_verifyBlocks.Copy(blocks, 0, 0, ChunkBlocks.GetLength(chunk.SideSize)); { Debug.LogFormat("Bechmark - compression ({0} block types, probability of change: {1})", blockTypes, probabiltyOfChange); // Compression { float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { blocks.Compress(); }, iters); t2 = t / iters; int memSizeCompressed = blocks.BlocksCompressed.Count * StructSerialization.TSSize <BlockDataAABB> .ValueSize; int memSizeUncompressed = Env.CHUNK_SIZE_WITH_PADDING_POW_3 * StructSerialization.TSSize <BlockData> .ValueSize; float compressionFactor = memSizeCompressed / (float)memSizeUncompressed; output = string.Format("Compression\nout:{0}, boxes created: {1}, mem: {2}/{3} (factor:{4})\ntime:{5} | {6} ms", number[0], blocks.BlocksCompressed.Count, memSizeCompressed, memSizeUncompressed, compressionFactor, t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } } // Decompression { float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { blocks.Decompress(); }, iters); t2 = t / iters; output = string.Format("Decompression\nout:{0}\ntime:{1} | {2} ms", number[0], t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } } // Verify that data has not changed for (int i = 0; i < ChunkBlocks.GetLength(chunk.SideSize); i++) { Assert.IsTrue(s_verifyBlocks.Get(i) == chunk.Blocks.Get(i)); } } }
void Compression(StreamWriter writer, Chunk chunk, int blockTypes, int probabiltyOfChange) { const int iters = 100; var blocks = chunk.blocks; // Initialize the block array. Padded area contains zeros, the rest is random { Random r = new Random(0); ushort type = (ushort)r.Next(0, blockTypes); int index = 0; for (int y = 0; y < Env.ChunkSize; ++y) { for (int z = 0; z < Env.ChunkSize; ++z) { for (int x = 0; x < Env.ChunkSize; ++x, ++index) { int prob = r.Next(0, 99); if (prob < probabiltyOfChange) { type = (ushort)r.Next(0, blockTypes); } blocks.SetRaw(index, new BlockData(type)); } } } } if (s_verifyBlocks == null) { s_verifyBlocks = new ChunkBlocks(null, chunk.SideSize); } s_verifyBlocks.Copy(blocks, 0, 0, ChunkBlocks.GetLength(chunk.SideSize)); StringBuilder s = new StringBuilder(); { s.AppendFormat("Bechmark - compression ({0} block types, probability of change: {1})", blockTypes, probabiltyOfChange); Debug.Log(s); writer.WriteLine(s); // Compression { float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { blocks.Compress(); }, iters); int memSizeCompressed = blocks.BlocksCompressed.Count * StructSerialization.TSSize <BlockDataAABB> .ValueSize; int memSizeUncompressed = Env.ChunkSizeWithPaddingPow3 * StructSerialization.TSSize <BlockData> .ValueSize; float compressionFactor = memSizeCompressed / (float)memSizeUncompressed; s.Remove(0, s.Length); s.AppendFormat("Compression -> out:{0}, time:{1} ms, boxes created: {2}, mem: {3}/{4} (factor:{5})", number[0], (t / iters).ToString(CultureInfo.InvariantCulture), blocks.BlocksCompressed.Count, memSizeCompressed, memSizeUncompressed, compressionFactor); } Debug.Log(s); writer.WriteLine(s); // Decompression { float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { blocks.Decompress(); }, iters); s.Remove(0, s.Length); s.AppendFormat("Decompression -> out:{0}, time:{1} ms", number[0], (t / iters).ToString(CultureInfo.InvariantCulture)); } Debug.Log(s); writer.WriteLine(s); // Verify that data has not changed for (int i = 0; i < ChunkBlocks.GetLength(chunk.SideSize); i++) { Assert.IsTrue(s_verifyBlocks.Get(i) == chunk.blocks.Get(i)); } } }