C++ 02 Apr 2010 19:11:11

C++ Set Performance

(newer version from 2012-02-20, older std::map benchmarks for GNU g++ 4.3.2 and MSVC++ 2010 beta 2)

A performance comparison of the speed of operations on the various set implementations used in C++. There are 16383 unique elements across which 1000000 insert, lookup, iterate, and erase operations are performed.

And this time I’ve left the raw tick numbers and enabled table sorting so you can compare for yourself. Just be aware that the Linux and Windows numbers cannot be compared against each other.

Sources

Linux: GNU g++ 4.4.1

  • Updated 2010-04-14: Rerun with g++ 4.4.1. Almost identical numbers, but boost::unordered_set::erase() is much faster in this version.
  • Compiler: GNU g++ 4.4.1 -std=c++0x -O3
  • Arch: Linux 2.6.27.9-159.fc10.x86_64, 2.66GHz Xeon, 8 GiB RAM
  • Memory usage counted via Valgrind

Key: uint32_t

g++ 4.4.1 <uint32_t>InsertLookupIterateEraseAllocsBytes Allocated
std::set3630544163496057927692643598160016393655520
std::unordered_set33186608301502241841603040995216405746448
boost::unordered_set3349779230615040145888709217616407657536
sorted_vector3600440962625079362473611200563224131228
sorted_deque5309824324202687524256017163406414368608
google::sparse_hash_set1724558081624345765820801321102576425863709528
google::dense_hash_set269976321070902415849611716976020262176

Key: std::string

g++ 4.4.1 <string>InsertLookupIterateEraseAllocsBytes Allocated
std::set16702456161636179776103091218140057616393655520
std::unordered_set23139233622467171297182412023240016405746448
boost::unordered_set2074383681984604486511522662444816407657536
sorted_vector35002422081603444144132976184619265624262296
sorted_deque274772371218672925441318401092511920272136688

Windows: MSVC++ 2010 RC1

  • Compiler: MSVC++ 2010 RC1 _SECURE_SCL=0
  • Arch: Windows 7 64 bit, 3.0GHz Core2Duo, 4 GiB RAM
  • Extra Container: sti::sset (wouldn’t compile with g++, so omitted there for now)

Key: uint32_t

VC++ 2010 RC1 <uint32_t>InsertLookupIterateErase
std::set71451470734280581766347175306186
std::unordered_set41133424547025360113256118094400
boost::unordered_set502553881726571712176110382427
sorted_vector68500439128363442421754117755028
sorted_deque1152645203470817207255663650932533
sti::sset2948979762679823806711354127557

Key: std::string

VC++ 2010 RC1 <string>InsertLookupIterateErase
std::set1370212416876426329699075166895604
std::unordered_set677187891141716270224622137859399
boost::unordered_set17324965613376923236610220043045
sorted_vector2529399042899673525450902404031290
sorted_deque256053500111679865202705221887514182
sti::sset72492688663908276497317112394043

4 Responses to “C++ Set Performance”

  1. on 27 Oct 2010 at 22:34:49 1.Mahbub Murshed said …

    The numbers are: the smaller the better. Right?

  2. on 27 Oct 2010 at 22:37:41 2.Tino Didriksen said …

    Yep. The numbers denote time/ticks and memory used performing the action, so smaller is better.

  3. on 25 Jan 2011 at 13:55:58 3.Ethan Schreiber said …

    Why do you suppose google::dense_hash_set used less memory than google::sparse_hash_set ? According to Google, this is opposite of what one should expect.

  4. on 25 Jan 2011 at 16:05:46 4.Tino Didriksen said …

    Very likely because I am using key type uint32_t.

    sparse_hash_set allocates each element separately, where dense_hash_set allocates contiguous chunks to be doled out later. For type uint32_t that is much more efficient, but I would expect that for a larger type it would be drastically inverted.

Subscribe to the comments through RSS Feed

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image