Home » U++ Library support » U++ Core » Preferred way to access VectorMap
|
Re: Preferred way to access VectorMap [message #35040 is a reply to message #35039] |
Sun, 01 January 2012 17:57 |
|
Hi Luigi,
First of all, beware that each of those two methods does something else:
VectorMap<K = int,T = myclass> {
T& Get(const K& k) { return value[Find(k)]; }
T& operator[](int i) { return value[i]; }
}
Now, lets say we are talking about Vector, where Get and operator [] are identical. In this case, operator[] calls get: T& operator[](int i) { return Get(i); }
So it is one function call shorter to use get, but I would say that modern compilers will optimize this out, so there is not much of a difference between both method. The only time you might see some speedup is when you need to do several operations over the same element, than it is definitely a good idea to save a reference and work with that (method 2), instead of calling operator[] or Get multiple times, because Get has little overhead caused by checking the range of argument.
Also note that since Get and operator[] are equivalent, you can use a third variant:
3) myclass& a = V[i];
a.myclass_method...
Best regards,
Honza
|
|
|
|
Goto Forum:
Current Time: Fri Sep 20 03:45:05 CEST 2024
Total time taken to generate the page: 0.05573 seconds
|