public void Heap_Buffer_Index_Access_Is_Fast_Enough() { var array = new float[1024]; var buffer = new FloatHeapBlock(1024); var arrayTime = Clock.BenchmarkTime(() => { for (var i = 0; i != 1024; ++i) { if (array[i] != 0) { Assert.Fail("What?!?"); } } }); var bufferTime = Clock.BenchmarkTime(() => { for (var i = 0; i != 1024; ++i) { if (buffer[i] != 0) { Assert.Fail("What?!?"); } } }); Console.WriteLine("array: {0} vs buffer: {1}", arrayTime, bufferTime); }
void Benchmark_Modulus3() { const int iters = 1000000; Debug.Log("Bechmark - mod3"); using (StreamWriter writer = File.CreateText("perf_mod3.txt")) { uint[] number = { 0 }; double t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = number[0] % 3; }, iters ); Debug.LogFormat("Mod3 -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("Mod3 -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = Helpers.Mod3(number[0]); }, iters ); Debug.LogFormat("Mod3 mersenne -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("Mod3 mersenne -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); } }
void Benchmark_1D_to_3D_Index() { const int iters = 1000000; Debug.Log("Bechmark - 1D to 3D index calculation"); using (StreamWriter writer = File.CreateText("perf_1d_to_3d_index.txt")) { int[] number = { 0, 0, 0, 0 }; double t = Clock.BenchmarkTime( () => { ++number[1]; ++number[2]; ++number[3]; number[0] = Helpers.GetIndex1DFrom3D(number[1], number[2], number[3], Env.ChunkSize, Env.ChunkSize); }, iters ); Debug.LogFormat("GetIndex1DFrom3D -> out:{0}, x:{1},y:{2},z:{3}, time:{4}", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("GetIndex1DFrom3D -> out:{0}, x:{1},y:{2},z:{3}, time:{4}", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; t = Clock.BenchmarkTime( () => { ++number[1]; ++number[2]; ++number[3]; number[0] = Helpers.GetIndex1DFrom3D(number[1], number[2], number[3], 33, 33); }, iters ); Debug.LogFormat("GetIndex1DFrom3D non_pow_of_2 -> out:{0}, x:{1},y:{2},z:{3}, time:{4}", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("GetIndex1DFrom3D non_pow_of_2 -> out:{0}, x:{1},y:{2},z:{3}, time:{4}", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; t = Clock.BenchmarkTime( () => { ++number[1]; ++number[2]; ++number[3]; number[0] = Helpers.GetChunkIndex1DFrom3D(number[1], number[2], number[3]); }, iters ); Debug.LogFormat("GetChunkIndex1DFrom3D -> out:{0}, x:{1},y:{2},z:{3}, time:{4}", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("GetChunkIndex1DFrom3D -> out:{0}, x:{1},y:{2},z:{3}, time:{4}", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture)); } }
void Benchmark_Noise() { const int iters = 10; FastNoise noise = new FastNoise(0); Debug.Log("Bechmark - 1D, 2D, 3D noise"); using (StreamWriter writer = File.CreateText("perf_noise.txt")) { float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { 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++) { number[0] += noise.SingleSimplex(0, x, z); } } } }, iters); t2 = t / iters; output = string.Format("noise.Generate 2D\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); } number[0] = 0; t = Clock.BenchmarkTime( () => { 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++) { number[0] += noise.SingleSimplex(0, x, y, z); } } } }, iters); t2 = t / iters; output = string.Format("noise.Generate 3D\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); } } }
public void Compare_C_Lib() { var ptr1 = Marshal.AllocHGlobal(1024); var clib = Clock.BenchmarkTime(() => Memory.ZeroMem(ptr1.ToPointer(), 1024), 100000); var alt = Clock.BenchmarkTime(() => Memory._ZeroMem(ptr1, 1024), 100000); Console.WriteLine("clib: {0} vs alt: {1}", clib, alt); Marshal.FreeHGlobal(ptr1); }
void Benchmark_Noise() { const int iters = 10; FastNoise noise = new FastNoise(0); Debug.Log("Bechmark - 1D, 2D, 3D noise"); using (StreamWriter writer = File.CreateText("perf_noise.txt")) { float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { for (int y = 0; y < Env.ChunkSize; y++) { for (int z = 0; z < Env.ChunkSize; z++) { for (int x = 0; x < Env.ChunkSize; x++) { number[0] += noise.SingleSimplex(0, x, z); } } } }, iters); Debug.LogFormat("noise.Generate 2D -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("noise.Generate 2D -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; t = Clock.BenchmarkTime( () => { for (int y = 0; y < Env.ChunkSize; y++) { for (int z = 0; z < Env.ChunkSize; z++) { for (int x = 0; x < Env.ChunkSize; x++) { number[0] += noise.SingleSimplex(0, x, y, z); } } } }, iters); Debug.LogFormat("noise.Generate 3D -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("noise.Generate 3D -> out:{0}, time:{1} ms", number[0], t.ToString(CultureInfo.InvariantCulture)); } }
void Benchmark_Modulus3() { const int iters = 1000000; Debug.Log("Bechmark - mod3"); using (StreamWriter writer = File.CreateText("perf_mod3.txt")) { uint[] number = { 0 }; double t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = number[0] % 3; }, iters ); t2 = t / iters; output = string.Format("Mod3\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); } number[0] = 0; t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = Helpers.Mod3(number[0]); }, iters ); t2 = t / iters; output = string.Format("Mod3 mersenne\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); } } }
void Benchmark_Noise() { Noise noise = new Noise("benchmark"); Debug.Log("Bechmark - 1D, 2D, 3D noise"); using (StreamWriter writer = File.CreateText("perf_noise.txt")) { float[] number = { 0, 0, 0, 0 }; double t = Clock.BenchmarkTime(() => { number[1] += 1.0f; number[0] += noise.Generate(number[1]); }, 1000000); Debug.LogFormat("noise.Generate 1D -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("noise.Generate 1D -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; t = Clock.BenchmarkTime(() => { number[1] += 1.0f; number[2] += 1.0f; number[0] += noise.Generate(number[1], number[2]); }, 1000000); Debug.LogFormat("noise.Generate 2D -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("noise.Generate 2D -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; t = Clock.BenchmarkTime(() => { number[1] += 1.0f; number[2] += 1.0f; number[3] += 1.0f; number[0] += noise.Generate(number[1], number[2], number[3]); }, 1000000); Debug.LogFormat("noise.Generate 3D -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("noise.Generate 3D -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); } }
void Benchmark_AbsValue() { Debug.Log("Bechmark - abs"); using (StreamWriter writer = File.CreateText("perf_abs.txt")) { int[] number = { 0 }; double t = Clock.BenchmarkTime(() => { ++number[0]; number[0] = Mathf.Abs(number[0]); }, 1000000 ); Debug.LogFormat("Mathf.abs -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("Mathf.abs -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; t = Clock.BenchmarkTime(() => { ++number[0]; number[0] = Math.Abs(number[0]); }, 1000000 ); Debug.LogFormat("Math.abs -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("Math.abs -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; t = Clock.BenchmarkTime(() => { ++number[0]; number[0] = number[0] < 0 ? -number[0] : number[0]; }, 1000000 ); Debug.LogFormat("i < 0 ? -i : i -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("i < 0 ? -i : i -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; t = Clock.BenchmarkTime(() => { ++number[0]; int mask = number[0] >> 31; number[0] = (number[0] + mask) ^ mask; }, 1000000 ); Debug.LogFormat("(i + mask) ^ mask -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("(i + mask) ^ mask -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; t = Clock.BenchmarkTime(() => { ++number[0]; number[0] = (number[0] + (number[0] >> 31)) ^ (number[0] >> 31); }, 1000000 ); Debug.LogFormat("(i + (i >> 31)) ^ (i >> 31) -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("(i + (i >> 31)) ^ (i >> 31) -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); number[0] = 0; t = Clock.BenchmarkTime(() => { ++number[0]; number[0] = Helpers.Abs(number[0]); }, 1000000 ); Debug.LogFormat("Helpers.abs -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); writer.WriteLine("Helpers.Abs -> out:{0}, time:{1}", number[0], t.ToString(CultureInfo.InvariantCulture)); } }
void Benchmark_AbsValue() { const int iters = 1000000; Debug.Log("Bechmark - abs"); using (StreamWriter writer = File.CreateText("perf_abs.txt")) { int[] number = { 0 }; double t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = Mathf.Abs(number[0]); }, iters ); t2 = t / iters; output = string.Format("Mathf.abs\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); } number[0] = 0; t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = Math.Abs(number[0]); }, iters ); t2 = t / iters; output = string.Format("Math.abs\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); } number[0] = 0; t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = Helpers.Abs(number[0]); }, iters ); t2 = t / iters; output = string.Format("Helpers.Abs\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); } number[0] = 0; t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = number[0] < 0 ? -number[0] : number[0]; }, iters ); t2 = t / iters; output = string.Format("i < 0 ? -i : i\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); } number[0] = 0; t = Clock.BenchmarkTime( () => { ++number[0]; int mask = number[0] >> 31; number[0] = (number[0] + mask) ^ mask; }, iters ); t2 = t / iters; output = string.Format("(i + mask) ^ mask\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); } number[0] = 0; t = Clock.BenchmarkTime( () => { ++number[0]; number[0] = (number[0] + (number[0] >> 31)) ^ (number[0] >> 31); }, iters ); t2 = t / iters; output = string.Format("(i + (i >> 31)) ^ (i >> 31)\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); } } }
void Benchmark_MemCopy() { int[] memItems = { 32, 64, 128, 256, Env.CHUNK_SIZE_WITH_PADDING_POW_2, Env.CHUNK_SIZE_POW_3 }; int[] iters = { 1000000, 1000000, 50000, 50000, 10000, 5000 }; Debug.Assert(memItems.Length == iters.Length); int maxItems = memItems[memItems.Length - 1]; byte[] bd1 = Helpers.CreateArray1D <byte>(maxItems * StructSerialization.TSSize <BlockData> .ValueSize); for (int i = 0; i < bd1.Length; i++) { bd1[i] = 1; } bd2 = Helpers.CreateArray1D <BlockData>(maxItems); for (int i = 0; i < bd2.Length; i++) { bd2[i] = new BlockData(0x101); } BlockData dummy = new BlockData(0x101); TestClass1 tc1 = new TestClass1(); TestClass2 tc2 = new TestClass2(); Debug.Log("Bechmark - memory copy"); using (StreamWriter writer = File.CreateText("perf_memcpy.txt")) { for (int i = 0; i < iters.Length; i++) { int loops = iters[i]; int items = memItems[i]; uint bytes = (uint)items * (uint)StructSerialization.TSSize <BlockData> .ValueSize; Debug.LogFormat("Bytes to copy: {0}", bytes); writer.WriteLine("Bytes to copy: {0}", bytes); { float[] number = { 0 }; t = Clock.BenchmarkTime( () => { tc1.Copy(bd1, 0, 0, bytes); }, loops); t2 = t / loops; output = string.Format("MemoryCopy\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); } } for (int j = 0; j < items; j++) { Assert.IsTrue(tc1[j] == dummy); } { float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { tc2.Copy(bd2, 0, 0, items); }, loops); t2 = t / loops; output = string.Format("ArrayCopy\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); } } for (int j = 0; j < items; j++) { Assert.IsTrue(tc2[j] == dummy); } } } }
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 Benchmark_Noise_Dowsampling() { const int iters = 10; FastNoise noise = new FastNoise(0); Debug.Log("Bechmark - 1D, 2D, 3D noise downsampling"); using (StreamWriter writer = File.CreateText("perf_noise_downsampling.txt")) { for (int i = 1; i <= 3; i++) { NoiseItem ni = new NoiseItem { noiseGen = new NoiseInterpolator() }; ni.noiseGen.SetInterpBitStep(Env.CHUNK_SIZE, i); ni.lookupTable = Helpers.CreateArray1D <float>(ni.noiseGen.Size + 1); float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { PrepareLookupTable1D(noise, ni); for (int x = 0; x < Env.CHUNK_SIZE; x++) { number[0] += ni.noiseGen.Interpolate(x, ni.lookupTable); } }, iters); t2 = t / iters; output = string.Format("noise.Generate 1D\nout:{0}, downsample factor {1}\ntime:{2} | {3} ms", number[0], i, t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } } for (int i = 1; i <= 3; i++) { NoiseItem ni = new NoiseItem { noiseGen = new NoiseInterpolator() }; ni.noiseGen.SetInterpBitStep(Env.CHUNK_SIZE, i); ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1)); float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { PrepareLookupTable2D(noise, ni); for (int z = 0; z < Env.CHUNK_SIZE; z++) { for (int x = 0; x < Env.CHUNK_SIZE; x++) { number[0] += ni.noiseGen.Interpolate(x, z, ni.lookupTable); } } }, iters); t2 = t / iters; output = string.Format("noise.Generate 2D\nout:{0}, downsample factor {1}\ntime:{2} | {3} ms", number[0], i, t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } } for (int i = 1; i <= 3; i++) { NoiseItem ni = new NoiseItem { noiseGen = new NoiseInterpolator() }; ni.noiseGen.SetInterpBitStep(Env.CHUNK_SIZE, i); ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1)); float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { PrepareLookupTable3D(noise, ni); 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++) { number[0] += ni.noiseGen.Interpolate(x, y, z, ni.lookupTable); } } } }, iters); t2 = t / iters; output = string.Format("noise.Generate 3D\nout:{0}, downsample factor {1}\ntime:{2} | {3} ms", number[0], i, t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } } } }
void Benchmark_1D_to_3D_Index() { const int iters = 1000000; Debug.Log("Bechmark - 1D to 3D index calculation"); using (StreamWriter writer = File.CreateText("perf_1d_to_3d_index.txt")) { int[] number = { 0, 0, 0, 0 }; double t = Clock.BenchmarkTime( () => { ++number[1]; ++number[2]; ++number[3]; number[0] = Helpers.GetIndex1DFrom3D(number[1], number[2], number[3], Env.CHUNK_SIZE, Env.CHUNK_SIZE); }, iters ); t2 = t / iters; output = string.Format("GetIndex1DFrom3D\nout:{0}, x:{1},y:{2},z:{3}\ntime:{4} | {5} ms", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; t = Clock.BenchmarkTime( () => { ++number[1]; ++number[2]; ++number[3]; number[0] = Helpers.GetIndex1DFrom3D(number[1], number[2], number[3], 33, 33); }, iters ); t2 = t / iters; output = string.Format("GetIndex1DFrom3D non_pow_of_2\nout:{0}, x:{1},y:{2},z:{3}\ntime:{4} | {5} ms", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } number[0] = 0; number[1] = 0; number[2] = 0; number[3] = 0; t = Clock.BenchmarkTime( () => { ++number[1]; ++number[2]; ++number[3]; number[0] = Helpers.GetChunkIndex1DFrom3D(number[1], number[2], number[3]); }, iters ); t2 = t / iters; output = string.Format("GetChunkIndex1DFrom3D\nout:{0}, x:{1},y:{2},z:{3}\ntime:{4} | {5} ms", number[0], number[1], number[2], number[3], t.ToString(CultureInfo.InvariantCulture), t2.ToString(CultureInfo.InvariantCulture)); Debug.Log(output); foreach (string s in output.Split('\n')) { writer.WriteLine(s); } } }
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)); } } }
void Benchmark_Noise_Dowsampling() { const int iters = 10; FastNoise noise = new FastNoise(0); Debug.Log("Bechmark - 1D, 2D, 3D noise downsampling"); using (StreamWriter writer = File.CreateText("perf_noise_downsampling.txt")) { for (int i = 1; i <= 3; i++) { NoiseItem ni = new NoiseItem { noiseGen = new NoiseInterpolator() }; ni.noiseGen.SetInterpBitStep(Env.ChunkSize, i); ni.lookupTable = Helpers.CreateArray1D <float>(ni.noiseGen.Size + 1); float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { PrepareLookupTable1D(noise, ni); for (int x = 0; x < Env.ChunkSize; x++) { number[0] += ni.noiseGen.Interpolate(x, ni.lookupTable); } }, iters); Debug.LogFormat("noise.Generate 1D -> out:{0}, time:{1} ms, downsample factor {2}", number[0], t.ToString(CultureInfo.InvariantCulture), i); writer.WriteLine("noise.Generate 1D -> out:{0}, time:{1} ms, downsample factor {2}", number[0], t.ToString(CultureInfo.InvariantCulture), i); } for (int i = 1; i <= 3; i++) { NoiseItem ni = new NoiseItem { noiseGen = new NoiseInterpolator() }; ni.noiseGen.SetInterpBitStep(Env.ChunkSize, i); ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1)); float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { PrepareLookupTable2D(noise, ni); for (int z = 0; z < Env.ChunkSize; z++) { for (int x = 0; x < Env.ChunkSize; x++) { number[0] += ni.noiseGen.Interpolate(x, z, ni.lookupTable); } } }, iters); Debug.LogFormat("noise.Generate 2D -> out:{0}, time:{1} ms, downsample factor {2}", number[0], t.ToString(CultureInfo.InvariantCulture), i); writer.WriteLine("noise.Generate 2D -> out:{0}, time:{1} ms, downsample factor {2}", number[0], t.ToString(CultureInfo.InvariantCulture), i); } for (int i = 1; i <= 3; i++) { NoiseItem ni = new NoiseItem { noiseGen = new NoiseInterpolator() }; ni.noiseGen.SetInterpBitStep(Env.ChunkSize, i); ni.lookupTable = Helpers.CreateArray1D <float>((ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1) * (ni.noiseGen.Size + 1)); float[] number = { 0 }; double t = Clock.BenchmarkTime( () => { PrepareLookupTable3D(noise, ni); for (int y = 0; y < Env.ChunkSize; y++) { for (int z = 0; z < Env.ChunkSize; z++) { for (int x = 0; x < Env.ChunkSize; x++) { number[0] += ni.noiseGen.Interpolate(x, y, z, ni.lookupTable); } } } }, iters); Debug.LogFormat("noise.Generate 3D -> out:{0}, time:{1} ms, downsample factor {2}", number[0], t.ToString(CultureInfo.InvariantCulture), i); writer.WriteLine("noise.Generate 3D -> out:{0}, time:{1} ms, downsample factor {2}", number[0], t.ToString(CultureInfo.InvariantCulture), i); } } }