Boost logo

Boost Users :

Subject: Re: [Boost-users] boost variant compile time visitation: verification
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-04-21 10:25:10


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_at_[hidden]> 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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net