Use local variables or access multiple times a struct value ( C++ )

webdeb

in JS it a good practice to create a variable, for reusing, instead of access the value in a deep object structure:

for (var i = 0, l = arr.length; i < l; ++i) {
  var someValueINeedOftenHere = arr[i].path.to.value;
  // do several things with this var..
}

So instead of finding the value in this deep object structure, we store it locally and we can reuse it over and over again. This should be a good practice, not only because it lets you write cleaner code, but also because of performance.

So, when I am writing C++ code, and I've to iterate over a vector, which holds a lot of structs / objects. Is then the same, or it doesn't matter?

Chris Beck

Generally speaking, in C/C++ it doesn't matter. In C and C++, the memory layout of every structure is known at compile-time. When you type arr[i].path.to.value, that's going to be essentially the same as *(&arr[0] + i * (something) + offset_1 + offset_2 + offset_3), and all that will get simplified at compile-time to something like *(&arr[0] + i * (something) + something). And those something's will be computed by the compiler and hard-coded into the binary, so effectively looking up arr[i].path.to is not faster than arr[i].path.to.value.

This is not mandated by the standard or anything as far as I know, but it's how most compilers will actually work.

If you want to be sure in some specific case, you can look at godbolt and see what assembly it cooks up: http://gcc.godbolt.org/

Note that I'm assuming that when you make the local variable, you are taking a reference to the value arr[i].path.to.value, which is most similar to what you do in javascript. If you actually copy the value to a new variable then that will create some copying overhead. I don't think that copying it would be advantageous w.r.t. cache misses unless the usage pattern is pretty complicated. Once you access arr[i].path.to.value once, all the stuff around it is going to be in the cache, and there's no reason that copying it onto the stack would make anything faster.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Declaring local const variables of anonymous struct in C

Declaring functions and variables multiple times in C++

local variables overwrite in function run multiple times in a loop

C# Func: access local variables in a block

How to access C struct value in assembly?

C access struct in struct

How to use StopWatch multiple times in C#?

C - How to use a string with scanf multiple times

Use of unassigned local variable: value type vs custom struct

C - Local variables have the same address and value

Declare variable in Access Form Sub to use multiple times

In C#, why assign member variables to local variables and then use local variables instead of using member variables directly

"use strict": Assign Value to Multiple Variables

Use property using the value of multiple variables

Access value of Struct data

How to set multiple local variables to the same value in a single declaration?

How can I use sum() value in SQL Server multiple times?

How to store a temporary value in a Postgresql query and then use it multiple times?

How to use Formik's handleChange multiple times to change the same value?

Use text box value multiple times to update text on web page

How to use CASE statement when to local variables for multiple conditions

How to access local variables while handling ctrl-C event?

use promise multiple times

use a CountDownLatch multiple times

Use 'as' multiple times

Some variables are loaded multiple times

Parallel.ForEach with Dictionary and multiple local variables in C#

Swift access to C Struct

Nested struct access in C