Hicham,

visitation is not done at compile-time. Doc states as far as I remember that the compiler might (with high probability) inline the calls, so that they have similar performance as a switch statement. Switch usually equals to a jump in assembler. But there must be runtime overhead, since you put at runtime object instances into the variant.


Greetings,
Ovanes



On Tue, Apr 21, 2009 at 4:09 PM, Hicham Mouline <hicham@mouline.org> wrote:
Following my prev message, I wrote the following test on a win platform,
compiled with VS2005 /O2 in release mode

#include <iostream>
#include <boost/variant/variant.hpp>
#include <windows.h>

typedef boost::variant<int, double, char> VT;

struct print_type : public boost::static_visitor<const char*> {
 const char* operator()( int )
 {
   return "int";
 }
 const char* operator()( double )
 {
   return "double";
 }
 const char* operator()( char )
 {
   return "char";
 }
};


int main()
{
 const VT v('c');
 LARGE_INTEGER usec0, usec1;
 const char* volatile res;

 QueryPerformanceCounter( &usec0 );
 for(size_t s=0; s<2000000; ++s)
   res = v.apply_visitor( print_type() );
 QueryPerformanceCounter( &usec1 );
 std::cout<< (usec1.QuadPart - usec0.QuadPart) <<std::endl;

 QueryPerformanceCounter( &usec0 );
 for(size_t s=0; s<2000000; ++s)
   res = "char";
 QueryPerformanceCounter( &usec1 );
 std::cout<< (usec1.QuadPart - usec0.QuadPart) <<std::endl;
}

There is no statistically significant difference between the 2 cases.

Can I deduce that at least on this platform, for this code, the visitation
is done at compile-time?

Rds,

_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users