1

Question seems to be basic but I am not sure, but to the point. I have three vectors:

std::vector<int> vect1;
std::vector<int> vect2;
std::vector<int> vect3;

And for example vect1 has 3 members (10, 20 and 30), and vect2 has also 3 members with some other values like 15, 25 and 35. And I would like to push back all values of vect1 into vect3, and then push back all values of vect2 into vect3, so vect3 would have 6 members in order: 10, 20, 30, 15, 25, 35.

I know I can do that with for loop, but I wonder if I have thousands of members like in audio processing (eg. 44100) and each member would have some complex calculation, wouldn't be more CPU saving if I would make some simple steps? I imagine something like:

vect3 = vect1 + vect2;

or

vect3.push_back ...; // and here in some way all values of vect1 
vect3.push_back ...; // and here in some way all values of vect2

Maybe it's stupid, because even if there is some simple function like that, I suppose looping would be going somewhere under the ground in std::vector definition. But I just wonder out of curiosity.

At all I was always wondering about some basics things about loops, how much time is consumed by each single step of some basic loop, for example:

int a=0;
for (int i=1; i<=100; i++)
{
   a = i;
}

So I can calculate how much time is consumed by execute whole loop. Is there any way I can check it? And other stupid questions, for example, does it matter for CPU load if I create variables outside (before) the loop, like here:

int a;
int b;
int c;
for (int i=0; i<100; i++)
{
    a = i * 2;
    b = i * 3;
    c = a + b;
}

Or maybe better would be like that:

int c;
for (int i=0; i<100; i++)
{
    int a = i * 2;
    int b = i * 3;
    c = a + b;
}

Where to find answer for such questions?

Quentin
  • 62,093
  • 7
  • 131
  • 191
pajczur
  • 235
  • 3
  • 10
  • I ask that question, also because my professor on university told me that better, and more proper way to programming (at least in C++) is to make each single task in separate functions or even classes. But if I have some complicated loops, for example FFT, and then draw the results on the screen, I need to make loop in FFT function and separate loop (which is similar) in drawing function I wonder wouldn't it be better for CPU if make one big and complicated loop, then two big loops one by one? Now I don't have any subject with that professor so I can't ask him for that kind of details. – pajczur Mar 07 '18 at 19:14
  • 1
    Also keep your questions to one per post - though the "answer" to the second one is to benchmark and test (and take a look at the generated assembly code), especially since based on the specific code different compilers may be able to optimize differently – UnholySheep Mar 08 '18 at 13:25
  • Modern C++ compilers are pretty good. Your whole loop will likely end up as `NOP` - no operation. This is one of the challenges of benchmarking; you typically need to make sure that the code you're measuring actually does something noticeable or the compiler may discard the unnoticeable work. – MSalters Mar 08 '18 at 13:27

0 Answers0