micro optimisation in for loop c++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's assume C++ for loop in the form of:
for(int i; i<10;i++)
So the integer needs to be allocated in the beginning and then increased at every step the as well as compared. So wouldn't it be faster to do something like that:
f or(int i; i++<10;)
since the variable doesn't need to be loaded into the storage again? espacially when making it volatile?
This little example code gave the same result for all the cases. Does the for loop get optimized anyway or am I missing something?
#include<iostream>
#include<ctime>
int main() {
time_t start,ende;
volatile int dummy = 1;
const int rep = 1000000000;
// Method 1
start = time(0);
for (int i = 0; i < rep; i++)
dummy = 1;
ende = time(0);
std::cout << "Method 1: " << difftime(ende,start)*1000 << " ms" << std::endl;
// Method 2
start = time(0);
for (int i = 0; i++ < rep; )
dummy = 1;
ende = time(0);
std::cout << "Method 2: " << difftime(ende,start)*1000 << " ms" << std::endl;
// Method 3
start = time(0);
for (volatile int i = 0; i < rep; i++)
dummy = 1;
ende = time(0);
std::cout << "Method 3: " << difftime(ende,start)*1000 << " ms" << std::endl;
}
OS: Linux
Compiler: g++
Optimization: standart (no flag)

Compilers are a lot smarter than you think. They won't produce the kind of spurious load that you think they do.

Optimization depends on compiler and its options.
I'd suggest you to disassemble your code and see what's the result of the optimization.
A simple good disassemble e.g. HT editor or IDA version 5 (it free now). For small piece of code it will be easy enough.

Related

