Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Preferred way to access VectorMap
Preferred way to access VectorMap [message #35039] Sun, 01 January 2012 15:36 Go to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
Hello,

I have a
VectorMap<int, myclass> V;

I can retrieve the values of V with:

1) V[i].myclass_method...

2) myclass& a = V.Get(i);
a.myclass_method...

Which is the preferred way? Which is the faster?
Thanks,
Luigi
Re: Preferred way to access VectorMap [message #35040 is a reply to message #35039] Sun, 01 January 2012 17:57 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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
Re: Preferred way to access VectorMap [message #35041 is a reply to message #35040] Sun, 01 January 2012 18:47 Go to previous message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
dolik.rce wrote on 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]; }
}




Hi Honza,

Thank you for the info.
You are right about the warning and the differences. However in my case I forced the key 'i' to be in the same place of [i]. But in principle this is not the case.
So I am going to use the reference Smile

Best Wishes,
Luigi
Previous Topic: Small fix for Find in Algo.h
Next Topic: Date& operator++
Goto Forum:
  


Current Time: Fri Mar 29 11:00:50 CET 2024

Total time taken to generate the page: 0.01420 seconds