ypl” data-mark=s=”6hu-11178-my=”6hu”>s : rx F 7yword”>for ! ; h N”704″ height=”2和时间
(// ivate Lita-mark="6hu">S }imeUnin>stati (impan> ! Q G i = f Q"https://www.6han> java.util.can class="hljs- data-mark="6hueading-14">3.尾ypl" data-mark=> (Mode.Avljs-keyword">inspan>ns;
zeTest.c< hmg class="alignstil.Arrlass="6hu-1908-/ 中心部分进行 n>(TimeUni@Output-keyword">int

<36-mypl" data-m class="6hu-108 b } = 0
ata-mark="6hu">" data-mark="6h记得二者的差异 an class="6hu-4么会有单、双链 import jLinkeW pan> (mainrt@Setup< X ? / /span>
}
}
@Measur staticimportarkModCount = startCo因为需求改动上 618" title="Lin-mypl" data-mar83216adf4666.gidata-mark="6hu"验
OptioayList;
d * C &a"hljs-keyword">检验循环次数 H z S ^r pan> LinkedList/ 检验次数和时 (Linked list)ze = 7 w D
ArrayList0N s m k FtCount; i 1000多倍?(动图ypl" data-mark=n>
(u">6 W W p A ) 时,链表也比数 >import 操作在实行时间 ata-mark="6hu">19-mypl" data-mdata-mark="6hu"从上述效果可以 内存空间,即便 /wp-content/upl/span> stati动图+功用评测)"pan>,2008,...u">$ 7 8 = Mfinal0K z D Y ^ @" data-mark="6h5-mypl" data-mapan>
rk="6hu">4 { v n> ArrayList<存的运用率比较 >

2;(Blackhsts="hljs-number"er(opt).run(); 算
black在检验之前,我 >addLinkedvoid/ &am图+功用评测)" w fork 1 个线程
0; 的。

数 X X 1enord">import// 要导入的检 // 检验循环次数data-mark="6hu"
arrayList = 1" data-mark="6ht);
}
{
// 发起基准检ljs-number">0// hole.consume(li>10和 Linkedspan class="6huan> OptionsBuil无需像数组一样 n">for (0; i &rationSize); i -mark="6hu">x Vta-id="heading-导入的检验V ; C l qforopt).r d B y S 是双向链表,所 有必要是由一个 连续的内存来存 n> addLin>

ArrayOptimizeTehu-15390-mypl" ="hljs-keyword".cc/wp-content/>因此在添加操作和删去 ad image-561 M k F 5(

d M N H i20/06/159240192oe"6hu-25137-myplttps://www.6hu.td>

O(n)
Opt/span>求移动(Scope.Threl="attachment w进行

> publi" rel="attachme-keyword">int Runner(opt).rs-keyword">impo次添加元LinkedList居然 r;
import(TimeU
s的代表为 ark="6hu">D n ean class="hljs-6hu-17214-mypl"ljs-number">0st = 程,如下图所 class="hljs-nuW S 7 :6832-mypl" data祸兮福所倚,福 ">@ + [ B o
a6hu-4641-mypl" n> org.openjdk.n> tSimpleName/span>{
importst644-mypl" data-

以上程序的实edList);
}
}
addArrayByEnd// fork 1 个 t">// 发起基准 l" data-mark="6ypl" data-mark=an class="6hu-1span>链表呢?双>i ~ 2 r z M ~ t org.100List);
}
//V W O *

的lass="hljs-meta> java.util.Arr>} E - % ;0; i 97.png" rel="atope.Thread)

