Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
¿Mi compilador ignorará el código inútil?

Revisé algunas preguntas en la red sobre este tema, pero no encontré ninguna respuesta para mi pregunta, o es para otro idioma o no responde totalmente (el código muerto no es un código inútil), así que aquí está mi pregunta :

¿Es (explícito o no) un código inútil ignorado por el compilador?

Por ejemplo, en este código:

 double[] TestRunTime = SomeFunctionThatReturnDoubles; // A bit of code skipped int i = 0; for (int j = 0; j < TestRunTime.Length; j++) { } double prevSpec_OilCons = 0;

¿Se eliminará el bucle for?

Yo uso .net4.5 y vs2013


El trasfondo es que mantengo una gran cantidad de código (que no escribí) y me preguntaba si el código inútil debería ser un objetivo o si podría dejar que el compilador se encargue de eso.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Bueno, sus variables i y prevSpec_OilCons , si no se usan en ninguna parte, se optimizarán, pero no su ciclo.

Entonces, si su código se parece a:

 static void Main(string[] args) { int[] TestRunTime = { 1, 2, 3 }; int i = 0; for (int j = 0; j < TestRunTime.Length; j++) { } double prevSpec_OilCons = 0; Console.WriteLine("Code end"); }

bajo ILSpy será:

 private static void Main(string[] args) { int[] TestRunTime = new int[] { 1, 2, 3 }; for (int i = 0; i < TestRunTime.Length; i++) { } Console.WriteLine("Code end"); }

Dado que el ciclo tiene un par de declaraciones, como comparación e incremento, podría usarse para implementar un período de espera/retraso algo corto. (aunque no es una buena práctica hacerlo) .

Considere el siguiente ciclo, que es un ciclo vacío, pero llevará mucho tiempo ejecutarlo.

 for (long j = 0; j < long.MaxValue; j++) { }

El bucle en su código no es un código inactivo, en lo que respecta al código inactivo, el siguiente es un código inactivo y se optimizará.

 if (false) { Console.Write("Shouldn't be here"); }

El bucle ni siquiera será eliminado por los nervios de .NET. Basado en esta respuesta

over 4 years ago · Santiago Trujillo Report

0

El bucle no se puede eliminar , el código no está muerto , por ejemplo:

 // Just some function, right? private static Double[] SomeFunctionThatReturnDoubles() { return null; } ... double[] TestRunTime = SomeFunctionThatReturnDoubles(); ... // You'll end up with exception since TestRunTime is null for (int j = 0; j < TestRunTime.Length; j++) { } ...

Por lo general, el compilador simplemente no puede predecir todos los resultados posibles de SomeFunctionThatReturnDoubles y es por eso que conserva el ciclo.

over 4 years ago · Santiago Trujillo Report

0

En su bucle hay dos operaciones implícitas en cada iteración. Un incremento:

 j++;

y comparación

 j<TestRunTime.Length;

Por lo tanto, el bucle no está vacío aunque parezca que lo está. Hay algo que se está ejecutando al final y, por supuesto, el compilador no lo ignora.

Esto sucede también en otros bucles.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!