Code optimization

If I have a big structure (having many member variables). This structure pointer is passed to many functions in my code. Some member variables of this structure are used very often, in almost all functions.

  • If I put these commonly used member variables at the beginning of the structure declaration, it will select the code for MCPS - a million cycles per second (the time it takes the code). If I become frequently accessible members on time, will they have access to an effective / shorter time than if they were randomly placed in the structure at the bottom of the structure’s declaration? If so, what is the logic?

  • If I have a member of a structure that is accessed in some function as follows:

    structurepointer1-> member_variable

Would this help in optimization in the MCPS aspect if I assign it to a local variable and then access the local variable as shown below?

local_variable = structurepointer1->member_variable;

If so, how does this help?

+3
source share
8 answers

1) The position of the field in the structure should not affect its access time, except that if your structure is very large and spans several pages, it may be a good idea to position the members that are often used in quick succession to get closer to increase link locality and try to reduce cache misses.

2) / . . , , . , , , , . , , .

: . , , , . , ( ).

+14

-, : , , .

, , , , . .

+7

, , - , http://dl.fefe.de/optimizer-isec.pdf. , , , , , , .

2, , , . , , , , .

, Mark Ransom, , .

+3

, . , , . . , , , , , .

, .

+2
  • , . - , .

  • local , . ( ), .

0

, , , . , , , , .

, , , c . , , , " ".

  • - . , , . , , , .

  • . . , , , . , . , , , .

, , , . , -, , ;) , .

0
  • . valgrind .
  • , . , . , , .

, , , , . .

, C / .

0

, . , , . , , , . , , ( ), , , , , :

calc = structurepointer1->member_variable * x + c;    
/* Do something in function which doesn't involve member_variable; */
function(structurepointer1);    

calc2 = structurepointer1->member_variable * y;

member_variable, , . , , 1

int temp = structurepointer1->member_variable;
calc = temp * x + something;
function(structurepointer1);
calc2 = temp * y;

There is another reason why you can use a local variable for your member variables, it can make the code more readable.

0
source

Source: https://habr.com/ru/post/1715209/


All Articles