Comparing the performance of a single int_ generator

| Home | Libraries | People | FAQ | More

##### Comparing the performance of a single int_ generator

These performance measurements are centered around default formatting of a single `int` integer number using different libraries and methods. The overall execution times for those examples are compared below. We compare using `sprintf`, C++ iostreams, Boost.Format, and Spirit.Karma.

For the full source code of the performance test please see here: int_generator.cpp. All the measurements have been done by executing `1e7` iterations for each formatting type (NUMITERATIONS is set to `1e7` in the code shown below).

Code used to measure the performance for `ltoa`:

    char buffer[65]; // we don't expect more than 64 bytes to be generated here
    for (int i = 0; i < MAX_ITERATION; ++i)
    {
        ltoa(v[i], buffer, 10);
    }

Code used to measure the performance for standard C++ iostreams:

    std::stringstream str;
    for (int i = 0; i < MAX_ITERATION; ++i)
    {
        str.str("");
        str << v[i];
    }

Code used to measure the performance for Boost.Format:

    std::string str;
    boost::format int_format("%d");
    for (int i = 0; i < MAX_ITERATION; ++i)
    {
        str = boost::str(int_format % v[i]);
    }

Code used to measure the performance for Spirit.Karma using a plain character buffer:

    char buffer[65]; // we don't expect more than 64 bytes to be generated here
    for (int i = 0; i < MAX_ITERATION; ++i)
    {
        char *ptr = buffer;
        karma::generate(ptr, int_, v[i]);
        *ptr = '\0';
    }

The following table shows the overall performance results collected while using different compilers. All times are in seconds measured for `1e7` iterations (platform: Windows7, Intel Core Duo(tm) Processor, 2.8GHz, 4GByte RAM). For a more readable comparison of the results see this figure.

Table 5. Performance comparison for a single int (all times in [s], `1e7` iterations)

Library | gcc 4.4.0 (32 bit) | VC++ 10 (32 bit) | Intel 11.1 (32 bit) | gcc 4.4.0 (64 bit) | VC++ 10 (64 bit) | Intel 11.1 (64 bit)
--- | --- | --- | --- | --- | --- | ---
ltoa | 1.542 | 0.895 | 0.884 | 1.163 | 1.099 | 0.906
iostreams | 6.548 | 13.727 | 11.898 | 3.464 | 8.316 | 8.115
Boost.Format | 16.998 | 21.813 | 20.477 | 17.464 | 14.662 | 13.646
Spirit.Karma int_ | 1.421 | 0.744 | 0.697 | 1.072 | 0.953 | 0.606

Figure 3. Performance comparison for a single int