Показать сообщение отдельно
Старый 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 elapsedAllocationelapsedFillelapsedGetnumber;

                
timer.Restart();

                
int[,,] map = new int[512128512];

                
elapsedAllocation timer.ElapsedTicks;
                
Console.WriteLine("Multidimensional allocate: {0}ms"Math.Round(elapsedAllocation 1000f Stopwatch.Frequency3));
                
timer.Restart();

                for (
int i 020i++) {
                    for (
int x 0512x++) {
                        for (
int y 0128y++) {
                            for (
int z 0512z++) {
                                ++
counter;
                                
map[xyz] = counter;
                            }
                        }
                    }
                }

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

                for (
int i 065536; ++i) {
                    
number map[10010100];
                }

                
elapsedGet timer.ElapsedTicks;
                
Console.WriteLine("Multidimensional get 65536 times: {0}ms"Math.Round(elapsedGet 1000f Stopwatch.Frequency3));
            }

            
Thread.Sleep(100);

            { 
// Flattened
                
long elapsedAllocationelapsedFillelapsedGetnumber;

                
timer.Restart();

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

                
elapsedAllocation timer.ElapsedTicks;
                
Console.WriteLine("Flattened allocate: {0}ms"Math.Round(elapsedAllocation 1000f Stopwatch.Frequency3));
                
timer.Restart();

                for (
int i 020i++) {
                    for (
int x 0512x++) {
                        for (
int y 0128y++) {
                            for (
int z 0512z++) {
                                ++
counter;
                                
map[512 128 128 z] = counter;
                            }
                        }
                    }
                }

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

                for (
int i 065536; ++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.Frequency3));
            }

            
Thread.Sleep(100);

            { 
// Jagged
                
long elapsedAllocationelapsedFillelapsedGetnumber;

                
timer.Restart();

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

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

                
elapsedAllocation timer.ElapsedTicks;
                
Console.WriteLine("Jagged allocate: {0}ms"Math.Round(elapsedAllocation 1000f Stopwatch.Frequency3));
                
timer.Restart();

                for (
int i 020i++) {
                    for (
int x 0512x++) {
                        for (
int y 0128y++) {
                            for (
int z 0512z++) {
                                ++
counter;
                                
map[x][y][z] = counter;
                            }
                        }
                    }
                }

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

                for (
int i 065536; ++i) {
                    
number map[100][10][100];
                }

                
elapsedGet timer.ElapsedTicks;
                
Console.WriteLine("Jagged get 65536 times: {0}ms"Math.Round(elapsedGet 1000f Stopwatch.Frequency3));
            }


            
Console.Read();
            return 
0;
        }
    }

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