Click here to Skip to main content
15,886,725 members
Articles / Web Development / HTML

Head-to-head benchmark: C++ vs .NET

Rate me:
Please Sign up or sign in to vote.
4.64/5 (338 votes)
4 Jul 2011LGPL356 min read 1.1M   2.3K   202  
How fast is C++ compared to C#? Let's compare code ported directly between the two languages.
#ifndef _EASYMAP_H
#define _EASYMAP_H
#include <map>

// I find the STL map interface difficult to remember and cumbersome. This wrapper
// provides an easier-to-use interface for common operations, comparable to the
// .NET IDictionary interface, which I can remember even without IntelliSense.
template<typename TKey, typename TValue>
struct EasyMap : public std::map<TKey,TValue>
{
	// Gets the value associated with a key, returning true if found, false if not. 
	// If the key was not found, then 'value' is left unchanged.
	bool TryGet(const TKey& key, TValue& value) const
	{
		iterator it = find(key);
		if (it == end())
			return false;
		value = it->second;
		return true;
	}
	
	// Gets the value associated with a key, or the specified default value if the 
	// key was not found.
	TValue Get(const TKey& key, TValue defaultValue = TValue()) const
	{
		TryGet(key, defaultValue);
		return defaultValue;
	}
	
	bool Contains(const TKey& key) const
	{
		return find(key) != end();
	}
	
	// Removes an entry from the map if it exists, returning true if the key was
	// found (and removed) or false if the key was not present in the map.
	bool Remove(const TKey& key)
	{
		return erase(key) > 0;
	}
	
	// Adds a key-value pair, or returns false if the key already existed.
	bool TryAdd(const TKey& key, const TValue& value)
	{
		return insert(value_type(key, value)).second; // returns true if pair was added
	}
	
	// Adds a key-value pair or throws an exception if the key already existed.
	void Add(const TKey& key, const TValue& value)
	{
		if (!TryAdd(key, value))
			throw runtime_error("The key is already in the dictionary.")
	}
	
	// Gets a reference to the existing value if the key exists. If the key does
	// not exist, adds the specified key-value pair and returns a reference to the
	// new value. Ex: you could increment a counter with "++map.GetOrAdd(key,0)".
	TValue& GetOrAdd(const TKey& key, const TValue initialValue = TValue())
	{
		iterator lb = lower_bound(key);
		if (lb != end() && !(key_comp()(key, lb->first)))
			return lb->second; // key already exists
		else
			return insert(lb, value_type(key, initialValue))->second;
	}
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions