c# - Can I speed up code by moving typeof out of loops? -


i have code executes typeof many times inside loops.

i'm wondering if should call typeof outside loops , use variable?

my code this.

for(int i=0; < 1000; i++) {     type t = foo(i);     if(t == typeof(string))     {         //....     } } 

is following code faster, slower or no difference?

type s = typeof(string) for(int i=0; < 1000; i++) {     type t = foo(i);     if(t == s)     {         //....     } } 

instead of running few c# tests see runs faster. i'm looking answer can explain happens when typeof used.

does produce compile time constant?

no (as in extremely negligible), typeof() done @ compile-time--the work type comparison. should asking why you're checking types when using polymorphism.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -