Lequel de ces blocs de code est le plus performant, et lequel est le plus lisible ? Je suppose que le gain est négligeable, en particulier dans le deuxième bloc. Je suis simplement curieux.
Bloc 1
string height;
string width;
if (myFlag == 1)
{
height = "60%";
width = "60%";
}
else
{
height = "80%";
width = "80%";
}
Bloc n° 2
string height = "80%";
string width = "80%";
if (myFlag == 1)
{
height = "60%";
width = "60%";
}
Mise à jour de
Lorsque j'ai testé le code ci-dessus, les deux blocs ont donné les mêmes résultats.
Bloc 1
myFlag = 1: 3 Milliseconds
myFlag = 0: 3 Milliseconds
Bloc n° 2
myFlag = 1: 3 Milliseconds
myFlag = 0: 3 Milliseconds
Mais une chose importante que j'ai remarquée ici (grâce à Réponse de Matthew Steeples ici ) est que, puisque le bloc de code que j'ai testé n'a pas utilisé les variables hauteur et largeur sauf pour l'affectation dans les blocs if-else et if du bloc de code 1 et 2 respectivement, le compilateur a optimisé le code IL en supprimant complètement les blocs if et if-else en question, montrant ainsi des résultats invalides pour notre test ici. .
J'ai mis à jour les deux blocs de code pour écrire les valeurs de la hauteur et de la largeur dans un fichier, ce qui les utilise à nouveau et force le compilateur à exécuter nos blocs de test (j'espère), mais vous pouvez observer dans le code que la partie d'écriture du fichier n'affecte pas nos résultats de test.
Voici les résultats mis à jour, le code C# et IL
Résultats
Bloc 1
myFlag = 1: 1688 Milliseconds
myFlag = 0: 1664 Milliseconds
Bloc n° 2
myFlag = 1: 1700 Milliseconds
myFlag = 0: 1677 Milliseconds
Code C#.net
Bloc 1
public long WithIfAndElse(int myFlag)
{
Stopwatch myTimer = new Stopwatch();
string someString = "";
myTimer.Start();
for (int i = 0; i < 1000000; i++)
{
string height;
string width;
if (myFlag == 1)
{
height = "60%";
width = "60%";
}
else
{
height = "80%";
width = "80%";
}
someString = "Height: " + height + Environment.NewLine + "Width: " + width;
}
myTimer.Stop();
File.WriteAllText("testifelse.txt", someString);
return myTimer.ElapsedMilliseconds;
}
Bloc n° 2
public long WithOnlyIf(int myFlag)
{
Stopwatch myTimer = new Stopwatch();
string someString = "";
myTimer.Start();
for (int i = 0; i < 1000000; i++)
{
string height = "80%";
string width = "80%";
if (myFlag == 1)
{
height = "60%";
width = "60%";
}
someString = "Height: " + height + Environment.NewLine + "Width: " + width;
}
myTimer.Stop();
File.WriteAllText("testif.txt", someString);
return myTimer.ElapsedMilliseconds;
}
Code IL généré par ildasm.exe
Bloc 1
.method public hidebysig instance int64 WithIfAndElse(int32 myFlag) cil managed
{
// Code size 144 (0x90)
.maxstack 3
.locals init ([0] class [System]System.Diagnostics.Stopwatch myTimer,
[1] string someString,
[2] int32 i,
[3] string height,
[4] string width,
[5] string[] CS$0$0000)
IL_0000: newobj instance void [System]System.Diagnostics.Stopwatch::.ctor()
IL_0005: stloc.0
IL_0006: ldstr ""
IL_000b: stloc.1
IL_000c: ldloc.0
IL_000d: callvirt instance void [System]System.Diagnostics.Stopwatch::Start()
IL_0012: ldc.i4.0
IL_0013: stloc.2
IL_0014: br.s IL_0070
IL_0016: ldarg.1
IL_0017: ldc.i4.1
IL_0018: bne.un.s IL_0029
IL_001a: ldstr "60%"
IL_001f: stloc.3
IL_0020: ldstr "60%"
IL_0025: stloc.s width
IL_0027: br.s IL_0036
IL_0029: ldstr "80%"
IL_002e: stloc.3
IL_002f: ldstr "80%"
IL_0034: stloc.s width
IL_0036: ldc.i4.5
IL_0037: newarr [mscorlib]System.String
IL_003c: stloc.s CS$0$0000
IL_003e: ldloc.s CS$0$0000
IL_0040: ldc.i4.0
IL_0041: ldstr "Height: "
IL_0046: stelem.ref
IL_0047: ldloc.s CS$0$0000
IL_0049: ldc.i4.1
IL_004a: ldloc.3
IL_004b: stelem.ref
IL_004c: ldloc.s CS$0$0000
IL_004e: ldc.i4.2
IL_004f: call string [mscorlib]System.Environment::get_NewLine()
IL_0054: stelem.ref
IL_0055: ldloc.s CS$0$0000
IL_0057: ldc.i4.3
IL_0058: ldstr "Width: "
IL_005d: stelem.ref
IL_005e: ldloc.s CS$0$0000
IL_0060: ldc.i4.4
IL_0061: ldloc.s width
IL_0063: stelem.ref
IL_0064: ldloc.s CS$0$0000
IL_0066: call string [mscorlib]System.String::Concat(string[])
IL_006b: stloc.1
IL_006c: ldloc.2
IL_006d: ldc.i4.1
IL_006e: add
IL_006f: stloc.2
IL_0070: ldloc.2
IL_0071: ldc.i4 0xf4240
IL_0076: blt.s IL_0016
IL_0078: ldloc.0
IL_0079: callvirt instance void [System]System.Diagnostics.Stopwatch::Stop()
IL_007e: ldstr "testifelse.txt"
IL_0083: ldloc.1
IL_0084: call void [mscorlib]System.IO.File::WriteAllText(string,
string)
IL_0089: ldloc.0
IL_008a: callvirt instance int64 [System]System.Diagnostics.Stopwatch::get_ElapsedMilliseconds()
IL_008f: ret
} // end of method frmResearch::WithIfAndElse
Bloc n° 2
.method public hidebysig instance int64 WithOnlyIf(int32 myFlag) cil managed
{
// Code size 142 (0x8e)
.maxstack 3
.locals init ([0] class [System]System.Diagnostics.Stopwatch myTimer,
[1] string someString,
[2] int32 i,
[3] string height,
[4] string width,
[5] string[] CS$0$0000)
IL_0000: newobj instance void [System]System.Diagnostics.Stopwatch::.ctor()
IL_0005: stloc.0
IL_0006: ldstr ""
IL_000b: stloc.1
IL_000c: ldloc.0
IL_000d: callvirt instance void [System]System.Diagnostics.Stopwatch::Start()
IL_0012: ldc.i4.0
IL_0013: stloc.2
IL_0014: br.s IL_006e
IL_0016: ldstr "80%"
IL_001b: stloc.3
IL_001c: ldstr "80%"
IL_0021: stloc.s width
IL_0023: ldarg.1
IL_0024: ldc.i4.1
IL_0025: bne.un.s IL_0034
IL_0027: ldstr "60%"
IL_002c: stloc.3
IL_002d: ldstr "60%"
IL_0032: stloc.s width
IL_0034: ldc.i4.5
IL_0035: newarr [mscorlib]System.String
IL_003a: stloc.s CS$0$0000
IL_003c: ldloc.s CS$0$0000
IL_003e: ldc.i4.0
IL_003f: ldstr "Height: "
IL_0044: stelem.ref
IL_0045: ldloc.s CS$0$0000
IL_0047: ldc.i4.1
IL_0048: ldloc.3
IL_0049: stelem.ref
IL_004a: ldloc.s CS$0$0000
IL_004c: ldc.i4.2
IL_004d: call string [mscorlib]System.Environment::get_NewLine()
IL_0052: stelem.ref
IL_0053: ldloc.s CS$0$0000
IL_0055: ldc.i4.3
IL_0056: ldstr "Width: "
IL_005b: stelem.ref
IL_005c: ldloc.s CS$0$0000
IL_005e: ldc.i4.4
IL_005f: ldloc.s width
IL_0061: stelem.ref
IL_0062: ldloc.s CS$0$0000
IL_0064: call string [mscorlib]System.String::Concat(string[])
IL_0069: stloc.1
IL_006a: ldloc.2
IL_006b: ldc.i4.1
IL_006c: add
IL_006d: stloc.2
IL_006e: ldloc.2
IL_006f: ldc.i4 0xf4240
IL_0074: blt.s IL_0016
IL_0076: ldloc.0
IL_0077: callvirt instance void [System]System.Diagnostics.Stopwatch::Stop()
IL_007c: ldstr "testif.txt"
IL_0081: ldloc.1
IL_0082: call void [mscorlib]System.IO.File::WriteAllText(string,
string)
IL_0087: ldloc.0
IL_0088: callvirt instance int64 [System]System.Diagnostics.Stopwatch::get_ElapsedMilliseconds()
IL_008d: ret
} // end of method frmResearch::WithOnlyIf
On peut donc dire que le Le bloc IF-Else (Bloc #1) s'exécute plus rapidement que le bloc if (Bloc #2) comme l'ont souligné de nombreuses personnes sur ce forum.