Re: [Boost-users] boost variant compile time visitation: verification

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,

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

This sentence from the doc Notable features of <http://www.boost.org/doc/libs/1_38_0/doc/html/boost/variant.html> boost::variant include: * Full value semantics, including adherence to standard overload resolution rules for conversion operations. * Compile-time type-safe value visitation via <http://www.boost.org/doc/libs/1_38_0/doc/html/boost/apply_visitor.html> boost::apply_visitor. is confusing to me. I was gonna ask if there was a purely compile-time visitor, but that doesn't make sense, as the instance of the variant can be assigned any of the bounded types at runtime. Regards, From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Ovanes Markarian Sent: 21 April 2009 15:25 To: boost-users@lists.boost.org Subject: Re: [Boost-users] boost variant compile time visitation: verification 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

Compile-time type-safe value visitation via boost::apply_visitor.
is confusing to me.
It means that the *dispatching* (i.e. the decision where to visit) is made at compile-time. The *execution* of the visitation obviously occurs at runtime, because you define runtime functions in the visitor, not metafunctions. However, in case of trivial function body (like the one in your example), the optimizer does its job and omits the function call - so you get "true" compile-time visitation.
participants (3)
-
Hicham Mouline
-
Igor R
-
Ovanes Markarian