Hello everyone,
I'm a french student in C#, I'm a beginner in developpement and I try to understand the mechanics of the C#.
Can you advice to me which code is better and faster to execute and why ? (without tell to me that I've done a function which already exists)
I tried to calculate execution's time but I fail !
Sorry for my bad english.
Code:
char[] voyelle = { 'a', 'e', 'i', 'o', 'u', 'y' };
int[] nbVoyelle = new int[voyelle.Length];
string mot;
int j;
Console.WriteLine("Saisir un mot");
mot = Console.ReadLine();
DateTime InstantDepart = DateTime.Now;
for (int i = 0; i < voyelle.Length; i++)
{
j = 0;
while (j != mot.Length - 1)
{
if (voyelle[i] == mot[j])
nbVoyelle[i]++;
j++;
}
}
for (int i = 0; i < voyelle.Length; i++)
Console.WriteLine("{0} apparait {1} fois.\n", voyelle[i], nbVoyelle[i]);
TimeSpan TempTempsExec = (DateTime.Now - InstantDepart);
Console.WriteLine("temps execution : {0}\n", TempTempsExec);
Console.ReadLine();
Code:
char[] voyelle = { 'a', 'e', 'i', 'o', 'u', 'y' };
int[] nbVoyelle = new int[voyelle.Length];
string mot;
int j;
Console.WriteLine("Saisir un mot");
mot = Console.ReadLine();
DateTime InstantDepart = DateTime.Now;
for (int i = 0; i < voyelle.Length; i++)
{
string copie = mot;
j = 0;
while (j != copie.Length - 1)
{
j = copie.IndexOf(voyelle[i]);
if (j != -1)
{
copie = copie.Substring(j + 1);
nbVoyelle[i]++;
}
else
break;
}
}
for (int i = 0; i < voyelle.Length; i++)
Console.WriteLine("{0} apparait {1} fois.\n", voyelle[i], nbVoyelle[i]);
TimeSpan TempTempsExec = (DateTime.Now - InstantDepart);
Console.WriteLine("temps execution : {0}\n", TempTempsExec);
Console.ReadLine();