<small id='4Q6c1'></small><noframes id='4Q6c1'>

      <i id='4Q6c1'><tr id='4Q6c1'><dt id='4Q6c1'><q id='4Q6c1'><span id='4Q6c1'><b id='4Q6c1'><form id='4Q6c1'><ins id='4Q6c1'></ins><ul id='4Q6c1'></ul><sub id='4Q6c1'></sub></form><legend id='4Q6c1'></legend><bdo id='4Q6c1'><pre id='4Q6c1'><center id='4Q6c1'></center></pre></bdo></b><th id='4Q6c1'></th></span></q></dt></tr></i><div id='4Q6c1'><tfoot id='4Q6c1'></tfoot><dl id='4Q6c1'><fieldset id='4Q6c1'></fieldset></dl></div>
    1. <tfoot id='4Q6c1'></tfoot>

      <legend id='4Q6c1'><style id='4Q6c1'><dir id='4Q6c1'><q id='4Q6c1'></q></dir></style></legend>
      • <bdo id='4Q6c1'></bdo><ul id='4Q6c1'></ul>
    2. C/C++ 中的自展开宏循环

      时间:2023-07-20

            <tbody id='l6rlV'></tbody>
            <bdo id='l6rlV'></bdo><ul id='l6rlV'></ul>

            <tfoot id='l6rlV'></tfoot>

            • <legend id='l6rlV'><style id='l6rlV'><dir id='l6rlV'><q id='l6rlV'></q></dir></style></legend>
              <i id='l6rlV'><tr id='l6rlV'><dt id='l6rlV'><q id='l6rlV'><span id='l6rlV'><b id='l6rlV'><form id='l6rlV'><ins id='l6rlV'></ins><ul id='l6rlV'></ul><sub id='l6rlV'></sub></form><legend id='l6rlV'></legend><bdo id='l6rlV'><pre id='l6rlV'><center id='l6rlV'></center></pre></bdo></b><th id='l6rlV'></th></span></q></dt></tr></i><div id='l6rlV'><tfoot id='l6rlV'></tfoot><dl id='l6rlV'><fieldset id='l6rlV'></fieldset></dl></div>

              1. <small id='l6rlV'></small><noframes id='l6rlV'>

                本文介绍了C/C++ 中的自展开宏循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我目前正在处理一个项目,其中每个周期都很重要.在分析我的应用程序时,我发现一些内部循环的开销非常高,因为它们只包含一些机器指令.此外,这些循环中的迭代次数在编译时是已知的.

                所以我想不是用 copy & 手动展开循环.我可以使用宏在编译时展开循环,以便以后可以轻松修改.

                我的印象是这样的:

                #define LOOP_N_TIMES(N, CODE) <在此处插入魔法>

                这样我就可以替换 for (int i = 0; i < N, ++i) { do_stuff();} 与:

                #define INNER_LOOP_COUNT 4LOOP_N_TIMES(INNER_LOOP_COUNT, do_stuff();)

                它自己展开:

                do_stuff();做东西();做东西();做东西();

                由于大多数时候 C 预处理器对我来说仍然是个谜,我不知道如何实现这一点,但我知道这一定是可能的,因为 Boost 似乎有一个 BOOST_PP_REPEAT 宏.不幸的是,我不能在这个项目中使用 Boost.

                解决方案

                您可以使用模板展开.请参阅示例的反汇编 .

                <小时>

                生活在 Coliru

                模板<无符号N>struct faux_unroll {模板 静态无效调用(F const& f){F();faux_unroll<N-1>::call(f);}};模板 <>struct faux_unroll<0u>{模板 静态无效调用(F const&){}};#include #include int main() {srand(时间(0));双 r = 0;faux_unroll<10>::call([&] { r += 1.0/rand(); });std::cout <<r;}

                I am currently working on a project, where every cycle counts. While profiling my application I discovered that the overhead of some inner loop is quite high, because they consist of just a few machine instruction. Additionally the number of iterations in these loops is known at compile time.

                So I thought instead of manually unrolling the loop with copy & paste I could use macros to unroll the loop at compile time so that it can be easily modified later.

                What I image is something like this:

                #define LOOP_N_TIMES(N, CODE) <insert magic here>
                

                So that I can replace for (int i = 0; i < N, ++i) { do_stuff(); } with:

                #define INNER_LOOP_COUNT 4
                LOOP_N_TIMES(INNER_LOOP_COUNT, do_stuff();)
                

                And it unrolls itself to:

                do_stuff(); do_stuff(); do_stuff(); do_stuff();
                

                Since the C preprocessor is still a mystery to me most of the time, I have no idea how to accomplish this, but I know it must be possible because Boost seems to have a BOOST_PP_REPEAT macros. Unfortunately I can't use Boost for this project.

                解决方案

                You can use templates to unroll. See the disassembly for the sample Live on Godbolt

                But -funroll-loops has the same effect for this sample.


                Live On Coliru

                template <unsigned N> struct faux_unroll {
                    template <typename F> static void call(F const& f) {
                        f();
                        faux_unroll<N-1>::call(f);
                    }
                };
                
                template <> struct faux_unroll<0u> {
                    template <typename F> static void call(F const&) {}
                };
                
                #include <iostream>
                #include <cstdlib>
                
                int main() {
                    srand(time(0));
                
                    double r = 0;
                    faux_unroll<10>::call([&] { r += 1.0/rand(); });
                
                    std::cout << r;
                }
                

                这篇关于C/C++ 中的自展开宏循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何可靠地判断 boost 线程是否已退出其 run 方法? 下一篇:使用 Visual Studio 2013 (Express) 构建提升

                相关文章

              2. <i id='Qw7ms'><tr id='Qw7ms'><dt id='Qw7ms'><q id='Qw7ms'><span id='Qw7ms'><b id='Qw7ms'><form id='Qw7ms'><ins id='Qw7ms'></ins><ul id='Qw7ms'></ul><sub id='Qw7ms'></sub></form><legend id='Qw7ms'></legend><bdo id='Qw7ms'><pre id='Qw7ms'><center id='Qw7ms'></center></pre></bdo></b><th id='Qw7ms'></th></span></q></dt></tr></i><div id='Qw7ms'><tfoot id='Qw7ms'></tfoot><dl id='Qw7ms'><fieldset id='Qw7ms'></fieldset></dl></div>
                <tfoot id='Qw7ms'></tfoot>
              3. <legend id='Qw7ms'><style id='Qw7ms'><dir id='Qw7ms'><q id='Qw7ms'></q></dir></style></legend>

                    <small id='Qw7ms'></small><noframes id='Qw7ms'>

                    • <bdo id='Qw7ms'></bdo><ul id='Qw7ms'></ul>