How to copy values from one vector to another quickly

1

I'm making a question that the number of elements in the vector are in 10 ^ 6 and just wanted to copy certain element to it only I did the normal but common way, is there another possibility if I make the copy faster? >

for(i = 0; i < teste; i++)
{
    if(primo(vetor[i]))
    {
       aux[cont] = vetor[i];
       cont++;
    }
}

Algorithm to find the prime number

int primo(int num)
{
  int pri = 1, i, raiz = sqrt(num);
  if(num == 2)
  {
    return 1;
  }
  if (num == 1 || num == 0 || num % 2 == 0)
  {
    return 0;
  }
  for(i = 3; i <= raiz; i += 2)
  {
     if(num % i == 0)
     {
         pri = 0;
         break;
     }
  }
   return pri;
}
    
asked by anonymous 18.05.2018 / 18:27

1 answer

2

Essentially not. What is probably slowing down is the primo() function. Need to see if the algorithm is the most suitable (has a square root trick), if it caches results, or if it is to do some other optimization. Finding large cousins is very slow because it is exponential.

In theory, in certain situations, there could have been gain by using some library that manipulates the memory with vectorized instructions of modern processors or GPU, but it is not always possible to use (this seems a case that can not), and is very advanced .

    
18.05.2018 / 18:30