CSC300 / CSC402: XPerformanceOfArrays *1.5 [9/13] Previous pageContentsNext page

01
02
03
04
05
  public void push(double item) {
    if (N == a.length) resize((int)Math.ceil (N*1.5));
    a[N] = item;
    N += 1;
  }

Output

00 
00 01 
00 01 02 
00 01 02 03 04 
00 01 02 03 04 05 06 07 
00 01 02 03 04 05 06 07 08 09 10 11 
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
Elapsed count f(          512):             1,922:      2.189 [     0.000 :        NaN]
Elapsed count f(        1,024):             3,144:      1.636 [     0.001 :   Infinity]
Elapsed count f(        2,048):             6,831:      2.173 [     0.000 :      0.000]
Elapsed count f(        4,096):            14,872:      2.177 [     0.000 :        NaN]
Elapsed count f(        8,192):            32,453:      2.182 [     0.001 :   Infinity]
Elapsed count f(       16,384):            52,782:      1.626 [     0.002 :      2.000]
Elapsed count f(       32,768):           114,681:      2.173 [     0.002 :      1.000]
Elapsed count f(       65,536):           249,859:      2.179 [     0.003 :      1.500]
Elapsed count f(      131,072):           407,564:      1.631 [     0.004 :      1.333]
Elapsed count f(      262,144):           884,271:      2.170 [     0.006 :      1.500]
Elapsed count f(      524,288):         1,924,095:      2.176 [     0.012 :      2.000]
Elapsed count f(    1,048,576):         3,148,295:      1.636 [     0.020 :      1.667]
Elapsed count f(    2,097,152):         6,821,541:      2.167 [     0.039 :      1.950]
Elapsed count f(    4,194,304):        14,824,201:      2.173 [     0.042 :      1.077]
Elapsed count f(    8,388,608):        32,305,900:      2.179 [     0.147 :      3.500]
Elapsed count f(   16,777,216):        52,653,164:      1.630 [     0.150 :      1.020]
Elapsed count f(   33,554,432):       114,275,340:      2.170 [     0.508 :      3.387]
Elapsed count f(   67,108,864):       248,730,932:      2.177 [     1.268 :      2.496]
Average delta: 2.025

N pushes in linear time

Each time we resize: constant pushes multiply by 1.5

Each push is amortized constant time

Previous pageContentsNext page