.net - Should I use nested blocks to improve performance -


i have been learning stack , heap , particularly stack lifo approach.

does work nested blocks within methods , can used improve performance e.g.

public void test() {     int = 5;     //      int j = 5;     // j } 

in example, @ end of method , j freed stack.

would following more efficient? (i know simple procedure.....) or effort of freeing more saving on size of stack?

public void test() {     {         int = 5;         //     }     {         int j = 5;         // j     } } 

this solely example purposes, know refactor etc. interested in happens memory in method....

the stack allocated whole of method call anyway.

there are subtle cases adding blocks help, in terms of captured variables - if have 2 lambda expressions, 1 of captures i , 1 of captures j, in first case you'd end synthesized class captures both variables, whereas in second case you'd end 2 classes... @ least in current implementation.

without variable capture, it's @ least possible jit notice use 1 variable @ time, , optimize reuse same stack space both.

it's rare micro-optimizations have significant performance impact, can have significant readability impact... start making code less readable when:

  • you have robust performance tests in place
  • you have concrete performance goal in mind (so know when you're done)
  • you have narrowed down bottleneck is
  • you have proved reduction in readability provides significant boost in performance

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -