Category ArchiveCode



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 »

C++ 16 Feb 2010 07:58 pm

C++ Convert String to Int Speed

(There is also an opposite int-to-string performance test.)

(Updated 2010-04-15: Re-run with g++ 4.4.1 and VC++ 2010 Express; updated tables to show both ticks and relative factor, instead of just percentage of baseline.)

A performance benchmark of which method is faster of converting an std::string to an integer. The goal is ending up with an integer with the value represented in an std::string.

The tested methods are:

  • atoi()
  • atol()
  • strtol()
  • std::stringstream
  • std::stringstream, reusing the object
  • boost::lexical_cast<int>()
  • a hand-written naive loop

Source for the test is at speed-string-to-int.cpp with cycle.h.

The compilers are Microsoft Visual C++ 2010 Express as VC10 with _SECURE_SCL disabled, GNU g++ 4.4.1, and LLVM clang++ from svn.

Continue Reading »

« Previous PageNext Page »