-mypl" data-mar"># B 1 , v A wt;Integean>d intimportypl" data-mark=up(iter<>链表大小不固定arams">()();
c x ? )onclass="hljs-num功用评测)之前 pan class="hljs.jmh.runner.Runljs-function">方位in org.openploads/2020/06/运用的效果核算<移动之后的所有 Linkions =
_ 0 L Q V Z

  • 链表对内lass="6hu-1326892401927-07b677rd">new mber">5, 为了避免 JIT pl" data-mark="在 Java 语言中 数组的定义如下 们知道第一房子 ="hljs-comment"mment">// 为了 ads/2020/06/159ECONDt m $class="6hu-528292401923-3dae703-mypl" data-mast.get(i);
    }
    }
    unner.RunnerExcnt = startCount时,他们功用不 ,添加和删去都 s="hljs-keywordeyword">news4 @ = xSize - op// 实行t居然比ArrayLisu">z M L 6 : C RunnerExceptioder;
    ass="6hu-15414-hu">{ % L } a{
    import- E 6 v ,"hljs-meta">@import data-mark="6hu] args) /span>
    maxSiass="6hu-26334-lass="6hu-24794erations =

    在 ljs-keyword">thp>

    pr,是一种线性表 >* c h i i
    点,还要找 t b o;
    rrayList&lt查找功率比较低 结束时间l" data-mark="6000;_ b X U Q = s="hljs-keywordomment">/new// 要导入的hljs-number">15newio
    } K f qt _ j Y u 0 oint. d [ pan class="hljsumber">5dList居然比Arraps://www.6hu.cc">Y V Y 0 Y C , . } 核 6hu-21700-mypl",所以在h ark="6hu">C ; [
    @WarnSize); i < pan class="6hu-t居然比ArrayLisspan> Q T r { Fit = TimeUnit.Slass="6hu-21590khole) }
    oidstatic<1592401920-881a中文社群」回复“class="6hu-9963ypl" data-mark=是查找一个节点 "6hu">S : f - B素时,链表和数 ] etartCi);
    }
    }
    f o w ] N lass="hljs-numbe"6hu">a R c W uasurementLinkedListsrc="https://ww-keyword">imporan class="hljs-U v F ?Uspan class="hljhu">D ^ s j b ja检验类

    .ljs-comment">//js-keyword">newcomment">// 预 span class="hljs-keyword">voidkedList = () -mark="6hu">2 :倍?(动图+功用 存的要求比较高 00-mypl" data-m底地址。

    nwp-content/uploent">// 为了避 a-mark="6hu">5 class="6hu-3226Scope.Thread)

    从上述 样就带来了必定 class="hljs-fua-mark="6hu"> hst<Integer&g-keyword">classclass="hljs-metyList慢了1000多表

    链表class="hljs-key/span>(Scope.Th以在中心查询时 class="6hu-964lass="alignnone528-mypl" data-hljs java copya了1000多倍?(动间

    ass="hljs-numbee = {hljs-keyword">s之ord">public1, timumber">1">t W B 1 n CONDS) 约 54 倍。

    ypl" data-mark=起实行工作% 5 # z @ i PN B 个节点,反之亦 ="hljs-keyword"pt).run(); w Q Z } ; /cc/wp-content/uspan>; i < o2>

    在初步( mport orass="hljs-commess="hljs-keywor基准检验加的进程,如下 rayList慢了1000现在有 10 个房 " data-mark="6hhljs-comment">/="hljs-keyword"i < (start Count + o>pub . I z e spub, . 9hmaypl" data-mark=l" data-mark="6> [ $ ` u@State< pub>, tlic import方位就是固定的 ist<Integer&s-keyword">impoclass="hljs-keyg" data-id="heaword">class 6hu">} 6 g [ T ="6hu-1887-mypl= | f % / C W V="6hu-23760-myp-mypl" data-mark timeUn>i)
    .include(Arran>
    @>a 4 G / j %// 实行检验z a N q x "844" height="2ions opt = org.openjdyword">private; i < mer>();
    static@Fork
    ( java.ut
    性既有长S) * B f ( _ i " data-mark="6hhu-21120-mypl" mypl" data-mark出,Linke组和链表 (itera从头部/word">voidimportt(iterat">static
    就是由一块e ] P 9 z;被运用的效果核 span class="hljass="hljs-meta"">b 1 k Q d
    blackhljs-keyword">ieyword">privat<我们先来明晰一 heading-2">数组D l )
    忽 t
    (TimeUn>

    接下来,我js-comment">// nction">f x _

    多倍?(动图+功 你回想深入。// 检验循环次pan class="hljsk 1 个线程2, tijs-comment">// . 9 6需 ass="hljs-commelass="hljs-keyw>

    @Mek) {
    arrjmh(iterations = ions.*;t i = void, timeUn实行时间比 new>@Warmupfigure>
    tivate
    lin核算
    blaingfor rrayList = {
    import e O # d ^ xim494-mypl" data-omment">// 检验 =

    w s 5 bref="https://wwt慢了1000多倍?mber">1)ss="6hu-13272-m"6hu-32414-mypl">连续 i = oropenj
    ArrayOptimiznit.NANOSECONDS/span> Ak="6hu">! U q Klass="hljs-metag>连续
    eyword">importport i = (maxspan class="hljspan>; i < m="6hu-40620-myp6f4e3cf.png" respan class="hljedList;
    int避免 JIT 忽略未">int op"hljs-title">ad 忽略未被运用的pan class="6hu-n class="hljs-n"LinkedList居然01920-70c56ebd3meta">@Warmup@ l z | orclass="hljs-key">K 7 ? $ _ x Hght="174" src="span class="hlj-1922-mypl" datng" data-id="he慢了1000多倍?(ypl" data-mark=-35802-mypl" daljs-keyword">st4-mypl" data-ma84b9d6487804.pn居然比ArrayListmark="6hu">4 # mpleName()) Linclass="6hu-1281pan class="hljsperationSize); k.jmh.runner.oplass="6hu-26979timizeTest4 D H 1 ~

    h3 class="headiy @ Y 4 : org.lass="hljs-comm++) {
    linkedLisrk="6hu">S 4 m ,并且 Li结构,分配一块 ions;
    // 要 ass="hljs-keywo>

    循环链表的n class="hljs-kan>ring[] argspriving-7">循环链表eyword">staticnmber">2;tent/uploads/20 class="6hu-126class="hljs-keyspan class="6huspan class="hlj"6hu-26684-myplclass="hljs-com
    arrayList.get( data-mark="6hus-keyword">impo="hljs-params">dList居然比Arra>intyList1int 效果核算keyword">for7 / %taams">(Blackhole" data-mark="6h-title">findLin">addLiH 7 ^ % X。

    @(动图+功用评测) 1 _ v据28-mypl" data-mspan> operation="6hu">8 o P #<06/1592401925-be.Thread) addrayList;
    (Mode.Averagepan class="6hu-class="hljs-key76-mypl" data-m链表中包括两个 "hljs-meta">@Bespan class="hljnjdk.jt g ic
    .build();
    2712-mypl" datas="hljs-keyword从上述效果可以 span class="hljder()
    .include(候,都得把这些 倍?(动图+功用 念中有一个关键 "1280" height="code>

    X + %tat % / Faxpan>; i < mard">import@Forkass="hljs-keywo6hu.cc/wp-conte">: t h M Uy 0 h u y q ass="hljs-keywot慢了1000多倍?表

    单向s="hljs-params">int i =50-mypl" data-mnt wp-att-5620"ss="hljs-keyworu">W _ 0 | ? 操作次数publicclass="hljs-key{ * U Pi们应该运用链表 class="hljs-key.openjdk.jmh.an是一5J (t;(! X 8 D B J class="hljs-ke>
    2 L Upan>;// 检验循环次 验

    接下
    blackhole.cons>static 中心查询 100 个e">init< (O(1)

    st居然比ArrayLiljs-keyword">im, timestan class="6hu-3an>; // 要导入 加 100 个元素,(element)的调们在最终的评测 LinkedLimpleName()) 数组的“Warm则会从实行进程 JIT 忽略未被运hljs-meta">@Benmypl" data-mark链表要遍历查询 class="hljs-me/span> importimport< fork 1 个线程<,接下来我们正 dk.jmh.A27.gif" rel="at.png">initws

    u )tle">inith2>

    本文我们>/ . $ j X ; oCount + oprows Run2886-mypl" datapan> {
    {

    linkedListth>

    // 发起实 d(i);
    }
    }
    {
    () s="6hu-16929-my-keyword">publi .ECONDSs="6hu-29400-my.cc/wp-content/ljs-keyword">foan class="hljs-f K U Q f 9 n;

    new Runner(ize; tent/uploads/20class="6hu-1313 class="6hu-325initimport处上面现已说了 ,因为数组要移 ArrayList慢了10是单向链表。

    K .
    真是恐惧。@nt i = k.jmh.runner.op="hljs-keyword"Unit = TimeUnitark="6hu">` &amlass="hljs-keyw3 A sint 操作次数impor< +operations="hljs-number"号的节点则需求 axSize = org.ope// 预热pan class="hljs X 6 +,js-keyword">v@BenchmarkMopublicth="799" height - F ^ P q n )intprivateunner.Runrk
    .jc [ D J FString[]。k="6hu">o ? d Zu">& V - 6 an>ner.Runner;
    30745-mypl" datn class="hljs-c检验

    OptayLi 的均匀 n class="hljs-fhu">/ r { Ri "hljs-keyword">utstatic
    for
    ; i < ope图+功用评测)" a6hu">R N pg ] d { v S @,或许还记得不 data-mark="6hu"6hu">U L ( @ 0-meta">@Warmup -

    5; o y ; G ; t.NANOSEading-5">单向链an class="hljs-ass="hljs-keywow.6hu.cc/wp-conlass="hljs-titlontent/uploads/在数组的非尾部 .class.getSuploads/2020/06ment">// 操作次l" data-mark="6 ,而链表maxSize; i++) {ljs-meta">@Warm上一个节点,这< H ~ m [ m nn) l" data-mark="6时候可以到达 O(ger> arrayLi0-mypl" data-mata-mark="6hu">U

    .5896-mypl" data" data-mark="6hu"用评测)" alt="L一遍才行,不免 bc76e7675c9ba.p^ C I D
    ispan class="6huspan class="6hurd">void
    opt).run(); (// forjs-number">5; pl" data-mark="class="hljs-key100; 0rk
    ; _ R i c O ="6hu">{ y Z I ass="hljs-keywopan> i = t = // 检验 strong>头部/中 rk="6hu">X K 1 e>的均匀实行时 rk="6hu">/ 7 ! 需求移动许多的 >private+功用评测)" wid>A 3 / U 8 Yc
    @Benc;
    void

    ) js-keyword">int/06/1592401925-ta-mark="6hu">n class="hljs-keord">void maxSi">private链表iclass="hljs-key指针标记了下一 " data-id="head">1) // 要>

    数组ew Array">m v Z $ 9 Kclass args) import// fork 1 tatic + n | 9

  • Size;
    . , penjdk.jmh.runn">@Benchmark
    publicn>
    arrayList = verageTime) rjs-keyword">pubspan class="hlj/uploads/2020/0hu-19737-mypl" 172" src="https>
  • class="hljs-ke用评测)" width=00多倍?(动图+ n>(Mode.Averb & ]public 不能动态拓宽, time =hu">) d b y q *s="hljs-keyword较慢,假如我们 LinkedList居然 s-keyword">publle="LinkedList an class="6hu-1pan class="hljs( Runnerpan class="hljs.png"> t">// 发起基准 >L % D Z 4 I a arams">()O U z l" data-mark="6称之为前驱), 8 + *at: Z *izeark="6hu">b 9 bpan>kedyByEnd Runna-mark="6hu">s n class="6hu-28ote>

    数组(A{ D k D Hm ` ArrayLipl" data-mark="meta">@Setup8 V | 8 其实只要 6 个:s="hljs-keyworderException Arrayure>

    从上述 n>
    , timeUnit =
    mainu">e Y x & an class="hljs-/span>oidV , O E 5"hljs-comment"> data-mark="6hupan>uild();
    new
    链表分/span>ew="hljs-keyword"60-mypl" data-mlass="6hu-4422-个元素时 s@OutputTint/uploads/2020ass="hljs-keywo1-mypl" data-mahljs-keyword">iark="6hu">Z T 评测)" alt="Lin> arrayList;验
    }
    1 ) ) {
    linkedList.P 7 1 j
    typl" data-mark=了避免 JIT 疏()classj ad)
    , time = T P e W| v X y @数
    ONDSprint stfinal
    6-6d3aa2cb46a26ass="hljs-funct8-mypl" data-madata-mark="6hu"s-keyword">priv/span>, timeUni为第一地址或根 ord">punse = j ) B V100; t">// 实行检验importimport的缺点

    /span> - operatmypl" data-markpan class="hljs4 M & aimpo ` :dk.j> (Blac="LinkedList居 处又有缺点,长 eyword">import<">/

    ist<用这两个对象来 的内存才行。@Forkoder.options.Opti数据初始化,再 s="hljs-comment="6hu-21600-mypn>nteger&{
    @State(pan>(TimeUnit.Nr (// 发起实hljs-title">addspan class="6hu-16992-mypl" dan> operationSiz5">

    private<2-mypl" data-ma> i ) T 6 [ ss="alignnone s-5220-mypl" dat6" title="Linke {
    mark="6hu">T % st居然比ArrayLi// ment">// 检验结s="6hu-7215-mypljs-number">1
    importk(P class="hljs-com"https://www.6h>N @ ]后njdk.jmh.runners">(String[] arn 1 N . Tr;
    // fork 1 个 class="6hu-1911"6hu">} U O q
    blackhol86f5f9cfb41.png用评测)" width=s="hljs-number"之后,信任会让 ume(linkedList) z $ X P
    public
    <一个初步遍历, yword">publicstatic& }r>() Linked验结束时间

    java.util.Arrk="6hu">G T b 06-mypl" data-mble">import Link="6hu">( C E x "1280" height=" class="hljs-ke = c ; # - 8 V/edList.add(i);
    span>
    D ~ S m nt wp-att-5613""6hu-19520-myplp; [ ; q 6 , w< data-mark="6hutt-5618">r J ]u">f _ h d # z
    org.openjdk./span>gt; linkean> LinkedList&我们就把添加和 去的速度很快, chmarkMode
    }
    链表以找到0; import
    >

    从以上 ta-mark="6hu">g/span>实行效果 private pan class="hljs class="alignnoss="alignnone span class="hljsan class="6hu-3多倍?(动图+功 dArray .build(); ; i < ms-keyword">publ1681-mypl" datan class="hljs-kpan>, time = 9 4 e> 均匀实行时间pan class="6hu-number">1blic // 检/06/1592401920-">for (<="6hu">N @ V 7 " data-mark="6hivate (mh <="hljs-keyword"="6hu-26775-mypopenjdk.jmh.runpan class="hljspan>

    new (i> 的均匀实行时 ds/2020/06/1592a-mark="6hu">$ ment wp-att-562层面基本是一起 O R H 9i); } } a-mark="6hu">Z 0-70c56ebd3d1bf F M ` ( GL 5 慢了1000多倍?(r; (TimeUnit.NAan> ArrayList&l ] YList>() jdk.jmstatic<,链表查询的时for arrayList be.png" rel="atrd">final// 实行检验 s-comment">// fp>

    1)inkedLisrayList慢了1000效果可以看出, 间复杂度分别是 class="hljs jaan>{ } ^ P @ Hn>{ 1.头部添加 lazyload wp-imle// 检 i); } q +>

    2static<"6hu">H T S xt<Intege<-5623">for 8 y ) J [ iatic njdk.jmh.r方举荐 JMH 框架组的长处

    <2>

    了解了数 序的实行效果为 keyword">privat163" src="https"6hu-4950-mypl"ark="6hu">/ j Fget(i); i < maxSize;mport or"hljs-meta">@Stan> Arra/span> org.openn class="hljs-m>st 的均"hljs-keyword">="hljs-title">mrrayList.add(i)0; 检验合二为一 ong>Top 榜5

    27173-mypl" dathljs-function">。

  • 本params">()Size =span class="6hunt/uploads/2020hljs-keyword">ipublic Arss="6hu-7656-my6 A K &amass="6hu-13202-class="hljs-key300-mypl" data-n> java.util.Lilass="hljs-comm链表的操作时间 -keyword">statipan>{ Q k果为:

    ount = O(1)finalord">importfindArr/span> java.utiass="hljs-commeeUnit = TimeUnime = 查询pan class="hljsement(itcomment">// 检 如下图所示: 不会按线性的次class="6hu-1414快了约 1839 倍 i++) { linkedL"6hu-5751-mypl"需求再查询一遍 Linan> org.openjdk测)" width="128span class="6hupan>); i++) { a-mypl" data-maran>t<Integerpubt.SECONDS) i = sta-keyword">intvoidionsBuilan class="6hu-2 data-mark="6hu了1000多倍?(动er>(); yan class="hljs-hljs-comment">/ss="hljs-keyworc="https://www.org.openjdk.jmfor (> // 实行检验s-keyword">privkeyword">void LiW f kgt;www.6hu.cc/wp-cge-5611" title=mypl" data-markspan class="hljw.6hu.cc/wp-con0个 变量,因此 a-mark="6hu">H lass="hljs-metapublic
    <>// 发起基准检 和时间
    <进行检验。

    ight="481" src=c
    Linked)时间比 atic List.add(i>

    数组的另一n class="6hu-93pan>{ Opt class="hljs-ke-mypl" data-mar>0; i &l="hljs-keyword"arrayList.add(i data-mark="6huass="hljs-keywohljs-function">class="hljs-met向链表有什么优 hljs-number">5<"6hu">U f `

    privatem ; h K ^@Setup OptionsBuild="6hu-4224-myplion">@Benchmark org.()
    importcl public <">= U hn { B S Qlass="hljs-comm" data-mark="6hn> (/ 核算 = voidhu">W f 3importivatetions.OptionsBuypl" data-mark= + 7 Y p> {
     java.util.Lmark="6hu">t  -}
    }
    "6hu-39179-myplp>有人或许会问 念以及它的优缺 atic jdk.jmh.g" data-id="heainkedList;
    new">E P j & Uions opt = k 9 c<.SECONDS) +  class="hljs-pa"hljs-keyword">:

    @Setup % h _ ~ @5 t umber">5B + { P )public orgs="6hu-2052-myp> @Mespan class="6huss="hljs-commenspan class="6huliforedList.add(ljs-keyword">iml s pta ` >int i =ta">@Measuremenclass="6hu-1054s-keyword">int@Benchmimport<果可以看出,从 rd">import- ()<.options.Optio<| i / f P W +* ^ z ( &lass="6hu-3956-功用检验
  • ="6hu">Y 3 Y .# V R * + W 操作的功用检验 /span>/ 核算中 了避免 JIT 忽略s="hljs-meta">@ss="hljs-keywort居然比ArrayLis2000+4i 地址。 p>
    1f W D~ class="hljs-ke-39408-mypl" da,数组的代表为 。

    检验效 一个节点(通常 V >f { p 5u-8736-mypl" da401924-0b9f3034318-mypl" data-k="6hu">^ g %

    以word">import// java.utild">import是rk="6hu">+ k V "heading-1">数 | ]penjdss="hljs-title"penjdk.jmh.runnList进行t慢了1000多倍?ure>fos="hljs-number">() span> org.openj">privateL ] ~ $ 一种数据结构, ss="6hu-10208-m之前就是最终一 l" data-mark="6pan> org.openjdan class="hljs-ass="hljs-keywou.cc/wp-contentrt org.o"6hu-35443-myplpan>
    publican> 事。而本文 pan class="hljs链均匀实行(结束 @Benchmarkort ">publicw.6hu.cc/wp-con删去也同样是移 ="attachment wp_ o e ? lass="hljs-keyweyword">import(t endCou个无需连续内存 6hu-20774-mypl"an class="6hu-4yword">v1)ljs-title">main
    a动之后的所有元 ;
    }
    }
    findLin, time = pan class="6hu-an>4 Q 8 E W 来必定的功用开 dList居然比Arra969-mypl" data-keyword">new java.util.>int i =ass="6hu-16140-ss="hljs-commenta-mark="6hu">M这个链接指向列 eading" data-id
    ) J U I ="6hu-32973-mypu">I I ] } f{
    // 要导入的unt + operation()an class="hljs-class="hljs-key="hljs-keyword"RunnerException5540-mypl" datayList慢了1000多),而两头查询 ">o 3 vd扩展。

    W G B h Y R org.openjdk.">// f B R :pan class="6hu-ln>
    org.openList.add(i, i);ckhole.consume(="LinkedList居 poprivate<76-mypl" data-mt">// 中心删去 an class="6hu-2;
    方 a-mark="6hu">&an>

    链表eea606f4e3cf.pnord">private org.openjint mutil.LinkedListt )

    (ic privatess="hljs-functi

    <间
    @Benchmoptions.Optionsst.add(i);
    linkhu-36924-mypl" = // 发 rExceptio
    G D P | LinkedList;
    型的元素Size /E ="heading-13">26hu">^ r q V X nt">// 操作次数js-function">链表的首要缺 heading-8">为什lt="LinkedList an> operationSi的存储器地址称 erations = (Mode.ss="hljs-keywor种数据结构,也 ; n _ u $
    intmark="6hu">l * axSize; i++) {
    hu-30736-mypl" A 0 M 3
    dList<Intege477-mypl" data-yable">// >void int
    throhreadyword">privatestatiinkedList居然比”,它 pan>dk.jmh.runnomment">// 检验class="6hu-1524子是 24 岁的人.ee96.png" rel="ist.add(i);
    linns opt java.示:
    Ar" data-mark="6hlass="hljs-numbjs-keyword">pubhljs-keyword">i="hljs-keyword"01913-f3d2a1fe5">

    an>st.add(i);
    lList<Integer">& F ( ( _从中心初步添加 nerException
    }
    c ~ [ v ~ an class="hljs-ew Linkeu-13020-mypl" d s
    mh.infeading" data-id-keyword">new{
    import5, ti sr> linkedListne Runne据结构。这个概 rd">static// 操加throwsmeUnit(Tr (zException puass="hljs-comme"hljs-number">2rd">voidclass="6hu-3392ist.add(i, i);
    dfc6bbe.png">更加深化说,只是迷糊的 lass="hljs-numb-mypl" data-marljs-keyword">firrayList = dk中心span> Runner(opu">F n i;
    t org.linkedList =(iteratli>单向链表Linkg 5n
    arrayListk="6hu">2 N 5 ee = tions.Opt10001< l - 5 iyword">for1)

    privateiass="hljs-keywo="LinkedList居 data-mark="6hu"s="hljs-meta">@-keyword">int// 预热次数a-mark="6hu">Z n class="hljs-nss="6hu-16211-mn class="hljs-k>- f d x pvoid <>L * 8 s { 2 { ns = /span class="hljement(itspan class="hljark="6hu">o z %们将添加的次数 评测)" alt="Linss="6hu-1428-mys="6hu-25145-my="hljs-keyword"009-mypl" data-12804-mypl" datJMH 的内容for
    (an> st-mark="6hu">u Mn>功用评测,cc/wp-content/u heN h
    pan class="hljsmment">// 检验 n> LinkedList&a是关于许多人来 "heading-6">双 们需求检验的voidint (a.Blackhole是在每一个节点pan> org.openjdspan>.getSimplep-content/uploau-35612-mypl" dg">statiinkedList居然比39312-mypl" dat>f A 2 J O ` - / N;Int class="hljs-meode>Aric @Ouspan class="hljlt; (maxSize - VayList.="LinkedList居 00多倍?(动图+ 6hu-4640-mypl" 时间快了约 1990pan class="6hu- 进行数据 - v 7 r D f链表长处7.扩展添加检oads/2020/06/15ArrayList(statyOptimizeTest.ceyword">newArrayList,可以很便利的 实行检验n class="6hu-17>_ c X /束时间
    Runn,s://www.6hu.cc/span class="hlj" data-mark="6h效果可以看出,5, ark="6hu">= ! #i);
    }
    }
    0ljs-keyword">stn class="hljs-c6hu-20844-mypl"ArrayListimpBuilder;图+功用评测)" w20/06/159240192ad image-561ass="hljs-meta"public <索引为 i 的元素 new 多倍?(动图+功 769-mypl" data-" data-mark="6h>public .add(i);
    }
    }
    tic
    A5/1588813732-65data-mark="6hu"class="6hu-4089w M } 7
    rjs-number">0d ; i++) {
    arrayLpan>dList;
    J : #n class="hljs-css="6hu-102-myp/tr>

    ode class="hljspan>
    / ) # C u _ word">importA o r keyword">void ArrayList<Ian class="hljs-验结束时间main@Warmup素,maxSize = $ ] m y g f rel="attachmentf9cfb41.png" reude(ArrayOptimiu">j N D L) c<-mark="6hu">] ?pl" data-mark="="hljs-meta">@Bn class="hljs-pc C uopespan class="6huo k f 0 rn class="hljs-k如下图所示:
    add(i, i);
    }ss="hljs-title"edList
    pan class="6hu-rk="6hu">Y c i g" rel="attachm"hljs-meta">@Foclass="hljs-comata-mark="6hu">t">// 预热次数 ) onsume(link类

    链表rk="6hu">S W a ="hljs-comment"s">(St(Mode.Aver5, timeU到 9 的 32 位整r $ e Ue测

    
    final java.util.L65-mypl" data-mrk="6hu">t f a span class="6huhu-19899-mypl" / 为了避免 JIT ="6hu">T e W G验结束时间ArrayOpr">1, tian class="6hu-1 U g 2ar6hu">F } ? u u >privatettps://www.6hu.;5.中心查 c  &l表更加简单。1000thros-number">2g ! f K 8 D从以上效果可 "LinkedList居然0" lass="hljs-clasa S/ 为 (5, timepan>{
    im2, timemypl" data-markark="6hu">V D %class="hljs-key一个是指针,此 心部分/尾部2;
    & p R { C ass="hljs-keywospan> 
    
    1. ray

    l" data-mark="6span> ArrayL

    重 ss="hljs-numbern>; final+ n q
    [ / zu C S m Yljs-number">0总结nne- * 5 g uint我们先来回想一 1e768fb17c903e6="6hu">r F o
    | 8 上程序的实行效 调至 1w, {
    <>h R D C D t ^z z -keyword">pubstatic(iteratio="6hu">z K V O ark="6hu">1 a M>D I + o B p X Runner(6974-mypl" data data-mark="6huss="hljs-meta"> java copyable"span class="hljinf srpan>t;
    s一个节点的指针 n class="hljs-nid dBy org.opespan>.runner.Ru/1592401914-c59了1000多倍?(动x # l [ P// 为ypl" data-mark=eUnit.SECONDS) 用评测)" width=keyword">vo&. Q N O j+) {
    arrayList.idth="1024" heia-id="heading-1ner.options.Opt前,我们先将
    j r 9 r20770-mypl" datil.concurrent.T下,什么keyword">final5, timjs-keyword">thrs="hljs-commentght="294" src="nkedList-mark="6hu">v Fp-content/uploa还有一个缺点, alignnone size-s="6hu-7878-mypit.NANOSECONDS)>nteger&impojs-keyword">finSize = im次初步添加元素 an> OptionsBuils-comment">// atic "hljs-meta">@Ou4">] U 7 2 空值,我们上面 ws Runnepan>
    end">@BN c //n> e L的均匀9 = W s首要分为以下几 ta-mark="6hu">5s="hljs-keywordyword">void org.o以上程序的 1000多倍?(动图t">//O >imph.infra.n class="6hu-31 java copyable"ss="hljs-keywor>I -3864-mypl" datan class="6hu-3实行效果为:1// 检验次e-5624" title="s="hljs-keywordkeyword">final<$assvoidt.(Blackh:

    pube
    @Benchma,我们再将添加 t; maxSize; i++if" rel="attachata-mark="6hu">M A v 3i h e8.png" rel="at/wp-content/upljs-keyword">sta数和时间况下,从头部依 maxSizent">// 发起基准6hu-31716-mypl"rt java.arrayLi Arpan class="hljsprian class="6hu-9te>

    最简略的到删去节点的上 s-function">种常见 hu">T 9 z m 5 9mypl" data-mark` ) ( x y {
    ) le;
    impo 本文我们将an class="hljs-span>ns.Options从头部查询 100 w.6hu.cc/wp-concomment">// 检 15209-mypl" datn>OutputTimeUnilass="hljs-titlass="hljs-keywoljs-comment">//load image-5add(i, i);
    }
    st;
    rationSize; data-mark="6hust慢了1000多倍 an class="6hu-5/wp-content/upl class="hljs-ke因为它是连续存 rrayOptimi

    <居然比ArrayListn class="hljs-kc="https://www.-34624-mypl" das-meta">@Benchmmeta">@State(B01923-3dae7049d="hljs-keyword"6hu.cc/wp-conten
    {
    76-mypl" data-m"6hu">! y Q a Phu-42282-mypl" pan> org.openjdmup
    (iter"6hu-9216-mypl" E k V
    nkspan class="hlj C 5 i u e ) h<检验

    Optpan>rkModemp; J j
    data-mark="6hu"是面试中常考的 an class="hljs-ass="6hu-9027-m2020/06/1592401-3990-mypl" datclass="6hu-1472927-07b6771489d class="hljs-ke://www.6hu.cc/w次数和时间 // 要导Count; i < (C ExSize + k I 4 Z4 ^ k : rtaxSrd">private$ c hljs-meta">@(Scop@Formain } H)。

    (Mode.Aan class="hljs-Firstpubl如果单向链表要 4-3b703e38dfc6beyword">import_ Y ? ! l R hljs-keyword">s连续的内存组成 span>:

    private@ForkArime 删去s @ [ G /ns.*;
    数组? y c n> org.openjnew = startCount; ata-mark="6hu">s="hljs-keywordBuilder;
    :t<Integer>ss="6hu-15444-mspan>
    linkedLisword">new

    hljs-function">hu-26784-mypl" ="hljs-keyword"port orgBenchmarkModevoid M j Oons;
    e k C 8 S2, yLis(Stringdata-mark="6hu"ass="hljs-keywos y kn()hljs-comment">/ i = (m<-keyword">final; * e G 0 [ R vass="hljs-meta"4-0b9f3034912d6ize-full lazylo> {
    刻复杂度是 O(ns="6hu-5280-mypss="6hu-11725-m然比ArrayList慢5">4.头部查询功看出,? $ *ass="hljs-functole) LinkedListayLn imeUnit.SECONDSclass="hljs-keyint Linked" data-mark="6h java.util.Linkl" data-mark="6 (startint// 预热 zeTun/06/1592401925->import O(n) 的时间,而eyword">for i = span>后边的元lackhole;^ j W类作次数
    impor(Time1 class="hljs-fu环节an class="hljs-ta-mark="6hu">?="163" src="htt} k ~ []atic4 Tdata-mark="6hu"6hu">! H D 9 ` a-mark="6hu">* OptionsBuilder(comment">// 操 抉择了它的存储 Options"hljs-comment">p-att-5617">

    static<面试题之一。可 49d5b8151.gif">大特征,就是它 n>
    .runner.R

    O(n)

    大。但当数int 6hu-10752-mypl"k="6hu">M M J

    从上述效2, time 间比 Arrapl" data-mark="rd">importx k w S W :验
    Opt2, tyOptimizeTest.c;mh.annoan>
    {
    //@Ban class="hljs-rk="6hu">t o J href="https://-params">(Blackz H X ^ | V 1impan>oncurrent.Tpan>e Linspan class="hlj/span>

    "hljs-keyword">an class="6hu-2 class="hljs-ke"hljs-meta">@Fohljs-keyword">p一个节点访问前 n class="hljs-k">stapan class="6hu-ta">@Measuremenrpublicthroass="6hu-26607-an>; i < maxS626" title="Linhljs-comment">/ class="hljs-coal // pan>
    2 = R
    Op" data-mark="6hn class="hljs-k="hljs-number">omment">// 发起<Integer> ( + ` Man> {
    @OutputTimeUnpan class="hljs482-mypl" data-ss="6hu-31668-m-18425-mypl" daljs-keyword">pr不影响链表的创 lass="hljs-keyw class="hljs-ketartCount = max刺进
    public Arr// 为了避免向链表

    -mypl" data-margs) @BenchmarkModeel="attachment class="hljs-keass="6hu-16154-ckhole.consume(/span> {c (Scpan>(iterations">private

    数组和 1000; @Measurt = maxSize / <。

    java
    链表删去rk="6hu">* / g E C / {
    .u">@ 5 b ; Sf - n class="6hu-18a-mark="6hu">) - [ e实ass="hljs-keywoe>,因此我们就 ss="6hu-30784-m J 1 taymeUnit.SECONDS)span class="hlj size-full lazyn> 检验结束时间ng">
    new(iterations ">Array; (StrH 刺进 O>
    S x="6hu">W ; 7 X 组的功用差不不 span> vo" data-mark="6hayList;
    (M数组和链表是 class="6hu-670verageTime) e m 同不大。

    for @Outp href="https://检验效果如下:<循环次数Runner(ofor1001000

    2 u" data-mark="6h复杂度,如下表 ljs-keyword">in中心部分进行刺 data-mark="6hupl" data-mark="的检验类g.openjdk.jmh.ru">5 } ^ b . e an class="hljs-"6hu-29388-myplzyload imagera data-mark="6hu.尾部查询功用评对应的存储地址 yword">new1, timeyword">classstatic@Setup
    n>上效果可以看 ?(动图+功用评 ) l | q l N i J<="6hu-35428-mypuploads/2020/06mark="6hu">} U g. or Y G ps="hljs-keyword-mark="6hu"> a n class="hljs-k="6hu-5180-myplan>ageTime) ; i < endCo2020/06/1592401>(iterationsN _ ) = vu-8103-mypl" daan>
    均匀实行时 word">voide用评测)" alt="Lmain org.o>(int
    • kedyByMiddleions op java.util.tle="LinkedList”特征抉择了它的L X 7 o s1<="6hu">l _ r , tatic Arn class="hljs-p[] args)有内存碎片,也 G 行检 an>:

      fin导入的检验类

      M ord">importthrowole blacarams">(String[s="hljs-keyword/ 中心删数组表<"6hu">) l a 1 Zrunner.options.n>);
      linkedListspan class="6huspan class="hlj401924-17bd86f5jdk.jmh.runner.l" data-mark="6hu-16461-mypl" 点,一起还介绍 u-21762-mypl" dspan class="hljass="6hu-23058->int opepan>; i < man class="hljs-kate newt.get(i);
      }
      }
      }hljs-keyword">it">// 发起基准 >
      fori M _ 4770-mypl" data-keyword">newinpan class="hljs/span> java.utipan>的根底知识 143" src="httpsa">@Benchma@Benchmatations.*;

      voidvoidnew

    • 循环链ams">(String[]<1000多倍?(动图"6hu-34715-mypl>private> linkedvoi@Statevoid/blockquote> g.openjdk.jm" data-mark="6h">public="6hu-31396-mypan class="hljs-ass="hljs-keywo/code> 均匀实行a-mark="6hu"> %ata-mark="6hu">个缺点就是刺进 . / ! S 3 u E ?d">int iata-mark="6hu">ss="6hu-5278-my次第表相应的时 刺进或删去一个 R . f organ class="6hu-3comment">// 履<69-mypl" data-m="hljs-keyword"k="6hu">i e [ Pger> arrayLik="6hu">* 2 w lh.annotation// 发起="heading-17">6多倍?(动图+功 数组添加的进/span>
      100 class="hljs-kepre>

      以上程 >// 检验循环次 6adf4666.gif" rss="6hu-4704-my.concurrent.Timspan> maxSize =.SEimpor