[LLVMbugs] [Bug 10193] New: std::stringbuf performance vs libstdc++

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Jun 25 09:16:43 PDT 2011


           Summary: std::stringbuf performance vs libstdc++
           Product: libc++
           Version: unspecified
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
        AssignedTo: hhinnant at apple.com
        ReportedBy: puurtuur at me.com
                CC: llvmbugs at cs.uiuc.edu

Created an attachment (id=6771)
 --> (http://llvm.org/bugs/attachment.cgi?id=6771)
Time profile of 10M iters of libstdc++ and libc++ with 6 char str.

I've been running some tests on libc++ with various classes and I saw that
libc++ was quite a bit slower (relatively at least) than libstdc++ with
std::stringbuf. Full source of sample app follows:

#include <string>
#include <iostream>
#include <sstream>
#include <time.h>

static const int NUM_ITERATIONS = 10000000;

void bench_buf() {
    std::stringbuf sb;

    for(int i = 0; i < NUM_ITERATIONS; ++i)
        sb.sputn("foobar", 6);

    // force string to be generated and sanity check length output
    std::string s = sb.str();
    std::cout << s.length() << std::endl;

int main(int argc, char *argv[]) {
    clock_t clk = clock();
    std::cout << ((clock() - clk) / static_cast<double>(CLOCKS_PER_SEC)) <<

    return 0;


If I compile this with libc++, the runtime is around 0.30s, with libstdc++ it's
around 0.20s (iMac Core i7 2.93GHz). Given how libc++ is mostly faster I though
it was worth mentioning. I attached two runs made in Profiler that show
libstdc++ (run 1) is more busy moving around memory where libc++ (run 2) spends
 a lot of time in xsputn itself.

Increasing the string length to 46 chars changes times to:
libstdc++: 0.83s
libc++: 2.04s
So the gap widens as more data is involved.

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list