c++ - Performance: better to inizialize variable or just fill it? -
let's have function called 10 times second, like:
void rxdata(system::byte *data){}
in function want fill struct *data, struct like:
struct datastr{ float first; float second; }
in term of performances , memory use better declare extern variable datastr str1
, doing:
void rxdata(system::byte *data){str1=*reinterpret_cast<datastr*>(data);}
or reinizializing every time struct, like:
void rxdata(system::byte *data){datastr str1; str1=*reinterpret_cast<datastr*>(data);}
can explain differencies in term of memory management? tend think second option more "clean" cannot realy argue why.
in terms of memory management, data located in different places in memory. global variable placed in static segment, while local variable lives on stack. created every time? yes. affect performance? depends. if datastr
looks show here, no.
it might have better performance even, because present in cache @ moment function executes. global variable might me there too, will, in different memory location, mean prefetcher have more work do.
option 2 better in terms of style. reader have @ hand while seeing function , won't have jump around decipher data flow.
Comments
Post a Comment