简体   繁体   English

C ++性能:模板vs boost.any

[英]C++ Performance: template vs boost.any

I was wondering if using boost.any (Without RTTI) classes anywhere templates can be used will slow down the program. 我想知道如果使用boost.any(没有RTTI)类,可以使用任何模板都会降低程序的速度。 Since boost any is actually a wrapper around a template class, one could say that with the modern compiler-optimalisations it will produce the same effect, am I right? 由于boost any实际上是模板类的包装器,可以说现代编译器优化它会产生相同的效果,我是对的吗?

tpl_vs_any.hpp tpl_vs_any.hpp

#include <iostream>
#include <vector>

using namespace std;

template<class T> class tpl
{
    T content;
public:
    tpl(const T& value) : content(value) {}
    operator T() const
    {
        return content;
    }
};

class any
{
public:

    any() : content(0) {}

    any(const any& other) : content(other.content -> clone()) {}

    template<class T> any(const T& value) : content(new holder<T>(value))
    {
    }

    ~any() 
    {
        delete content;
    }

    class placeholder
    {
    public:
        placeholder() {}
        virtual placeholder* clone() const = 0;
    };

    template<class T> class holder : public placeholder
    {
    public:
        T content;

        holder(const T& value) : content(value) {}
        ~holder() {}

        placeholder* clone() const
        {
            return new holder<T>(content);
        }
    };

    template<class T> operator T () const
    {
        return dynamic_cast<holder<T>*>(content)->content;
    }

    placeholder* content;
};

template<class T> void test()
{
    for (int i = 0; i < 10000; ++i)
    {
        vector<T> a;
        a.push_back(23.23);
        a.push_back(3.14);

        double x = (double)a[0];
    }
}

So would it be correct to say that: 所以这样说是正确的:

test<any>();

Is exactly as fast as: 完全一样快:

test<tpl<double>>();

Assuming that you know, just like the compiler does at the second example, that boost::any is only used as double in this situation? 假设您知道,就像编译器在第二个示例中所做的那样, boost::any仅在这种情况下用作double? (No RTTI for the any class). (任何课程都没有RTTI)。

I'm more wondering about the arguments for and against this thesis. 我更想知道支持和反对这一论点的论点。

Also, are there specific situations where there is difference between those methods? 此外,是否存在这些方法之间存在差异的特定情况?

Edit: Performance test 2: 编辑:性能测试2:

  • Example 1: 1,966.57 ms 例1:1,966.57毫秒
  • Example 2: 1,320.37 ms 例2:1,320.37毫秒

It seems like there is a relativly large difference. 似乎存在相对较大的差异。

Edit 2: Since it was not fair to compare the primary data type double against the class any I've made a new test: 编辑2:由于将主要数据类型double与类比较any我做了一个新测试是不公平的:

#include "tpl_vs_any.hpp"

int main()
{
    test<any>();
    return 0;
}

Speed: 1,794.54 ms 速度:1,794.54毫秒

#include "tpl_vs_any.hpp"

int main()
{
    test<tpl<double>>();
    return 0;
}

Speed: 1,715.57 ms 速度:1,715.57毫秒

Tested it multiple times, pretty much the same benchmarks. 多次测试,几乎相同的基准测试。

So would it be correct to say that: 所以这样说是正确的:

... ...

Is exactly as fast as: 完全一样快:

... ...

Assuming that you know, just like the compiler does at the second example, that boost::any is only used as double in this situation? 假设您知道,就像编译器在第二个示例中所做的那样,boost :: any仅在这种情况下用作double?

No. Current compilers do nowhere near that kind of level of introspection. 不是。当前的编译器远没有那种内省程度。 boost::any will be slower. boost::any会慢一些。

Of course, you could just run the code and find out for yourself. 当然,您可以运行代码并自行查找。

boost::any internally holds a pointer to an object, which it allocates with new . boost::any内部保存一个指向对象的指针,它用new分配。 One of the things that makes std::vector significantly faster than, say, std::list is that vector keeps all of its objects in a contiguous storage in a single allocation, which aside from the obvious reduction in memory allocation overhead, is also a lot more cache-friendly. 使std::vector明显快于std::list就是该向量将所有对象保存在单个分配中的连续存储中,除了明显减少内存分配开销外,还有更多缓存友好。

There's also the detail of adding RTTI to the allocation, which is generally trivial overhead, but in the case of really small types like double significantly increases the storage overhead. 还有将RTTI添加到分配的细节,这通常是微不足道的开销,但是在像double这样的非常小的类型的情况下,显着增加了存储开销。

boost::any is not part of the standard; boost::any不是标准的一部分; it's a specific implementation of a specific template. 它是特定模板的特定实现。 So you might as well just benchmark it; 所以你不妨只是对它进行基准测试; there aren't a bunch of other competing "standard implementations". 没有一堆其他竞争的“标准实施”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM