Показать сообщение отдельно
Старый 29.11.2011, 15:20   #48
moka
.
 
Регистрация: 05.08.2006
Сообщений: 10,429
Написано 3,454 полезных сообщений
(для 6,863 пользователей)
Ответ: Великая битва 4х языков программирования на простейшей задачке

Вот снова затестил, такой результат:

Multidimensional allocate: 1.258ms
Multidimensional fill: 701.627ms
Multidimensional get 65536 times: 1.09ms
Flattened allocate: 1.751ms
Flattened fill: 212.648ms
Flattened get 65536 times: 0.331ms
Jagged allocate: 460.847ms
Jagged fill: 286.233ms
Jagged get 65536 times: 0.455ms

Вот код с тремя вариантами:
using System;
using System.Diagnostics;
using System.Threading;

namespace 
speedTest {
    static class 
Program {
        static 
int Main() {
            
int counter = 0;

            
Stopwatch timer = new Stopwatch();

            
Thread.Sleep(100);

            { 
// Multidimensional
                
long elapsedAllocation, elapsedFill, elapsedGet, number;

                
timer.Restart();

                
int[,,] map = new int[512, 128, 512];

                
elapsedAllocation = timer.ElapsedTicks;
                
Console.WriteLine("Multidimensional allocate: {0}ms", Math.Round(elapsedAllocation * 1000f / Stopwatch.Frequency, 3));
                
timer.Restart();

                for (
int i = 0; i < 20; i++) {
                    for (
int x = 0; x < 512; x++) {
                        for (
int y = 0; y < 128; y++) {
                            for (
int z = 0; z < 512; z++) {
                                ++
counter;
                                
map[x, y, z] = counter;
                            }
                        }
                    }
                }

                
elapsedFill = timer.ElapsedTicks;
                
Console.WriteLine("Multidimensional fill: {0}ms", Math.Round((elapsedFill / 20f) * 1000f / Stopwatch.Frequency, 3));
                
timer.Restart();

                for (
int i = 0; i < 65536; ++i) {
                    
number = map[100, 10, 100];
                }

                
elapsedGet = timer.ElapsedTicks;
                
Console.WriteLine("Multidimensional get 65536 times: {0}ms", Math.Round(elapsedGet * 1000f / Stopwatch.Frequency, 3));
            }

            
Thread.Sleep(100);

            { 
// Flattened
                
long elapsedAllocation, elapsedFill, elapsedGet, number;

                
timer.Restart();

                
int[] map = new int[512 * 128 * 512];

                
elapsedAllocation = timer.ElapsedTicks;
                
Console.WriteLine("Flattened allocate: {0}ms", Math.Round(elapsedAllocation * 1000f / Stopwatch.Frequency, 3));
                
timer.Restart();

                for (
int i = 0; i < 20; i++) {
                    for (
int x = 0; x < 512; x++) {
                        for (
int y = 0; y < 128; y++) {
                            for (
int z = 0; z < 512; z++) {
                                ++
counter;
                                
map[512 * 128 * x + 128 * y + z] = counter;
                            }
                        }
                    }
                }

                
elapsedFill = timer.ElapsedTicks;
                
Console.WriteLine("Flattened fill: {0}ms", Math.Round((elapsedFill / 20f) * 1000f / Stopwatch.Frequency, 3));
                
timer.Restart();

                for (
int i = 0; i < 65536; ++i) {
                    
number = map[512*128*100+128*10+100];
                }

                
elapsedGet = timer.ElapsedTicks;
                
Console.WriteLine("Flattened get 65536 times: {0}ms", Math.Round(elapsedGet * 1000f / Stopwatch.Frequency, 3));
            }

            
Thread.Sleep(100);

            { 
// Jagged
                
long elapsedAllocation, elapsedFill, elapsedGet, number;

                
timer.Restart();

                
int[][][] map = new int[512][][];
                for (
int y = 0; y < 512; ++y) {
                    
map[y] = new int[128][];

                    for (
int z = 0; z < 128; ++z) {
                        
map[y][z] = new int[512];
                    }
                }

                
elapsedAllocation = timer.ElapsedTicks;
                
Console.WriteLine("Jagged allocate: {0}ms", Math.Round(elapsedAllocation * 1000f / Stopwatch.Frequency, 3));
                
timer.Restart();

                for (
int i = 0; i < 20; i++) {
                    for (
int x = 0; x < 512; x++) {
                        for (
int y = 0; y < 128; y++) {
                            for (
int z = 0; z < 512; z++) {
                                ++
counter;
                                
map[x][y][z] = counter;
                            }
                        }
                    }
                }

                
elapsedFill = timer.ElapsedTicks;
                
Console.WriteLine("Jagged fill: {0}ms", Math.Round((elapsedFill / 20f) * 1000f / Stopwatch.Frequency, 3));
                
timer.Restart();

                for (
int i = 0; i < 65536; ++i) {
                    
number = map[100][10][100];
                }

                
elapsedGet = timer.ElapsedTicks;
                
Console.WriteLine("Jagged get 65536 times: {0}ms", Math.Round(elapsedGet * 1000f / Stopwatch.Frequency, 3));
            }


            
Console.Read();
            return 
0;
        }
    }
} 
(Offline)
 
Ответить с цитированием
Эти 2 пользователя(ей) сказали Спасибо moka за это полезное сообщение:
HolyDel (29.11.2011), Reks888 (29.11.2011)