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


Continue Reading »

C &C++ &Code &Java &Perl &PHP &Python 17 Mar 2010 21:13:56

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 19:58:03

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 »

C++ 07 Feb 2010 15:41:15

C++ Convert Int to String Speed

(There is also an opposite string-to-int performance test.)
(Updated 2011-02-05: Added a hand-made function as baseline; re-run all tests, and with new clang++ from svn)

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

The tested methods are:

  • naive loop into a std::string
  • sprintf() into a char[] buffer, then std::string(buffer)
  • snprintf() into a char[] buffer, then std::string(buffer)
  • sprintf() into &std::string[0], and .resize() to fit
  • snprintf() into &std::string[0], and .resize() to fit
  • output to a std::stringstream, then std::string = stream.str()
  • as above, but reusing the stringstream object
  • std::strstream(&string[0]) then .resize(stream.pcount())
  • std::string = boost::lexical_cast<std::string>()
  • Boost.Spirit.Karma generate into a char[] buffer, then std::string(buffer)

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

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

Continue Reading »

C++ 02 Feb 2010 16:38:56

C++ Find Unique Elements Speed

A performance benchmark of which method is faster of finding all unique elements of a vector. The goal is having an ordered container with just the unique elements, where the tested methods are std::sort()+std::unique()+erase(), versus inserting into a separate std::set, versus inserting into std::unordered_set and then into std::set, and finally inserting into boost::unordered_set and then into std::set.

Source for the integer version is speed-find-unique-int.cpp with cycle.h.

The compilers are Microsoft Visual C++ 2010 Express as VC10 with _SECURE_SCL disabled, and GNU g++ 4.3.2.

Continue Reading »

« Previous PageNext Page »