CSC300 / CSC402: XPerformanceOfArrays *1.1 [10/13] Previous pageContentsNext page

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

Output

00 
00 01 
00 01 02 
00 01 02 03 
00 01 02 03 04 
00 01 02 03 04 05 
00 01 02 03 04 05 06 
00 01 02 03 04 05 06 07 
00 01 02 03 04 05 06 07 08 
00 01 02 03 04 05 06 07 08 09 
00 01 02 03 04 05 06 07 08 09 10 
00 01 02 03 04 05 06 07 08 09 10 11 12 
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 
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 
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 27 28 29 
Elapsed count f(          512):             5,721:      2.024 [     0.001 :   Infinity]
Elapsed count f(        1,024):            11,402:      1.993 [     0.001 :      1.000]
Elapsed count f(        2,048):            22,520:      1.975 [     0.002 :      2.000]
Elapsed count f(        4,096):            48,320:      2.146 [     0.001 :      0.500]
Elapsed count f(        8,192):            94,698:      1.960 [     0.002 :      2.000]
Elapsed count f(       16,384):           185,318:      1.957 [     0.005 :      2.500]
Elapsed count f(       32,768):           362,372:      1.955 [     0.006 :      1.200]
Elapsed count f(       65,536):           772,598:      2.132 [     0.012 :      2.000]
Elapsed count f(      131,072):         1,509,413:      1.954 [     0.018 :      1.500]
Elapsed count f(      262,144):         2,948,653:      1.954 [     0.018 :      1.000]
Elapsed count f(      524,288):         6,283,714:      2.131 [     0.048 :      2.667]
Elapsed count f(    1,048,576):        12,272,649:      1.953 [     0.046 :      0.958]
Elapsed count f(    2,097,152):        23,970,316:      1.953 [     0.051 :      1.109]
Elapsed count f(    4,194,304):        46,819,577:      1.953 [     0.102 :      2.000]
Elapsed count f(    8,388,608):        99,760,500:      2.131 [     0.223 :      2.186]
Elapsed count f(   16,777,216):       194,835,921:      1.953 [     0.454 :      2.036]
Elapsed count f(   33,554,432):       380,541,255:      1.953 [     0.953 :      2.099]
Elapsed count f(   67,108,864):       743,288,834:      1.953 [     1.579 :      1.657]
Average delta: 2.002

N pushes in linear time

Each time we resize: constant pushes multiply by 1.1

Each push is amortized constant time

The choice of multiplier value is a tradeoff between time and space.

Previous pageContentsNext page