Category ArchiveCode
C++ 20 Feb 2012 04:12 pm
C++ Set Performance 2
(Old version from 2010-04-02)
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.
The raw tick numbers are shown and table sorting is enabled so you can compare for yourself. Just be aware that the Mac OS X, Linux, and Windows numbers cannot be compared against each other.
The full source is available in svn as a CMake project for easy cross-platform testing.
Sources
- Containers:
- std::set, std::unordered_set, boost::unordered_set – the usual general purpose suspects.
- CG3::sorted_vector, std::vector based replacement for std::set. Suitable for small amounts of cheap objects, such as integers.
- CG3::sorted_deque, rubbish version of sorted_vector.
- CG3::interval_vector, specialized for storing integers that form intervals.
- sti::sset, skip-list based std::set replacement. Wouldn’t compile with g++ or clang++.
- Ticks counted via cycle.h (local mirror)
- Source: CMake Project: benchmarks, primary source set.cpp
C++ 31 Aug 2011 10:27 pm
C++ Include Speed
A performance benchmark of which include guard method is faster. Test times the compilation of a main.cpp that includes 10000 files 3 times each.
The tested methods are:
#pragma once
followed by#ifndef
#ifndef
followed by#pragma once
- Only
#pragma once
- Only
#ifndef
- External
#ifndef
Sources for the test is at inc.tar.gz, but it’s just 5×10000 files.
The compilers are Microsoft Visual C++ 2010, GNU g++ 4.6.1, and LLVM clang++ 2.9.
C++ 28 May 2011 07:04 pm
C++ Convert String to Double Speed
(There is also a string-to-int performance test.)
A performance benchmark of which method is faster of converting an std::string to a double. The goal is ending up with a double of the value represented in an std::string.
The tested methods are:
- a hand-written naive loop
- atof()
- strtod()
- sscanf()
- boost::lexical_cast<double>()
- boost::spirit::qi::parse()
- std::stringstream
- std::stringstream, reusing the object
Source for the test is at speed-string-to-double.cpp with cycle.h.
The compilers are Microsoft Visual C++ 2010 with _SECURE_SCL disabled, GNU g++ 4.6.0, and LLVM clang++ from Arch.
C++ 10 Feb 2011 06:24 pm
C++ String Compare Performance
A performance comparison of the speed of various ways to compare strings in C++. In this test, all comparisons are of not-equal strings.
Idea from #C++ on QuakeNet, where we always advocate using std::string over various char* functions. I wondered what, if any, the penalty for doing so was.
Sources
- Ticks counted via cycle.h (local mirror)
- Source: speed-string-compare.cpp
Things Tested
- a hand-written naive comparator loop; used as baseline
- string == string
- string == const char*
- strcmp(const char*, const char*) == 0
- strcmp(const char*, string.c_str()) == 0
- strcmp(string.c_str(), string.c_str()) == 0
- string.compare(string)
- string.compare(const char*)
- …and then the whole thing with different offsets.
C++ 28 Jan 2011 04:21 pm
C++ Read Whole File Performance
A performance comparison of the speed of various ways to read an entire file into an std::string in C++.
Idea from BD-Calvin in #C++ on QuakeNet.
Sources
- Ticks counted via cycle.h (local mirror)
- Source: speed-read-whole-file.cpp
- File used was generated with dd if=/dev/urandom of=random100 bs=1M count=100
Things Tested
- writing to a stringstream, then pulling data out as string
- constructing a string via streambuf_iterator
- getting file size via seeking, then preallocating a string to read into
- getting file size via stat(), then ditto…