c# - Why does it take longer to access to a previously created variable than a variable just declared? -


i've ran benchmark see whether access times less variable declared @ end of block of variable declarations or after.

benchmark code (selected variable declared @ end of block),

// benchmark 1     (long = 0; < 6000000000; i++) {     var a1 = 0;     var b1 = 0;     var c1 = 0;      // 53 variables later...      var x2 = 0;     var y2 = 0;     var z2 = 0;      z2 = 1;     // write      var _ = z2; // read } 

benchmark code (selected variable declared @ start of block),

// benchmark 2     (long = 0; < 6000000000; i++) {     var a1 = 0;     var b1 = 0;     var c1 = 0;      // 53 variables later...      var x2 = 0;     var y2 = 0;     var z2 = 0;      a1 = 1;     // write      var _ = a1; // read } 

to surprise results (averaged on 3 runs, excluding first build , without optimizations) follows,

benchmark 1: 9,419.7 milliseconds.

benchmark 2: 12,262 milliseconds.

as can see accessing "newer" variable in above benchmark 23.18% (2842.3 ms) faster, why?

normally, unused locals deleted optimizations in optimizing compiler in world. writing variables. easy case deletion of physical storage.

the relation between logical locals , physical storage highly complex. might deleted, enregistered or spilled.

so don't think var _ = a1; result in read a1 , write _. nothing.

the jit switches off few optimizations in functions many (i believe 64) local variables because algorithms have quadratic running time in number of locals. maybe that's why locals impact performance.

try fewer variables , not able distinguish variations of function 1 another.

or, try vc++, gcc or clang. should delete entire loop. i'd disappointed if didn't.

i don't think measuring relevant here. whatever result of benchmark - helps nothing real-world code. if interesting case i'd @ disassembly said think irrelevant. whatever find not interesting find.

if want learn code compiler typically generates should write simple functions , @ generated machine code. can instructional.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -