简体   繁体   English

如何比 for 循环更有效地初始化数组? C++

[英]How do I initialize arrays more efficiently than a for loop? c++

Currently, I am initializing arrays by creating for loops and setting arr[I] = 0. Is there another way to do this more efficiently?目前,我正在通过创建 for 循环并设置 arr[I] = 0 来初始化数组。还有其他更有效的方法吗?

Student::Student(){
    
    name = "John";
    for(int i = 0; i<sizeof(id);++i)
    {
        id[i] = '0';
    }
    for(int i = 0; i<sizeof(testScore);++i)
    {
        testScore[i] = 0;
    }
    avgScore = 100;
    grade = 'A';
    
}

You can try Zero initialization .您可以尝试 零初始化

example:例子:

    int arr1[12] = {}; // values: 000000000000
    std::array<int, 5> arr2 = {}; // values: 00000

This way you can skip the for loop and have a cleaner looking code.通过这种方式,您可以跳过 for 循环并获得更清晰的代码。

Loop Unrolling循环展开

Here is an example of loop unrolling:这是循环展开的示例:

const int capacity = sizeof(id) / sizeof(id[0]);
const int fill_value = 0; // hopefully compiler assigns to a register.
int i = 0;
if (capacity & 3 == 0)
{  
    for (i = 0; i < capacity; i += 4)
    {
        id[i + 0] = fill_value;
        id[i + 1] = fill_value;
        id[i + 2] = fill_value;
        id[i + 3] = fill_value;
    }
}
// initialize remaining slots
for (; i < capacity; ++i)
{
    id[i] = 0;
}

The use of fill_value variable rather than as a constant in the loop is to suggest to the compiler of placing fill_value into a register.在循环中使用fill_value变量而不是作为常量是为了向编译器建议将fill_value放入寄存器中。 In many processors, writing a register to memory is more efficient than either writing a constant to memory or loading the constant to register, then writing to memory (during the loop).在许多处理器中,将寄存器写入内存比将常量写入内存或将常量加载到寄存器然后写入内存(在循环期间)更有效。

The idea of unrolling the loop is execute more data instructions in a row before the jump or branch instruction.展开循环的想法是在跳转或分支指令之前在一行中执行更多数据指令。 Processor's don't like branch instructions.处理器不喜欢分支指令。 Branch instructions cause the processor to decide to reload the instruction cache or not;分支指令使处理器决定是否重新加载指令缓存; this occupies time that could be spent executing data instructions.这会占用可用于执行数据指令的时间。 The unrolled loop is also performing less compares than the original loop.展开的循环执行的比较次数也少于原始循环。 Again, compare instructions occupy time that could be spent executing data instructions.同样,比较指令占用的时间可能用于执行数据指令。

No Looping无循环

The goal is to minimize branching and comparing.目标是最小化分支和比较。
If your array is small (by your definition), you may want to assign the slots without a loop:如果您的数组很小(根据您的定义),您可能希望在没有循环的情况下分配插槽:

int * p = &id[0];
const int fill_value = 0;
*p++ = fill_value;  *p++ = fill_value; *p++ = fill_value; *p++ = fill_value;  
*p++ = fill_value;  *p++ = fill_value; *p++ = fill_value; *p++ = fill_value;  
*p++ = fill_value;  *p++ = fill_value; *p++ = fill_value; *p++ = fill_value;  
*p++ = fill_value;  *p++ = fill_value; *p++ = fill_value; *p++ = fill_value;  
*p++ = fill_value;  *p++ = fill_value; *p++ = fill_value; *p++ = fill_value;  

The operation of incrementing the pointer and deferencing is still faster than having the processor compare a value or change the program counter (branch).增加指针和延迟的操作仍然比让处理器比较值或更改程序计数器(分支)更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM