是模板元编程比等价的C code更快? [英] Is Template Metaprogramming faster than the equivalent C code?

查看:126
本文介绍了是模板元编程比等价的C code更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时的模板元编程比等价的C code更快? (我说的是运行时的性能):)

Is Template Metaprogramming faster than the equivalent C code ? ( I'm talking about the runtime performance) :)

推荐答案

首先,免责声明:
我什么认为的你问的是不只是模板元编程,而且还泛型编程。这两个概念是密切相关的,还有是对各包括没有确切的定义。但在短期,模板元编程基本上是编写使用模板程序,这在编译时进行评估。 (这使得它完全自由的在运行时,没有任何反应。值(或更常见的类型)已经由编译器计算,并且可作为常数(无论是一个const变量或枚举),或者作为一个typedef嵌套在一个类(如果你用它来计算一型)。

First, a disclaimer: What I think you're asking about is not just template metaprogramming, but also generic programming. The two concepts are closely related, and there's no exact definition of what each encompasses. But in short, template metaprogramming is essentially writing a program using templates, which is evaluated at compile-time. (which makes it entirely free at runtime. Nothing happens. The value (or more commonly, type) has already been computed by the compiler, and is available as a constant (either a const variable, or an enum), or as a typedef nested in a class (if you've used it to "compute" a type).

通用编程通过模板和必要时,模板元编程,以创建通用code其中的工作方式相同(并与在性能没有损失),与所有和任何类型。我将在下文中同时使用的例子。

Generic programming is using templates and when necessary, template metaprogramming, to create generic code which works the same (and with no loss in performance), with all and any type. I'm going to use examples of both in the following.

有关模板元编程的一个常见用途是使类型的泛型编程中使用,即使他们没有为它设计的。

A common use for template metaprogramming is to enable types to be used in generic programming, even if they were not designed for it.

由于模板元编程技术上完全发生在编译​​时,你的问题是泛型编程,它还是发生在运行多一点有关,但是是有效率的,因为它可以专门为它使用了precise类型在编译时。

Since template metaprogramming technically takes place entirely at compile-time, your question is a bit more relevant for generic programming, which still takes place at runtime, but is efficient because it can be specialized for the precise types it's used with at compile-time.

反正...


取决于你如何定义相当于C code。

Depends on how you define "the equivalent C code".

有关模板元编程(或一般通用编程)的诀窍在于,它允许大量的运算被转移到编译时,它可以灵活,参数化code,这只是硬$ C高效$ CD值。

The trick about template metaprogramming (or generic programming in general) is that it allows a lot of computation to be moved to compile-time, and it enables flexible, parametrized code that is just as efficient as hardcoded values.

在code显示这里例如在计算在编译时Fibonacci序列号码。

The code displayed here for example computes a number in the fibonacci sequence at compile-time.

C ++的code 无符号长fib11 =斐波纳契< 11uL> ::值,依赖于该链接所定义的模板元编程,并作为有效作为C code'无符号长fib11 = 89uL 。模板在编译时计算,产生可分配给一个变​​量的常量。因此,在运行时,code实际上是等同于一个简单的任务。

The C++ code 'unsigned long fib11 = fibonacci<11uL>::value', relies on the template metaprogram defined in that link, and is as efficient as the C code 'unsigned long fib11 = 89uL'. The templates are evaluated at compile-time, yielding a constant that can be assigned to a variable. So at runtime, the code is actually identical to a simple assignment.

因此​​,如果这是相当于C code,其性能是一样的。
如果等效C code是,可以计算出任意斐波纳契数的程序,适用于找到该序列中的第11号,那么C版本将慢得多,因为它必须被实现为功能,它计算在运行时的值。但是,这是相当于C code在某种意义上说,它是指具有同样的灵活性(这不只是硬codeD不变的,但实际的功能,可以返回的任何一个C程序的在Fibonacci序列号)。

So if that is the "equivalent C code", the performance is the same. If the equivalent C code is "a program that can compute arbitrary fibonacci numbers, applied to find the 11th number in the sequence", then the C version will be much slower, because it has to be implemented as a function, which computes the value at runtime. But this is the "equivalent C code" in the sense that it is a C program that exhibits the same flexibility (it is not just a hardcoded constant, but an actual function that can return any number in the fibonacci sequence).

当然,这是不常见的。但它是pretty多模板元编程的典型的例子。

Of course, this isn't often useful. But it's pretty much the canonical example of template metaprogramming.

泛型编程的一个更现实的例子是排序。

A more realistic example of generic programming is sorting.

在C,你有的qsort 标准库函数取一个数组和一个比较函数指针。这个函数指针调用不能被内联(除非在琐碎的情况下),因为在编译时,它不知道哪个函数将被调用。

In C, you have the qsort standard library function taking an array and a comparator function pointer. The call to this function pointer can not be inlined (except in trivial cases), because at compile-time, it is not known which function is going to be called.

当然的选择是专为您的特定数据类型的手写排序功能。

Of course the alternative is a hand-written sorting function designed for your specific datatype.

在C ++中,相当于是函数模板的std ::排序。它也需要一个比较,但不是这是一个函数指针,它是一个函数对象,看起来像这样:

In C++, the equivalent is the function template std::sort. It too takes a comparator, but instead of this being a function pointer, it is a function object, looking like this:

struct MyComp {
  bool operator()(const MyType& lhs, const MyType& rhs) {
     // return true if lhs < rhs, however this operation is defined for MyType objects
  }
};

,这可以被内联。在的std ::排序函数传递一个模板参数,因此它知道比较的确切类型,因此它知道该比较器功能不只是一个未知函数指针,但 MyComp ::运算符()

and this can be inlined. The std::sort function is passed a template argument, so it knows the exact type of the comparator, and so it knows that the comparator function is not just an unknown function pointer, but MyComp::operator().

最终的结果是,C ++函数的std ::排序是的究竟的有效率在C你手工codeD执行同样的排序算法。

The end result is that the C++ function std::sort is exactly as efficient as your hand-coded implementation in C of the same sorting algorithm.

所以,再一次,如果是相当于C code,那么性能是一样的。
但如果等价的C code是,它可应用到的任何的类型广义分类功能,并允许用户定义比较器,那么通用编程版本在C ++是大大更有效。

So again, if that is "the equivalent C code", then the performance is the same. But if the "equivalent C code" is "a generalized sorting function which can be applied to any type, and allows user-defined comparators", then the generic programming-version in C++ is vastly more efficient.

这是真正的伎俩。泛型编程和模板元编程不是比C更快。他们的方法来实现的一般情况下,可重复使用code 的是尽可能快的手codeD和硬盘codeD C

That's really the trick. Generic programming and template metaprogramming are not "faster than C". They are methods to achieve general, reusable code which is as fast as handcoded, and hardcoded C

这是一种方式来获得两全其美。硬codeD算法的性能,和一般的,参数化的人的灵活性和可重用性。

It is a way to get the best of both worlds. The performance of hardcoded algorithms, and the flexibility and reusability of general, parameterized ones.

这篇关于是模板元编程比等价的C code更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