优化C ++模板执行 [英] Optimize C++ template executions
问题描述
我正在研究性能至关重要的项目.该应用程序正在处理大量数据.代码是用C ++编写的,我需要进行一些更改.
I am working on project where the performance is critical. The application is processing a huge amount of data. Code is written in C++ and I need to do some changes.
给出了以下代码(这不是我的代码,我将其简化到最低限度):
There is given following code (It is NOT my code and I simplified it to minimum):
void process<int PARAM1, int PARAM2>() {
// processing the data
}
void processTheData (int param1, int param2) { // wrapper
if (param1 == 1 && param2 == 1) { // Ugly looking block of if's
process<1, 1>();
else if(param1 == 1 && param2 == 2) {
process<1, 2>();
else if(param1 == 1 && param2 == 3) {
process<1, 3>();
else if(param1 == 1 && param2 == 4) {
process<1, 4>();
else if(param1 == 2 && param2 == 1) {
process<2, 1>();
else if(param1 == 2 && param2 == 2) {
process<2, 2>();
else if(param1 == 2 && param2 == 3) {
process<2, 3>();
else if(param1 == 2 && param2 == 4) {
process<2, 4>();
} // and so on....
}
主要功能:
int main(int argc, char *argv[]) {
factor1 = atoi(argv[1]);
factor2 = atoi(argv[2]);
// choose some optimal param1 and param2
param1 = choseTheOptimal(factor1, factor2);
param2 = choseTheOptimal(factor1, factor2);
processTheData(param1, param2); //start processing
return 0;
}
希望代码看起来很清楚.
Hopefully the code looks clear.
功能:
- 过程是处理数据的核心功能,
- processTheData 是流程函数的包装.
- process is the core function that is processing the data,
- processTheData is a wrapper of the process function.
参数( param1 和 param2 )接受的值数量有限(假设大约10 x 10).
There is a limited number of values that the params (param1 and param2) takes (Let's say about 10 x 10).
在执行之前不知道 param1 和 param2 的值.
The values of param1 and param2 are NOT known before execution.
如果我简单地重写 process 函数,以便它使用函数参数而不是模板常量(意味着 process(int PARAM1,int PARAM2)),则该处理约为慢十倍.
If I simply rewrite the process function so it uses the function parameters instead of template constants (means process(int PARAM1, int PARAM2)) then the processing is about 10 times slower.
由于上述 PARAM1 和 PARAM2 必须是 process 函数的常数.
Because of the above the PARAM1 and PARAM2 must be the constant of process function.
在 processTheData 函数中是否有任何聪明的方法来消除if的这个丑陋块?
Is there any smart way to get rid of this ugly block of if's located in processTheData function?
推荐答案
类似.
#include <array>
#include <utility>
template<int PARAM1, int PARAM2>
void process() {
// processing the data
}
// make a jump table to call process<X, Y> where X is known and Y varies
template<std::size_t P1, std::size_t...P2s>
constexpr auto make_table_over_p2(std::index_sequence<P2s...>)
{
return std::array<void (*)(), sizeof...(P2s)>
{
&process<int(P1), int(P2s)>...
};
}
// make a table of jump tables to call process<X, Y> where X and Y both vary
template<std::size_t...P1s, std::size_t...P2s>
constexpr auto make_table_over_p1_p2(std::index_sequence<P1s...>, std::index_sequence<P2s...> p2s)
{
using element_type = decltype(make_table_over_p2<0>(p2s));
return std::array<element_type, sizeof...(P1s)>
{
make_table_over_p2<P1s>(p2s)...
};
}
void processTheData (int param1, int param2) { // wrapper
// make a 10x10 jump table
static const auto table = make_table_over_p1_p2(
std::make_index_sequence<10>(),
std::make_index_sequence<10>()
) ;
// todo - put some limit checks here
// dispatch
table[param1][param2]();
}
这篇关于优化C ++模板执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!