سؤال

We know that object size depends on its field type. But does object instantiation time depend on its fields type? See my test

public class Test {
    byte f1;
    byte f2;
    byte f3;
    byte f4;
    byte f5;
    byte f6;
    byte f7;
    byte f8;
    byte f9;
    byte f10;

    public static void main(String[] args) {
        int n = 1000000;
        Test[] a = new Test[n];
        long t0 = System.currentTimeMillis();
        for(int i = 0; i < n; i++) {
            a[i] = new Test();
        }
        System.out.println(System.currentTimeMillis() - t0);
    }
}

Maybe not first class, still gives me rather stable results for different field types:

byte - 125 ms
int  - 250 ms
long - 370 ms

Why is that? I ran it from Eclipse on my notebook (Celeron 925), it needs -Xmx1024M.

هل كانت مفيدة؟

المحلول

Larger types will require more time as the OS has to search suitable amount of memory in the HEAP to fit that type. So the larger the type, it will require more time generally.

Most of the time Object instantiation time directly proportional to fields types

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top