Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何减少constexpr函数的编译时间?_C++_Optimization_Constexpr - Fatal编程技术网

C++ 如何减少constexpr函数的编译时间?

C++ 如何减少constexpr函数的编译时间?,c++,optimization,constexpr,C++,Optimization,Constexpr,假设我们想在编译时构建一个非平凡的表 template<int N, int M> constexpr auto foo() { std::array<std::array<int, N>, M> a = {}; for(int m = 1; m < M; m++) for(int n = 1; n < N; n++) { // For exposition only

假设我们想在编译时构建一个非平凡的表

template<int N, int M>
constexpr auto foo()
{
    std::array<std::array<int, N>, M> a = {};
    for(int m = 1; m < M; m++)
        for(int n = 1; n < N; n++)
        {
            // For exposition only
            auto x = (m ^ 42) + (n << 3) - m;
            auto y = (n ^ 420) + (m % 420);
            a[m][n] = (a[(x + m) % m][(y + n) % n] + (x ^ y)) % 0xFACADE;
        }
    return a;
}

constexpr auto bar(int n, int m)
{
    constexpr auto dim = /* something */;
    constexpr auto table = foo<dim, dim>();
    return table[n][m];
}
模板
constexpr auto foo()
{
std::数组a={};
对于(int m=1;mauto x=(m^42)+(n我想编译器只是优化了以下几点,所以您可能没有什么优势,但是:

1)
m^42
m%420
不依赖于
n
,因此您可以在内部循环之外计算它们

2) 如果我没有错

(x + m) % m  ==  x % m + m % m
             ==  x % m + 0
             ==  x % m

3) 您可以尝试将一些
const
添加到
auto
变量中

所以你可以试试看

template <int N, int M>
constexpr auto foo ()
{
    std::array<std::array<int, N>, M> a = {};

    for(int m = 1; m < M; m++)
    {
        auto const m42 = m ^ 42;
        auto const m420 = m % 420;

        for(int n = 1; n < N; n++)
        {
            // For exposition only
            auto const x = m42 + (n << 3) - m;
            auto const y = (n ^ 420) + m420;
            a[m][n] = (a[x % m][y % n] + (x ^ y)) % 0xFACADE;
        }
    }
    return a;
}
模板
constexpr auto foo()
{
std::数组a={};
对于(int m=1;mauto const x=m42+(n是否必须在编译时执行?您试图用它解决的真正问题是什么?用例是什么?在本地运行它。没有top*(或超时)在您的本地计算机上。@Someprogrammerdude没有。但最初的问题引起了我的兴趣。另一方面,我可以想到一个我曾经试图在编译时构建东西的constexpr解析器。这当然是愚蠢/格式的问题。@Codo我的意思是它使编译时间长得离谱。我想知道预编译头是否可以在这里有用吗?(它们不会直接减少编译时间,但可以通过缓存第一次编译的结果以供重用来减少重新编译代码的次数)
(x+m)%m
防止出现负值。提升是优化器所做的事情之一。考虑到它们的SSA形式是相同的,我非常确定
const
在这里完全被忽略,因为它们的SSA形式是相同的。最后,这些都适用于运行时的代码,但由于
constexpr
函数的工作方式和糟糕的性能它们的运行速度,我怀疑它们是完全不同的,这些建议不一定会被翻译过来。作为轶事证据,我看不出这个版本和原始版本之间有什么区别。@passwayr-“防止负值”-Ops;我没有考虑过。但是…所以你应该写
x=m42+(n
template <int N, int M>
constexpr auto foo ()
{
    std::array<std::array<int, N>, M> a = {};

    for(int m = 1; m < M; m++)
    {
        auto const m42 = m ^ 42;
        auto const m420 = m % 420;

        for(int n = 1; n < N; n++)
        {
            // For exposition only
            auto const x = m42 + (n << 3) - m;
            auto const y = (n ^ 420) + m420;
            a[m][n] = (a[x % m][y % n] + (x ^ y)) % 0xFACADE;
        }
    }
    return a;
}