c++ - Is it slower to access a field from several structures vs accessing elements of an array / vector? -


i noticed other students use lot of arrays elements of should fields of object.

is because faster access element array vs field structure?

for example, have clock cycles stored object as...

1) array of 5 ints  2) field in 5 structures 

and need access these elements find min / max.

i idea of keeping properties of object opposed having multiple arrays have fit naturally field. i'm not sure if i'm writing considered bad programming habits handling field in structure.

maybe not 5 elements - thousands of elements, there might difference.

the universal principle of caching locality of reference - it's better store stuff together. if know program access 1 (or few) field of structures, it's better relocate field dedicated array. if program accesses (or most) fields of structures, it's better hold structures in array.

however, related performance, real behavior surprising, , have measure 1 way of implementation, other, , choose best.


if application doesn't require performance optimization, use code readable. may allocate "clock cycles" field in struct or array - way more readable depends on application. , on aspect of application developing.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -