I am solving a question where theres a main while loop with condition being int i =0; while(i<vector.size()) and inside this while loop I have another while loop that increases i if current element in the index is odd. Such as while(i<vector.size()&&vector[i]%2!=0 )i++;. My question is if i replaced the second while loop with and if statement, would my performance be different? which would be faster and why? I know Big O complexity would be O(N) in both cases but I am talking about real world speed. I am using c++ if it matters. Does the compiler make optimizations for the while loop/ is there more checks to do in an if statement and so on? This is just a question out of curiosity.
int i=0;
vector<int> temp(1000,1);
while(i<temp.size())
{
while(i<vector.size()&&temp[i]%2!=0)
{
i++;
}
}
or
int i=0;
vector<int> temp(1000,1);
while(i<temp.size())
{
if(i<vector.size()temp[i]%2!=0)
{
i++;
}
}
Aucun commentaire:
Enregistrer un commentaire