C# For、Foreach性能比较
创始人
2025-05-28 16:38:28
0

一直听说foreach会有gc产生,有说.net3.5之前有,.net4则修复,一直没有测试,今天测试记录一下

测试在Update中进行:

  1. foreach遍历字典有GC,不过只有第一次有。例如:在update中,两句代码都用foreach遍历同一个字典,则只有第一个foreach有gc产生。

//第一种方法
foreach (var item in peopleDictionary)
{
}
//第二种方法
foreach (KeyValuePair keyValuePair in peopleDictionary)
{}
  1. for循环遍历字典,同样的,如果两句代码遍历同一个字典。也只有第一次有GC产生。可以使用下面代码。

IEnumerator ee = peopleDictionary.GetEnumerator();
ee.MoveNext();
for (int i = 0; i < peopleDictionary.Count; ee.MoveNext(), i++)
{
}
  1. 使用foreach循环遍历其中的key或values,则会分别产生额外GC,同样只只产生一次。如下面代码

foreach (var item in peopleDictionary.Values)
{
}
foreach (var item in peopleDictionary.Keys)
{
}
  1. 再来一个,每帧都有GC产生的方法,哈哈。

//每帧都有GC产生
IEnumerator ee = peopleDictionary.GetEnumerator();
ee.MoveNext();
for (int i = 0; i < peopleDictionary.Count; ee.MoveNext(), i++)
{
}
//只有一次有GC产生,关键字var
var ee = peopleDictionary.GetEnumerator();
ee.MoveNext();
for (int i = 0; i < peopleDictionary.Count; ee.MoveNext(), i++)
{
}
  1. GC产生的大小,与字典存的数据结构有关,存int的GC 就小于存Class GC。

总结:

  1. 遍历List、Array。for循环、 foreach循环、while循环都没有GC。

  1. 遍历Dictionary。for循环、 foreach循环,第一次都会产生GC。只有一次也可以放心使用。

最后来一段测试代码,大家可以自行测试

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Profiling;
//using System.Collections.Specialized;public class People
{public string Name;public int Age;
}public class Test : MonoBehaviour
{int[] tempsInts = new[] { 1, 3, 4, 5, 6 };private People[] peoples;private List peoplesList;private List intList;private Dictionary peopleDictionary;private Dictionary intDictionary;private int tem;// Use this for initializationvoid Awake(){peoples = new People[5];peoplesList = new List();intList = new List();peopleDictionary = new Dictionary();intDictionary = new Dictionary();for (int i = 0; i < 5; i++){People people = new People();people.Age = i;people.Name = ((char)'A' + i).ToString();peoples[i] = people;peoplesList.Add(people);peopleDictionary.Add(i, people);intDictionary.Add(i, i);intList.Add(i);}}void Update(){Profiler.BeginSample("Foreach Int Dictionary  PeoplePeoplePeople");foreach (KeyValuePair keyValuePair in peopleDictionary){}Profiler.EndSample();Profiler.BeginSample("Foreach Int Dictionary-2-2-2");foreach (KeyValuePair keyValuePair in peopleDictionary){}Profiler.EndSample();Profiler.BeginSample("Foreach Int Dictionary-5-5-5");foreach (var item in peopleDictionary){}Profiler.EndSample();Profiler.BeginSample("For Int Dictionary1111----");var e = peopleDictionary.GetEnumerator();e.MoveNext();for (int i = 0; i < peopleDictionary.Count; e.MoveNext(), i++){}Profiler.EndSample();Profiler.BeginSample("For Int Dictionary2222----");IEnumerator ee = peopleDictionary.GetEnumerator();ee.MoveNext();for (int i = 0; i < peopleDictionary.Count; ee.MoveNext(), i++){}Profiler.EndSample();Profiler.BeginSample("Foreach Int Dictionary-1-1-1");foreach (var item in peopleDictionary.Keys){}Profiler.EndSample();Profiler.BeginSample("Foreach Int Dictionary000----");foreach (var item in peopleDictionary.Values){}Profiler.EndSample();Profiler.BeginSample("Foreach Int Dictionary999----");foreach (People item in peopleDictionary.Values){}Profiler.EndSample();}
}

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...