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
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 previous 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
 
Read Message
Read Message
Read Message
Previous Topic: Small fix for Find in Algo.h
Next Topic: Date& operator++
Goto Forum:
  


Current Time: Fri May 17 01:19:46 CEST 2024

Total time taken to generate the page: 0.02751 seconds