简体   繁体   中英

Array Declared inside method makes a difference in time from an array Declared outside a method and used inside as an input

Using java to try to solve a mathematical problem, and looking for ways to increase the efficiency of the solution, I got a very significant big increase in execution time and did not know how it came from. After a couple of tests I may have found the answer but still I do not know how or why this happens.

Here is the test code which shows this time difference:

public class arrayAcessTime {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        AccessTime();
    }

    public static long getLargestPrimeFactor(long l, int[] x)
    {
        long p = 0, n = l/2, r = (long) Math.sqrt(l), y = 49;

        for(long i = 13; i <= r;)
        {
            for(int j = 0; j<x.length; j++)
            {
                if (l % i == 0) 
                {
                  n = l/i;
                } 

                else 
                {
                   n = l / (i + 1); 
                }

                i+=x[j];
            }
        }

        return p;
    }

    public static long getLargestPrimeFactor(long l)
    {
        long p = 0, n = l/2, r = (long) Math.sqrt(l), y = 49;

     int x[] = {2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2,         

      System.out.println("x size: " + x.length);

        for(long i = 13; i <= r;)
        {
            for(int j = 0; j<x.length; j++)
            {
                if (l % i == 0) 
                {
                  n = l/i;
                } 

                else 
                {
                   n = l / (i + 1); 
                }

                i+=x[j];
            }
        }

        return p;
    }

    public static void AccessTime() {

        int array2[] = {2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2,....} //too large to write here 


        long start;
        double diff;

        System.out.println("Array2 size: " + array2.length);

        start = System.currentTimeMillis();

        getLargestPrimeFactor(8798765600851475143L, array2);

        diff = (System.currentTimeMillis() - start) / 1000.0;

        System.out.println("Time: " + diff);


        start = System.currentTimeMillis();

        getLargestPrimeFactor(8798765600851475143L);

        diff = (System.currentTimeMillis() - start) / 1000.0;

        System.out.println("Time: " + diff);

    }
}

Output:

Array2 size: 5760
Time: 6.144
x size: 5760
Time: 30.225

As you can see the time increase is very significant (about 5 times). The two methods are nearly the same except that one has an array initialized inside it while the other takes the initialized array as input. How or why this can cause this significant increase in time? On the other hand, I have not notice any noteworthy difference for considerably smaller arrays (<500). So, it seems to me that only larger arrays are affected by this. Does this make it better to initialize an array outside a method and take it as an input to the method instead of declaring it inside? Is this the same for other languages? Whats the better approach or it depends on the situation? Thanks Alot!

I have a hunch - but it's only a hunch, and it's still surprising.

This is a relatively long-running method - so Hotspot will probably JIT-compile it multiple times, applying more and more optimization.

The bytecode for your first method is pretty short, so JITting it is quick. However, the byte code for your second method will be huge - and this may mean that most of the time is spent JITting.

You could verify this by running each method multiple times, and printing out how long each iteration takes - I'd expect the "late" runs of each method to take about the same amount of time.

Again, I'd be surprised if this accounted for all the difference (I'd expect the JIT to optimize the initialization bytecode once and then ignore it after that) but it's at least worth testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM