Unity C#for和foreach效率比较
2024-09-08 19:26:27
下面是代码自己测试一下即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class TimeDuiBi : MonoBehaviour
{
List<int> list = new List<int>(); private void Start()
{
for (int i = 0; i < 3000000; i++)
{
list.Add(i);
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
int temp1 = 0;
int cout = list.Count;
for (int i = 0; i < cout; i++)
{
temp1 += list[i];
}
stopwatch.Stop();
Debug.Log("for time=: " + stopwatch.Elapsed.TotalMilliseconds + "temp1:" + temp1); System.Diagnostics.Stopwatch stopwatch2 = new System.Diagnostics.Stopwatch();
stopwatch2.Start();
int temp3 = 0;
for (int i = 0; i < list.Count; i++)
{
temp3 += list[i];
}
stopwatch2.Stop();
Debug.Log("for list.Count time=: " + stopwatch2.Elapsed.TotalMilliseconds + "temp3:" + temp3); System.Diagnostics.Stopwatch stopwatch1 = new System.Diagnostics.Stopwatch();
stopwatch1.Start();
int temp2 = 0;
foreach (var item in list)
{
temp2 += item;
}
stopwatch1.Stop();
Debug.Log("foreach time=: " + stopwatch1.Elapsed.TotalMilliseconds + "temp2:" + temp2);
}
}
}
以上就是测试的结果,不喜勿喷
搜索
复制
最新文章
- 在Web中实现C/S模式的Tab
- sql语句not in判断条件注意事项
- 430flash的操作
- ubuntu glusterfs 配置调试
- Modelsim的自动化脚本仿真平台
- 无法将类型“System.Collections.Generic.IEnumerable<;EmailSystem.Model.TemplateInfo>;”隐式转换为“System.Collections.Generic.List<;EmailSystem.Model.TemplateInf
- uva 10041 Vito&#39;s Family_贪心
- 基于visual Studio2013解决算法导论之019栈实现(基于数组)
- C#大牛应该知道的一些知识
- 计算机网络分层(OSI七层、 TCP/IP四层)
- Coursera 机器学习笔记(四)
- php5.6在yum下安装gd库
- 使用Anaconda搭建TensorFlow-GPU环境
- MyCat-schema.xml详解
- centos7配置iscsi
- 【NLP】Attention Model(注意力模型)学习总结
- 使用nexus搭建maven私服教程详解
- E - TOYS
- js如何判断访问来源是来自搜索引擎(蜘蛛人)还是直接访问
- Sql server 备份还原后出现“受限制用户”问题
热门文章
- 案例:用ajax 方法 解析xml
- vue 中引入iframe,动态设置其src,遇到的一些小问题总结
- epoll分布式通讯
- 【分析笔记】全志平台 gpio_wdt 驱动应用和 stack crash 解决
- vulnhub靶场之VENOM: 1
- 【转载】JAVA - 解决:Java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
- Idea移除和删除模块
- 什么是push通知栏消息?
- fast planner总结
- python3使用requests模块发https请求,提示caused by ssl error, can&#39;t connect to https url because the ssl module is not available