1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
int[] tab = new int[229948364];
Random rand = new Random();
Stopwatch stopWatchEn = new Stopwatch();
Stopwatch stopWatchTab = new Stopwatch();
Stopwatch stopWatchTabLen = new Stopwatch();
int lgTab = 0;
//Une première initialisation du tableau pour le charger en mémoire
for (int j = 0; j < tab.Length; j++)
{
tab[j] = 0;
}
//Différentes boucles for pour tester
for (int h = 0; h < 25; h++)
{
stopWatchTab.Start();
lgTab = tab.Length;
for (int j = 0; j < lgTab; j++)//Premier type de boucle : utilisation d'une variable chargé avec la taille du tableau
{
tab[j] = 5;
}
stopWatchTab.Stop();
stopWatchTabLen.Start();
for (int i = 0; i < tab.Length; i++)//Second type : utilisation à chaque tour de la taille "calculée" du tableau
{
tab[i] = 5;
}
stopWatchTabLen.Stop();
stopWatchEn.Start();
for (int j = 0; j < 229948364; j++)//Troisième type : utilisation directe d'un entier correspondant à la taille du tableau
{
tab[j] = 5;
}
stopWatchEn.Stop();
}
stopWatchEn.Stop();
stopWatchTab.Stop();
stopWatchTabLen.Stop();
//Résultats
Console.WriteLine("Voici le résultat des tests :");
Console.WriteLine("Entier : " + stopWatchEn.Elapsed.Milliseconds);
Console.WriteLine("Tab : " + stopWatchTab.Elapsed.Milliseconds);
Console.WriteLine("TabLength : " + stopWatchTabLen.Elapsed.Milliseconds);
} |
Partager