Archive for March, 2011

Posted by kent at 16 March 2011

Category: c++

Tags: , ,

Boost::format’s performance sucks.

I used boost::format in an inner loop in my project, and the optimized runtime was 20 seconds for 1 small case.

I optimized away boost::format by replacing it with stream manipulators and i got the runtime down to 2 seconds for the same case.

I’ll publish an article later on the details on how to do this.

 

VN:F [1.9.13_1145]
Rating: 3.0/5 (2 votes cast)
Share

Posted by kent at 14 March 2011

Category: c++

Tags: , , ,

I always forget how to use boost::format when I need it. So, here are a few samples from when I did remember.

Only need is to include the proper boost header. No compile of any of the boost libraries is necessary.

The source is also available in subversion (webclient) (subversion checkout).


// build with make or:
//
// g++ -I../../boost_1_46_0 fmt.cpp
//

#include <iostream>
#include <sstream>
#include <boost/format.hpp>

using namespace std;
using namespace boost;

int main()
{
    stringstream ss;

    int year    = 2011;
    int month   = 3;
    int day     = 11;
    int hour    = 12;
    int minute  = 1;
    double seconds= 23.35;
    double epoch  = 1231234567890.35;

    // Fill with whitespace, right align
    ss << format("#1.0 %4i %4i %4i\n") % year % month % day;
    // Output: #1.0 2011    3   11

    // Fill with whitespace, left align
    ss << format("#1.1 %-4i %-4i %-4i\n") % year % month % day;
    // Output: #1.1 2011 3    11

    // Fill with zero (pad)
    ss << format("#2.0 %04i %04i %04i\n") % year % month % day;
    // Output: #2.0 2011 0003 0011

    // Floating point precision
    ss << format("#3.0 %11.2f\n") % seconds;
    // Output: #3.0       23.35

    // Floating point precision with zero-fill (zero pad)
    ss << format("#4.0 %011.2f\n") % seconds;
    // Output: #4.0 00000023.35

    // Left aligned string literal with 30+20 columns
    ss << format("#5.0 %-30s%-20s\n") % "LEFT ALIGNED 1" % "LEFT ALIGNED 2";
    // Output: #5.0 LEFT ALIGNED 1                LEFT ALIGNED 2

    // Right aligned string literal
    ss << format("#6.0 %30s%20s\n") % "RIGHT ALIGNED 1" % "RIGHT ALIGNED 2";
    // Output: #6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2

    cout << ss.str();

    return 0;
}

The output should be:

#1.0 2011    3   11
#1.1 2011 3    11
#2.0 2011 0003 0011
#3.0       23.35
#4.0 00000023.35
#5.0 LEFT ALIGNED 1                LEFT ALIGNED 2
#6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2
VN:F [1.9.13_1145]
Rating: 4.0/5 (1 vote cast)
Share

Posted by kent at 9 March 2011

Category: Dev

Tags: , ,

From TDWTF.

Inept management hires inept arrogant conceited glory hogging twit

Inept management promotes twit at twit’s urging

Twit hires more twits for personal gain

Twit blames remaining competant workers for downward spiral

Remaining competant worker leaves

Company does well-deserved tail spin, crashes and burns

VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share