0

Is this any faster then using traditional way ie looping from index 0 -- to -- > lastIndex

//i --> element from left

//lastIndex -i --> element from right

    int ArraySum =0;
    int lastIndex = A.length-1;
    for(int i = 0; i <= lastIndex/2; i++){
        if(i == lastIndex-i){
        //it means its the same element 
        ArraySum += A[i];
        break;
       }
    // else we have element from left and element from right
    ArraySum += A[i] + A[lastIndex-i];  
}

test the code & compare the result on http://www.geeksforgeeks.org/program-find-sum-elements-given-array/

class Test
{
  public static void main(String[] args) 
{   
  int A[] = {3,4,5,10,2};
  int ArraySum =0;
  int lastIndex = A.length-1;
  for(int i = 0; i <= lastIndex/2; i++){
     if(i == lastIndex-i){
     ArraySum += A[i];
     break;
    }
    ArraySum += A[i] + A[lastIndex-i];  
   }
    System.out.println("Sum of given array is " +ArraySum);
 }
}
Tanmay jain
  • 814
  • 6
  • 11
  • 2
    Have a look [here](https://stackoverflow.com/a/1006425/6439023) – Luca Nicoletti Sep 04 '17 at 16:18
  • 1
    I doubt this code will find the sum of all Array element. Did you run it ? – Om Sao Sep 04 '17 at 16:19
  • why don't simply measure the time? – Fureeish Sep 04 '17 at 16:19
  • This is not duplicated – P_M Sep 04 '17 at 16:31
  • @ Om Sao +1 here. This is not the array elements sum – P_M Sep 04 '17 at 16:32
  • 2
    Imagine you are in a supermarket, and your task is to pick up one thing from each aisle. Would it be faster to pick up something in aisle 1, then run to aisle 10, then aisle 2, then aisle 9 etc; or would it be faster to go to aisle 1, aisle 2, aisle 3 etc, up to 10? The first strategy is like your code here; the second would be like just going through the array elements in turn. – Andy Turner Sep 04 '17 at 16:42
  • @Om Sao yes the code works you can compare the ans with traditional for loop method I think it works faster incase number of Elements are very large paste the code on http://www.geeksforgeeks.org/program-find-sum-elements-given-array – Tanmay jain Sep 04 '17 at 17:36
  • @Luca Nicoletti thanks for the link :) – Tanmay jain Sep 04 '17 at 17:45
  • @Andy Turner maybe I m wrong , but I wrote this code to reduce number of loops required to calculate ArraySum as we gonna access a[0]--->A[lastIndex] anyway why not acces two arrayElement in single loop This way no. of loops required reduces to half – Tanmay jain Sep 04 '17 at 17:56
  • @Tanmay yes, the number of iterations is halved, but you're accessing and adding two elements per iteration, so you're doing the same amount of work in the addition. But you've also significantly increased the chance of cache misses (repeatedly having to fetch a different chunk of the array from main memory, cf running from aisle 1 to aisle 20, then back to aisle 2), which will simply slow things down. – Andy Turner Sep 04 '17 at 19:24
  • 1
    Loop unrolling is a well-known optimisation technique where you step by 2/4/8/whatever summing ***adjacent*** entries, and many Java JIT compilers will do this automatically. [Don't waste your time on local optimisations](https://www.beyondjava.net/blog/a-close-look-at-javas-jit-dont-waste-your-time-on-local-optimizations/). – Ken Y-N Sep 05 '17 at 04:23
  • @KenY-N thanks! :) – Tanmay jain Sep 05 '17 at 09:48
  • @AndyTurner ohh..yeah I was forgetting to take increased probability of cache misses into account thank you – Tanmay jain Sep 05 '17 at 09:53

0 Answers0