第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在不使用額外內(nèi)存的情況下連接列表?

如何在不使用額外內(nèi)存的情況下連接列表?

C#
回首憶惘然 2022-07-10 10:34:03
你如何在不加倍內(nèi)存的情況下連接巨大的列表?考慮以下代碼段: Console.WriteLine($"Initial memory size: {Process.GetCurrentProcess().WorkingSet64 /1024 /1024} MB"); int[] a = Enumerable.Range(0, 1000 * 1024 * 1024 / 4).ToArray(); int[] b = Enumerable.Range(0, 1000 * 1024 * 1024 / 4).ToArray(); Console.WriteLine($"Memory size after lists initialization: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB"); List<int> concat = new List<int>(); concat.AddRange(a.Skip(500 * 1024 * 1024 / 4)); concat.AddRange(b.Skip(500 * 1024 * 1024 / 4)); Console.WriteLine($"Memory size after lists concatenation: {Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024} MB");輸出是:Initial memory size: 12 MBMemory size after lists initialization: 2014 MBMemory size after lists concatenation: 4039 MB我想在連接后將內(nèi)存使用量保持在 2014 MB,而不修改 a 和 b。
查看完整描述

3 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

如果你需要一個(gè)List<int>,你不能這樣做。AList<int>總是直接包含它的數(shù)據(jù),所以當(dāng)你有兩個(gè)(比如說(shuō))100 個(gè)元素的數(shù)組和一個(gè)通過(guò)連接這兩個(gè)元素創(chuàng)建的列表時(shí),你已經(jīng)有了 400 個(gè)獨(dú)立元素。你無(wú)法改變這一點(diǎn)。

您正在尋找的是一種創(chuàng)建數(shù)據(jù)的獨(dú)立副本的方法。如果您只是在搜索它(就像評(píng)論中的聲音一樣),您可以使用使用IEnumerable<int>LINQ 創(chuàng)建的:

IEnumerable<int> concat = a.Concat(b);

如果您需要類似 anIReadOnlyList<T>甚至 an 的東西IList<T>,您可以自己實(shí)現(xiàn)這些接口以在多個(gè)數(shù)組上創(chuàng)建適配器 - 但您可能需要自己編寫(xiě)。如果你能堅(jiān)持IEnumerable<T>使用 LINQ,使用 LINQ 會(huì)簡(jiǎn)單很多。


查看完整回答
反對(duì) 回復(fù) 2022-07-10
?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

我可以建議您進(jìn)行一些優(yōu)化:


IEnumerable<int>在不調(diào)用 ToArray() 方法的情況下初始化 a 和 b


int size = 1000 * 1024 * 1024 / 4;

IEnumerable<int> a = Enumerable.Range(0, size);

IEnumerable<int> b = Enumerable.Range(0, size);

用已知容量初始化 concat


List<int> concat = new List<int>(size);

結(jié)果我得到以下輸出:


Initial memory size: 12 MB

Memory size after lists initialization: 13 MB

Memory size after lists concatenation: 1021 MB

如果您只想串聯(lián)搜索某些內(nèi)容,則可以這樣做而無(wú)需額外分配:


IEnumerable<int> concat = a.Skip(500 * 1024 * 1024 / 4).Concat(b.Skip(500 * 1024 * 1024 / 4));

int search = concat.Count(i => i % 2 == 0);

Console.WriteLine($"Search result: {search}");


查看完整回答
反對(duì) 回復(fù) 2022-07-10
?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

他們是執(zhí)著的。我只需要連接它們,進(jìn)行一些搜索,然后處理連接列表


如果您只需要進(jìn)行一些搜索,為什么首先需要連接?分別搜索兩個(gè)數(shù)組。


您正在搜索的內(nèi)容可能會(huì)橋接兩個(gè)數(shù)組。如果是這種情況,為了讓事情變得更容易并且不支付內(nèi)存價(jià)格,只需實(shí)現(xiàn)一個(gè)模擬操作但實(shí)際上不執(zhí)行它的包裝器:


sealed class Concatenated<T>:

    IReadOnlyList<T>

{

    public static Concatenated<T> 

        Concatenate<T>(

            IReadOnlyList<T> first,

            IReadOnlyList<T> second)

        => new ConcatenatedArray<T>(first, second);


    private readonly IReadOnlyList<T>

       first, second;


    private Concatenated(

        IReadOnlyList<T> first,

        IReadOnlyList<T> second)

    {

        this.first = first;

        this.second = second;

    }


    public T this[int index] 

        => index < first.Length ? 

           first[index]: 

           second[index - first.Length];


    public int Count => first.Length + second.Length;


    public IEnumerator<T> GetEnumerator()

    {

        foreach (var f in first)

            yield return f;


        foreach (var s in second)

            yield return s;

    }


    IEnumerator IEnumerable.GetEnumerator()

        => GetEnumerator();

}


查看完整回答
反對(duì) 回復(fù) 2022-07-10
  • 3 回答
  • 0 關(guān)注
  • 128 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)