开发者

index , pointer , iterator , which is better ? when it comes to vectors

开发者 https://www.devze.com 2022-12-21 18:30 出处:网络
hi i\'m doing a program that needs high performance of handling vector elements vector<Class_A> object ;

hi i'm doing a program that needs high performance of handling vector elements

vector<Class_A> object ;

1- which开发者_如何学C one is fastest to access the elements 2- which is more code simpler and not complex to deal with

index ? iterator ? pointer ?


An iterator or pointer will have the same performance on most implementations -- usually a vector iterator is a pointer. An index needs to calculate a pointer each time, but the optimizer can sometimes take care of that. Generally though, as another commenter said, there's no sense in optimizing this for performance.

All of that said, I would probably go with an iterator since it's easier to change to another type of container if need be.


Assuming you have inlining enabled, and arent doing index range checking, they will probably all be about the same. Besides micro optimizing this probably isn't going to gain you anything, you need to optimize globally. profile your process and target the slowest parts first

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号