Monthly ArchiveFebruary 2010



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 »

C++ 01 Feb 2010 15:32:18

Snippet: Dynamic Box of Strings

//tinodidriksen.com/uploads/code/cpp/dynamic-string-box.cpp

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> strs;
    strs.push_back("Hello World!");
    strs.push_back("This is a text line.");
    strs.push_back("Shorter line.");

    size_t maxl = 0;
    for (size_t i = 0 ; i<strs.size() ; ++i) {
        maxl = std::max(maxl, strs[i].length());
    }

    std::cout << std::string(maxl+4, '*') << std::endl;
    for (size_t i = 0 ; i<strs.size() ; ++i) {
        std::cout << "* "
            << strs[i]
            << std::string(maxl-strs[i].length()+1, ' ')
            << "*" << std::endl;
    }
    std::cout << std::string(maxl+4, '*') << std::endl;
}