Category ArchiveC++



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

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.


Continue Reading »

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

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…


Continue Reading »

C++ 14 Apr 2010 08:41 pm

C++ dynamic_cast Performance

(Updated 2010-10-27: Re-run the test with latest clang++ from subversion)

A performance comparison of the speed of dynamic_cast operations in C++.

Idea from http://www.nerdblog.com/2006/12/how-slow-is-dynamiccast.html, who did not provide any source so I wrote my own more extensive tests.

Sources

Things Tested

  • reinterpret_cast on a known type
  • virtual function call + reinterpret_cast
  • member variable access + reinterpret_cast
  • successful dynamic_cast to its own type
  • successful dynamic_cast from the derived levels to lower levels
  • failed dynamic_cast from the derived levels to an unrelated type


Continue Reading »

C++ 02 Apr 2010 07:11 pm

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


Continue Reading »

C &C++ &Code &Java &Perl &PHP &Python 17 Mar 2010 09:13 pm

Language Comparison: Find Longest Line

The task: Write a portable tool that takes a file name as first argument, and from that file outputs the number of lines, the longest line, the length of the longest line, and the line number of the longest line, in a binary-safe fashion (meaning, \n is the only reason a line should be delimited; embedded \0s may occur).

My sinister side goal is showing how complex the C code has to be in order to get performance near C++. It is possible to write a simpler version in C that is 20% faster for /usr/share/dict/words, but it is then 4x slower for the random files. It would also be possible to write a faster non-portable version using memory mapping, but can’t really call that simple. C++ has both readability and speed. If I missed some obvious portable optimization in the C code, let me know…

The contestants are:
[table "12" seems to be empty /]


Continue Reading »

« Previous PageNext Page »