[Help]Accessing structs efficiency

monday

Expert
Joined
Jun 23, 2014
Messages
1,125
Reaction score
149
Hi, is there usually a difference between the following 2 examples in terms of speed?


Code:
var[555][555][555][555] += 1
var[555][555][555][555] += 1
var[555][555][555][555] += 1
var[555][555][555][555] += 1
var[555][555][555][555] += 1
var[555][555][555][555] += 1



Code:
copy = var[555][555][555][555]
copy += 1
copy += 1
copy += 1
copy += 1
copy += 1
copy += 1
var[555][555][555][555] = copy
 

0x_

Wtf I'm not new....
Administrator
Joined
Feb 18, 2013
Messages
1,118
Reaction score
166
If you copy the array instead of taking a reference to the value or accessing it directly yes because you then allocate memory which costs time.

Also, your example is barely logical as it won't work in a real world scenario (.. or do you use std::array?).

We / I can only give right information if we get your idea also.
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,125
Reaction score
149
thanks, the idea is nothing specific, I had a little fear that writing long instructions which include long "paths" to the data like: var[555][555][555][555][555][555][555][555][555] would decrease the speed
 

0x_

Wtf I'm not new....
Administrator
Joined
Feb 18, 2013
Messages
1,118
Reaction score
166
The allocation will take longer yes, but access will happen in constant time as there's no traversal needed.
How's that possible?: Memory = RAM = Random access memory (as a little hint).
 
Top