Menu

fastformat 100 times slower of CString::Forma

Help
Anonymous
2010-05-11
2013-05-30
  • Anonymous

    Anonymous - 2010-05-11

    Here is problem.
    In Windows, VC 8 (VS 2005)
    I built library as Release Multithreaded,

    Either library was improperly built or fastformat is too s:low. 10M repeats of CString::Format takes ~6.5 sec in 32bit non-unicode build, while fastformat is some 10.5 minutes:

    CString sink;
    #define ATTEPMTS 10000000   
    __int64 am = 3543000;
    clock_t start1a = clock();
    for( int t=0; t<ATTEPMTS; ++t)
        sink.Format ( "%I64d", am );
    clock_t end1a = clock();
    outut.Format( "CString::Format elapsed %.2f\n", (double(end1a) - double(start1a)) / CLOCKS_PER_SEC );
    AfxMessageBox( outut );
    start1a = clock();
    for( int t=0; t<ATTEPMTS; ++t)
        fastformat::fmt( sink, "{0}", am );
    end1a = clock();
    outut.Format( "fastformat elapsed %.2f\n", (double(end1a) - double(start1a)) / CLOCKS_PER_SEC );
    AfxMessageBox( outut );
    
     
  • Matt Wilson

    Matt Wilson - 2010-05-11

    That's because fmt() and write() append to the existing contents of the sink. So, in the statement

    fastformat::fmt( sink, "{0}", am );
    

    you're adding to a string each time, making a very large string: "354300035430003543000354300035430003543000354300035430003543000354300035430003543000354300035430003543000354300035430003543000354300035430003543000354300035430003543000 … etc."

    Try placing the sink within the scope of the statement and you'll find the timings will be around the same. Here's a timing program I just knocked up:

    #ifdef _DEBUG
        const unsigned  ITERATIONS  =   1000;
    #else /* ? _DEBUG */
        const unsigned  ITERATIONS  =   1000000;
    #endif /* _DEBUG */
        winstl::performance_counter counter;
        stlsoft::uint64_t   var;
        { for(int WARMUPS = 2; 0 != WARMUPS; --WARMUPS)
        {
            counter.start();
            { for(unsigned i = 0; i != ITERATIONS; ++i)
            {
                CString sink;
                sink.Format("%I64d", var);
            }}
            counter.stop();
            if(1 == WARMUPS)
            {
                ff::fmtln(std::cout, "CString::Format(): {0}ms", counter.get_milliseconds());
            }
            counter.start();
            { for(unsigned i = 0; i != ITERATIONS; ++i)
            {
                CString sink;
                ff::fmt(sink, "{0}", var);
            }}
            counter.stop();
            if(1 == WARMUPS)
            {
                ff::fmtln(std::cout, "ff::fmt(): {0}ms", counter.get_milliseconds());
            }
        }}
    

    The output is

    CString::Format(): 642ms
    ff::fmt(): 598ms
    

    So, in fact FastFormat is a little faster than CString::Format()

    HTH

    Matt

     
  • Matt Wilson

    Matt Wilson - 2010-05-11

    Trying again, to get the formatting correct:

    {
    #ifdef _DEBUG
        const unsigned  ITERATIONS  =   1000;
    #else /* ? _DEBUG */
        const unsigned  ITERATIONS  =   1000000;
    #endif /* _DEBUG */
        winstl::performance_counter counter;
        stlsoft::uint64_t   var;
        { for(int WARMUPS = 2; 0 != WARMUPS; --WARMUPS)
        {
            counter.start();
            { for(unsigned i = 0; i != ITERATIONS; ++i)
            {
                CString sink;
                sink.Format("%I64d", var);
            }}
            counter.stop();
            if(1 == WARMUPS)
            {
                ff::fmtln(std::cout, "CString::Format(): {0}ms", counter.get_milliseconds());
            }
            counter.start();
            { for(unsigned i = 0; i != ITERATIONS; ++i)
            {
                CString sink;
                ff::fmt(sink, "{0}", var);
            }}
            counter.stop();
            if(1 == WARMUPS)
            {
                ff::fmtln(std::cout, "ff::fmt(): {0}ms", counter.get_milliseconds());
            }
        }}
        /* . */
        return EXIT_SUCCESS;
    }
    
     
  • Anonymous

    Anonymous - 2010-05-12

    Thank you,
    I figured out it and was going to add comment about my mistake, but you already pointed that out.

     

Log in to post a comment.