- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
// if I know that in_x will never be bigger than Max
template <unsigned Max>
void foo(unsigned in_x)
{
unsigned cap = Max;
// I can tell the compiler this loop will never run more than log(Max) times
for (; cap != 0 && in_x != 0; cap >>= 1, in_x >>= 1)
{
}
}
如上代码所示,我的猜测是如果我直接写
对于 (; in_x != 0; in_x >>= 1)
编译器不会展开循环,因为它不能确定最大可能的 in_x。
我想知道我是对还是错,是否有更好的方法来处理这些事情。
或者也许问题可以概括为好像可以编写一些代码来告诉编译器某些运行时值的范围,并且这样的代码不一定被编译成运行时二进制文件。
真的,和编译器打架XD
// with MSC
// if no __forceinline here, unrolling is ok, but the function will not be inlined
// if I add __forceinline here, lol, the entire loop is unrolled (or should I say the tree is expanded)...
// compiler freezes when Max is something like 1024
template <int Max>
__forceinline void find(int **in_a, int in_size, int in_key)
{
if (in_size == 0)
{
return;
}
if (Max == 0)
{
return;
}
{
int m = in_size / 2;
if ((*in_a)[m] >= in_key)
{
find<Max / 2>(in_a, m, in_key);
}
else
{
*in_a = *in_a + m + 1;
find<Max - Max / 2 - 1>(in_a, in_size - (m + 1), in_key);
}
}
}
最佳答案
实现这种行为的正确方法是使用 TMP 自行解除循环。即使这样,您仍将依赖编译器协作来进行大量内联(这是未授予的)。查看以下代码是否有帮助:
template <unsigned char MaxRec>
inline void foo(unsigned in_x)
{
if (MaxRec == 0) // will be eliminated at compile time
return; // tells the compiler to stop the pseudo recursion
if (in_x == 0) {
// TODO : end recursion;
return;
};
// TODO: Process for iteration rec
// Note: NOT recursion, the compiler would not be able to inline
foo<MaxRec-1>(in_x >> 1);
}
// Usage:
foo<5>(in_x); // doubt the compiler will inline 32 times, but you can try.
关于c++ - 如何向编译器提示循环运行的最长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9544601/
我正在尝试编写一个名为 map-longest 的 Clojure 实用函数(感谢备用名称建议)。该函数将具有以下“签名”: (map-longest fun missing-value-seq c1
为什么我创建了一个重复的线程 我在阅读后创建了这个线程 Longest increasing subsequence with K exceptions allowed .我意识到提出问题的人并没有真
我正在编写一个 Sub 来识别 1 到 1000 之间最长的 Collatzs 序列。由于我刚刚开始学习 VBA,我想知道如何添加过程来计算每个序列的长度。 Sub Collatz() Dim i
我正在编写一个 Sub 来识别 1 到 1000 之间最长的 Collatzs 序列。由于我刚刚开始学习 VBA,我想知道如何添加过程来计算每个序列的长度。 Sub Collatz() Dim i
我正在尝试减去 CSV 中的两列以创建第三列“持续时间”结束时间 - 开始时间 每一行也对应一个用户 ID。 我可以创建一个仅包含“持续时间”列的 csv 文件,但我宁愿将其重定向回原始 csv。 例
我在 2018.04 玩这个最长的 token 匹配,但我认为最长的 token 不匹配: say 'aaaaaaaaa' ~~ m/ | a+? | a+ /; # 「a」
因此,按照规范规定最终用户/应用程序提供的给定变量(200 字节)的字节长度。 使用 python 字符串,字符串的最大字符长度是多少,满足 200 字节,因此我可以指定我的数据库字段的 max_le
我需要针对我们的Jenkins构建集群生成每周报告。报告之一是显示具有最长构建时间的作业列表。 我能想到的解决方案是解析每个从属服务器(也是主服务器)上的“构建历史”页面,对于作业的每个构建,都解析该
我正在构建一个 iOS 应用程序,它将流式传输最长为 15 秒的视频。我阅读了有关 HLS 的好文章,因此我一直在对片段大小为 5 秒的视频进行转码。如果视频的第一部分加载时间太长,那么我们可以在接下
docs for Perl 6 longest alternation in regexes punt to Synopsis 5记录 longest token matching 的规则.如果不同的
我是一名优秀的程序员,十分优秀!