It is known that a while-loop(Ex: while(n--)
) or a for-loop(Ex: for(i=0; i<n; i++)
)'s time of execution depends on the variable n, i.e. O(n)
. Also, on an online judge, 10^7 operations ≈ 1s.
But I tried executing a while-loop and a for-loop for n > 10^9
with few operations and it seems to run easily under 1 sec. I am curious why this is happening?
#include <iostream> using namespace std; #define ll long long int main(){ ll t = 1e18; ll cnt = 0; while(t--){ cnt++; } cout << cnt << '\n'; cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; } Output: 1000000000000000000 Time elapsed: 0.003174 s.
cnt= 1e18;
– Paul Ogilvie10^7 operations ≈ 1s
is never static and differs on different platforms – thisisjaymehta