NaW
NaW

Reputation: 89

Make a loop work with having to cout every i in C++

I'm creating this code to time how long it takes a processor to do this operation for a school project. The problem was I couldn't get my loop working without printing every a result, if I didn't print the result of every a when multiplied by i, the processor would skip the loop. After solving this problem I tought than I am trying to benchmark my CPU and if I print every result it might affect on the performance or the time it takes to solve the operations. I've trying adding an if in the loop and only printing a when i=999999 but it doesn't work. Anybody can help?

#include <iostream>
#include <ctime>

using namespace std;

int main () {
    int start_s=clock();

    int i;
    for(i=0;i<1000000;i++){
        int a = i*434243;
        if(i = 999999){
            cout<<a;
        }

    }
    int stop_s=clock();
    cout << "time: "<< (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000;

    cout<<"ms";
    system ("pause");
    return 0;
}

Upvotes: 0

Views: 581

Answers (5)

Caleth
Caleth

Reputation: 62719

Your loop has undefined behaviour, so strictly speaking anything is allowable.

warning: iteration 4946 invokes undefined behavior [-Waggressive-loop-optimizations]
     int a = i*434243;
note: within this loop
     for(int i=0;i<1000000;i++){
                 ~^~~~~~~~

Ignoring that, it's perfectly reasonable for code that can be calculated by the compiler to be, and the results put directly where used in the resulting program. Thus 0ms is a perfectly reasonable amount of time to do a calculation.

With a more precise clock, you may be able to observe it taking non-zero time, although that is dominated by the overhead of calling now

#include <iostream>
#include <chrono>

int main () {
    auto start = std::chrono::high_resolution_clock::now();

    for(unsigned i=0u;i<1000000u;i++){
        unsigned a = i*434243u;

    }
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> diff = end-start;
    std::cout << "Time: " << diff.count() << " s\n";

    int dummy;
    std::cin >> dummy;
    return 0;
}

Time: 1.869e-06 s

See it live

Upvotes: 0

user8235810
user8235810

Reputation:

It is less effect of optimisation but more so about std::cout being an expensive operation. Hence, when you remove cout from for loop, it takes less than 1 millisecond. However, with cout executing miliions of times makes your program really slow which is expected behaviour. You can add some more arithmatic operation where you are using "a" and you can print result outside the for loop so that "a" is still significant. E.g.

int i=0;
int a = 0;
for(;i<1000000;i++){
    a = i*434243;
    a += i;
    //cout<<a<<endl;
}
cout<<a<<" " <<i<<endl;

Related link here.

Upvotes: 0

William J Bagshaw
William J Bagshaw

Reputation: 565

Actions that have no effect will be optimised away. The content of your loop only effects a local variable that is not used before it goes out out scope. (it is in-scope only inside the loop.) It is often difficult to create test code that does not optimise away to nothing. Alternatively you keep the code that you want but end up timing other actions such as saving the result as well. I suggest you try,

  • Put "int a;" outside the loop.
  • Use "volatile int a;"
  • Make a function and return a result from your loop.
  • Make the calculation in the loop dependent on a previous iteration. For example a = a * a;

You could, simply, turn down the optimisation, for example use -o0. However your performance will be poor and the timing would not be representative of normal real life code compiled with optimisation on.

Upvotes: 0

Dampen59
Dampen59

Reputation: 573

I don't have enough points to comment, but I think your problem comes from your vars type.

You should be doing this way :

double time = (double)(stop_s-start_s) * 1000.0 / CLOCKS_PER_SEC;
cout << "time: "<< time;

And change the type of stop_s and start_s to clock_t instead of int

Upvotes: 0

Andrii
Andrii

Reputation: 1906

You have a mistake in the if expression. Should be i == 999999 instead of i = 999999

Upvotes: 2

Related Questions