Cannot execute code after the loop in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm facing a problem in c++ that I don't understand.
this is my code:
auto DataArray = jvalue.at(U("data")).as_array();
std::cout << "Outside the loop, first output" << std::endl;
for (int i = 0; i <= 10; i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
std::cout << "Outside the loop, second output" << std::endl;
Output:
Outside the loop, first output
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
Press any key to continue . . .
It seems the code stops after the loop reach its end.
But why?
But if I commented out the
//auto data = DataArray[i];
//auto dataObj = data.as_object();
it doesn't have a problem.
By the way I'm working on cpprest and get json object data from api.
The jvalue variable holds the result.
And if I try and catch the code:
try {
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
catch (const std::exception& e) {
std::wcout << e.what() << std::endl;
}
the result is infinite loop with output: not an object.
Please help. Thank you.
I think you should use i < 10 instead i <= 10 in your loop:
for (int i = 0; i < 10; i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
Your last iteration hasn't output inside the loop. It fails there, there is no DataArray[10] with 10 index
And much better is to use DataArray.size() instead i < 10
for (int i = 0; i < DataArray.size(); i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}

Segmentation fault core dumped vector pointer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
void printTop5()
{
int counter = 1;
int x, y;
float civ;
cout << "Total no. of records available = " << pointVector.size();
cout << "Printing top 5 exploration destinations....";
sort(pointVector.begin(), pointVector.end(), ascendingCiv);
for (int i = 0; i < 5; i++)
{
PointTwoD p1 = pointVector[i];
x = p1.getX();
y = p1.getY();
civ = p1.getCivIndex();
cout << counter << ")\t";
if (civ > 0)
{
cout << "Civ idx: " << civ << " ; at sector(" << x << "," << y < ")\n";
}
else
cout << "<No records available>";
}
}
bool ascendingCiv(const PointTwoD &d1, const PointTwoD &d2)
{
return d1.getCivIndex() > d2.getCivIndex();
}
When I run this function I get this fault whereby it says segmentation fault core dumped. Any idea? Heard its about some memory problem.
One issue is that you are not checking whether you actually have at least 5 elements in your vector:
for (int i = 0; i < 5; i++)
{
PointTwoD p1 = pointVector[i];
If i is out-of-bounds of the vector, accessing pointVector[i] would invoke undefined behavior.
The loop condition can be changed to the following:
#include <algorithm>
//...
size_t loopEnd = std::min(pointVector.size(), 5);
for (size_t i = 0; i < loopEnd; i++)
{
PointTwoD p1 = pointVector[i];
The loop will either go up to the size of the vector or 5 elements, whichever is smaller.

Why is std:: move slower than copy? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Consider the following program. Why is move slower than copy? I thought that move semantics sped things up considerably when returning locals as rvalues from functions. Can you tell me why moving a large vector is slower than copying it. The benchmark for returning a shared_ptr is provided as a base case.
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
using namespace std;
vector<int> getStringByCopy()
{
vector<int> v;
for (int x = 0; x< 15000000;x++) v.push_back(x);
return v;
}
vector<int>&& getStringByMove()
{
vector<int> v;
for (int x = 0; x< 15000000;x++) v.push_back(x);
return move(v);
}
shared_ptr<vector<int>> getStringByPointer()
{
shared_ptr<vector<int>> v = make_shared<vector<int>>();
for (int x = 0; x< 15000000;x++) v->push_back(x);
return v;
}
int main(int argc, const char * argv[]) {
// insert code here...
chrono::system_clock::time_point begin = chrono::system_clock::now();
vector<int> v = getStringByCopy();
chrono::system_clock::time_point end = chrono::system_clock::now();
cout << v[0] << "copy took " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << endl;;
begin = chrono::system_clock::now();
vector<int>&& m = getStringByMove();
end = chrono::system_clock::now();
cout << m[0] << "move took " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << endl;;
begin = chrono::system_clock::now();
shared_ptr<vector<int>> sp = getStringByPointer();
end = chrono::system_clock::now();
cout << (*sp)[0] << "pointer took " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << endl;
return 0;
}
The output is:
0copy took 437043
0move took 462803
0pointer took 394549
Your method that is supposed to return per copy already return per move. If you return anything that were allocated on the stack per value in a method and return it, then you get a automatic move.

What is wrong in this solution to a codejam quiz? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Link of the question https://code.google.com/codejam/contest/4224486/dashboard
My solution:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int type_A(vector<int>vec){ // function for method 1 of computation;
int ret = 0;
for(auto i = 0;i<vec.size()-1;i++){
int a = vec[i],b=vec[i+1];
if(a>b)ret = ret+(a-b);
}
return ret;
} // end of 1st function
int type_B(vector<int>vec){ // function for method 2 of computation
int ret = 0;
for(auto i = 0;i<vec.size()-1;i++){
if(i==vec.size()-2){
if(vec[i]>vec[i+1])ret+=(vec[i]-vec[i+1]);
}else{
ret += vec[i];
}
}
return ret;
}
// end of function
int main()
{
ifstream input("input_file.in");
ofstream output("output_file.out");
int t;
input>>t;
for(auto i =1;i<=t;i++){
int n;
input>>n;
vector<int>vec(n);
for(auto j = 0;j<vec.size();j++){
int x;
input >>x;
vec[j] =x;
}
output << "Case #" << i << ": " << type_A(vec) << " " << type_B(vec) << endl;
}
}
When I run some examples given with the problems , I get the correct output but when I upload my output file to codejam it says that the answer is incorrect . Please help .
Your algorithm is wrong for type 2. It just happens that all of the examples happen to have the greatest difference items being between the second last item and the last item, which your code relies on (the test for i==vec.size()-2). This is obviously not the case for all of the full tests.
You will need to think of a different algorithm.

Inaccuracy in std::chrono::high_resolution_clock? [duplicate]

This question already has answers here:
What are the uses of std::chrono::high_resolution_clock?
(2 answers)
Closed 6 years ago.
So I was trying to use std::chrono::high_resolution_clock to time how long something takes to executes. I figured that you can just find the difference between the start time and end time...
To check my approach works, I made the following program:
#include <iostream>
#include <chrono>
#include <vector>
void long_function();
int main()
{
std::chrono::high_resolution_clock timer;
auto start_time = timer.now();
long_function();
auto end_time = timer.now();
auto diff_millis = std::chrono::duration_cast<std::chrono::duration<int, std::milli>>(end_time - start_time);
std::cout << "It took " << diff_millis.count() << "ms" << std::endl;
return 0;
}
void long_function()
{
//Should take a while to execute.
//This is calculating the first 100 million
//fib numbers and storing them in a vector.
//Well, it doesn't actually, because it
//overflows very quickly, but the point is it
//should take a few seconds to execute.
std::vector<unsigned long> numbers;
numbers.push_back(1);
numbers.push_back(1);
for(int i = 2; i < 100000000; i++)
{
numbers.push_back(numbers[i-2] + numbers[i-1]);
}
}
The problem is, it just outputs 3000ms exactly, when it clearly wasn't actually that.
On shorter problems, it just outputs 0ms... What am I doing wrong?
EDIT: If it's of any use, I'm using the GNU GCC compiler with -std=c++0x flag on
The resolution of the high_resolution_clock depends on the platform.
Printing the following will give you an idea of the resolution of the implementation you use
std::cout << "It took " << std::chrono::nanoseconds(end_time - start_time).count() << std::endl;
I have got a similar problem with g++ (rev5, Built by MinGW-W64 project) 4.8.1 under window7.
int main()
{
auto start_time = std::chrono::high_resolution_clock::now();
int temp(1);
const int n(1e7);
for (int i = 0; i < n; i++)
temp += temp;
auto end_time = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count() << " ns.";
return 0;
}
if n=1e7 it displays 19999800 ns
but if
n=1e6 it displays 0 ns.
the precision seems weak.