Microsoft Interview Question
Country: United States
Interview Type: In-Person
one complement to comments above.
1. Cache
consider a big array long ar[100][100].
a) for (int i = 0; i < 100; ++i)
for (int j = 0; j < 100; ++j)
b) for (int j = 0; j < 100; ++j)
for (int i = 0; i < 100; ++i)
a) is better and faster, most-of-the-time, than b). Because a)'s traversal is smooth and using cache, while b) makes big jumps and cachemiss occurs. This also tells us to use vectors as a default container. As Stroustrup mentioned in GoNative2012, it has been tested that vectors run faster that lists even in heavy use of insertion and deletion.
Any c++ function which are using inline function will have more memory when they are being called in loops because it the code will expand at compile time and hence it will increase the memory size .instead of that if we have normal function only once the object code will be generated therefore it will take less memory .
- sujeet kumar February 05, 2013Second - we can optimize c++ program by seeing when we are returning from function by value it created temporary variable for that they call constructor and destructor which also can be avoided ...