0

For both codes l is initially 1 , all variables except the array are long long , array is global int of size N. N is to the order of 10^6. But code 1 exceeds time limit of 5 seconds but code 2 runs in milliseconds. The only difference is in the last line. a,b,c are integer variables less than 50.

Compiler used :- g++

test case :- 3000000 50 50 50 1 (N,a,b,c,l)

Code 1:-

for(i=1;i<N;i++) { j=l; k=(j*j)%1000000; k=(a*k)%1000000; j=(b*j)%1000000; l=(j+k+c)%1000000; x=(int)l; ar[i]=x; } 

Code 2:-

for(i=1;i<N;i++) { j=l; k=(j*j)%1000000; k=(a*k)%1000000; j=(b*j)%1000000; l=(j+k+c)%1000000; x=(int)l; ar[i]=1; } 
10
  • Is these timings an average for a number of runs or just a one off? Commented Mar 8, 2014 at 12:24
  • 1
    Which compiler are you using? Did you enable optimization? Commented Mar 8, 2014 at 12:24
  • You should post a test-case. There are too many unknowns to reach any meaningful conclusions. Commented Mar 8, 2014 at 12:25
  • @OMGtechy g++ compiler , yes optimization is enabled. Commented Mar 8, 2014 at 12:28
  • en.wikipedia.org/wiki/Loop-invariant_code_motion Commented Mar 8, 2014 at 12:30

1 Answer 1

2

It is compiler's optimisation. Probably second code is just replaced with

for(i=1;i<N;i++) { ar[i]=1; } 
Sign up to request clarification or add additional context in comments.

1 Comment

May not even bother to compute l

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.