gpt4 book ai didi

c# - IEnumerable 的性能

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:53 26 4
gpt4 key购买 nike

<分区>

我有这个代码

List<int> items = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
//calculation is not being executed until "even" is used
IEnumerable<int> even = items.Where(x => x % 2 == 0);

DoStuff1(even);
DoStuff2(even);
DoStuff3(even);

我读过这个答案https://stackoverflow.com/a/3628705/6887468它说

When you use IEnumerable, you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList() you force the compiler to reify the results right away.

现在这个计算(在我的示例中 x % 2 == 0)是在每次调用 DoStuff() 时执行还是以某种方式保存在内存中?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com