Adding stringstream / cout degrades performance even when code is never called

I have a program in which a simple function is called a large number of times. I added some simple registration codes and found that this significantly affects performance, even if the registration code is not actually called. A complete (but simplified) test case is shown below:

#include <chrono>
#include <iostream>
#include <random>
#include <sstream>

using namespace std::chrono;

std::mt19937 rng;

uint32_t getValue()
{
    // Just some pointless work, helps stop this function from getting inlined.
    for (int x = 0; x < 100; x++)
    {
        rng();
    }

    // Get a value, which happens never to be zero
    uint32_t value = rng();

    // This (by chance) is never true
    if (value == 0)
    {
        value++; // This if statment won't get optimized away when printing below is commented out.

        std::stringstream ss;
        ss << "This never gets printed, but commenting out these three lines improves performance." << std::endl;
        std::cout << ss.str();
    }

    return value;
}

int main(int argc, char* argv[])
{
    // Just fror timing
    high_resolution_clock::time_point start = high_resolution_clock::now();

    uint32_t sum = 0;   
    for (uint32_t i = 0; i < 10000000; i++)
    {
        sum += getValue();  
    }

    milliseconds elapsed = duration_cast<milliseconds>(high_resolution_clock::now() - start);

    // Use (print) the sum to make sure it doesn't get optimized away.
    std::cout << "Sum  = " << sum << ", Elapsed = " << elapsed.count() << "ms" << std::endl;
    return 0;
}

Note that the code contains stringstream and cout, but they are never called. However, the presence of these three lines of code increases the runtime from 2.9 to 3.3 seconds. This is in release mode on VS2013. Curiously, if I build GCC using the -O3 flag, the extra three lines of code actually reduce the execution time by half a second or so.

, , , . : -, , ? sprintf()/printf(), , . , , ?

.. / , , . , THROW_EXCEPT (...), , , . , . - ?

: VS2013 , : https://drive.google.com/file/d/0B7b4UnjhhIiEamFyS0hjSnVzbGM/view?usp=sharing

+4
1

, , , , :

    if (value == 0)
00E21371  mov         ecx,1  
00E21376  cmove       eax,ecx  
    {
        value++;

, , , , :

if (value == 0)
00AE1371  jne         getValue+99h (0AE1379h)  
    {
        value /= value;
00AE1373  xor         edx,edx  
00AE1375  xor         ecx,ecx  
00AE1377  div         eax,ecx  

, , , . , :

if (value == 0)
008F13A0  jne         getValue+20Bh (08F14EBh)  
    {
        value++;     
        std::stringstream ss;
008F13A6  lea         ecx,[ebp-58h]  
008F13A9  mov         dword ptr [ss],8F32B4h  
008F13B3  mov         dword ptr [ebp-0B0h],8F32F4h  
008F13BD  call        dword ptr ds:[8F30A4h]  
008F13C3  push        0  
008F13C5  lea         eax,[ebp-0A8h]  
008F13CB  mov         dword ptr [ebp-4],0  
008F13D2  push        eax  
008F13D3  lea         ecx,[ss]  
008F13D9  mov         dword ptr [ebp-10h],1  
008F13E0  call        dword ptr ds:[8F30A0h]  
008F13E6  mov         dword ptr [ebp-4],1  
008F13ED  mov         eax,dword ptr [ss]  
008F13F3  mov         eax,dword ptr [eax+4]  
008F13F6  mov         dword ptr ss[eax],8F32B0h  
008F1401  mov         eax,dword ptr [ss]  
008F1407  mov         ecx,dword ptr [eax+4]  
008F140A  lea         eax,[ecx-68h]  
008F140D  mov         dword ptr [ebp+ecx-0C4h],eax  
008F1414  lea         ecx,[ebp-0A8h]  
008F141A  call        dword ptr ds:[8F30B0h]  
008F1420  mov         dword ptr [ebp-4],0FFFFFFFFh 

, - . , - ?

    if (value == 0)
011F1371  jne         getValue+0A6h (011F1386h)  
    {
        value++;
        printf("This never gets printed, but commenting out these three lines improves performance.");
011F1373  push        11F31D0h  
011F1378  call        dword ptr ds:[11F30ECh]  
011F137E  add         esp,4 

, , , .

, , , , .

, ​​:

void log()
{
    std::stringstream ss;
    ss << "This never gets printed, but commenting out these three lines improves performance." << std::endl;
    std::cout << ss.str();
}

if (value == 0)
{
    value++;
    log();

, , call log (011C12E0h).

+1

All Articles