技术频道导航
HTML/CSS
.NET技术
IIS技术
PHP技术
Js/JQuery
Photoshop
Fireworks
服务器技术
操作系统
网站运营

赞助商

分类目录

赞助商

最新文章

搜索

实例显示C# for、foreach和while循环的速度比较

作者:admin    时间:2023-6-7 12:54:6    浏览:

经常有人问“哪个循环更快?”,“哪个循环性能更高?” 等,我听说 for 循环比 foreach 循环快,但我以前从未测试过。因此,我决定看看所有循环之间的差异有多大,以及在速度和性能方面哪个在数组和列表上更快。

创建跟踪循环的类

我要找出最好的 C# 循环,为此,我将在 Visual Studio 2017 中创建一个控制台应用程序。

首先,我将创建一个名为“CustomStopwatch”的类,它有助于跟踪循环的开始和结束时间。

using System;  
using System.Diagnostics;  
  
namespace ConsoleApp2  
{  
    public class CustomStopwatch : Stopwatch  
    {  
  
        public DateTime? StartAt { get; private set; }  
        public DateTime? EndAt { get; private set; }  
  
  
        public void Start()  
        {  
            StartAt = DateTime.Now;  
  
            base.Start();  
        }  
  
        public void Stop()  
        {  
            EndAt = DateTime.Now;  
  
            base.Stop();  
        }  
  
        public void Reset()  
        {  
            StartAt = null;  
            EndAt = null;  
  
            base.Reset();  
        }  
  
        public void Restart()  
        {  
            StartAt = DateTime.Now;  
            EndAt = null;  
  
            base.Restart();  
        }  
  
    }  

创建一个名为“CustomStopwatch”的类

现在,在 Program.cs 类中编写代码。

for循环

首先,我要实现一个 for 循环:

  • 单整数(Single integer)
  • 数组(Array)
  • 列举(List)
using System;  
using System.Linq;  
  
namespace ConsoleApp2  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            CustomStopwatch sw = new CustomStopwatch();  
            sw.Start();  
            for (int i = 0; i < 10000; i++) Console.WriteLine(i);  
            sw.Stop();  
  
            CustomStopwatch sw1 = new CustomStopwatch();  
            int[] array = Enumerable.Range(0, 10000).ToArray();  
            sw1.Start();  
            for (int i = 0; i < array.Length; i++) Console.WriteLine(array[i]);  
            sw1.Stop();  
  
            CustomStopwatch sw2 = new CustomStopwatch();  
            var arrList = array.ToList();  
            sw2.Start();  
            for (int i = 0; i < arrList.Count; i++) Console.WriteLine(arrList[i]);  
            sw2.Stop();  
  
            Console.WriteLine($"for Time elapsed: {sw.ElapsedMilliseconds} Milliseconds, StartAt: {sw.StartAt.Value}, EndAt: {sw.EndAt.Value}");  
            Console.WriteLine($"for on array Time elapsed: {sw1.ElapsedMilliseconds} Milliseconds, StartAt: {sw1.StartAt.Value}, EndAt: {sw1.EndAt.Value}");  
            Console.WriteLine($"for on list Time elapsed: {sw2.ElapsedMilliseconds} Milliseconds, StartAt: {sw2.StartAt.Value}, EndAt: {sw2.EndAt.Value}");  
            Console.ReadKey();  
        }  
  
    }  
}  

for循环
点击图片放大

运行脚本以查看输出和执行时间(以毫秒为单位)。

 for循环执行时间

根据输出,列举(List)中的 for 循环更快。

foreach循环

让我们比较列举(List)和数组(Array)上的 foreach 循环。

using System;  
using System.Linq;  
  
namespace ConsoleApp2  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            int[] array = Enumerable.Range(0, 10000).ToArray();  
            var arrList = array.ToList();  
  
            CustomStopwatch sw1 = new CustomStopwatch();  
            sw1.Start();  
            foreach (var arr in array) Console.WriteLine(arr);  
            sw1.Stop();  
  
            CustomStopwatch sw2 = new CustomStopwatch();  
            sw2.Start();  
            foreach (var arr in arrList) Console.WriteLine(arr);  
            sw2.Stop();  
  
            Console.Clear();  
            Console.WriteLine($"for each on array Time elapsed: {sw1.ElapsedMilliseconds} Milliseconds, StartAt: {sw1.StartAt.Value}, EndAt: {sw1.EndAt.Value}");  
            Console.WriteLine($"for each on list Time elapsed: {sw2.ElapsedMilliseconds} Milliseconds, StartAt: {sw2.StartAt.Value}, EndAt: {sw2.EndAt.Value}");  
            Console.ReadKey();  
        }  
    }  
}  

 列举(List)和数组(Array)上的 foreach 循环
点击图片放大

运行以毫秒为单位查看输出和执行时间。

 foreach执行时间

在这里,列举(List)中的 foreach 循环更快。

while循环

我们再比较列举(List)和数组(Array)上的 while 循环。

using System;  
using System.Linq;  
  
namespace ConsoleApp2  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            int[] array = Enumerable.Range(0, 10000).ToArray();  
            var arrList = array.ToList();  
  
            CustomStopwatch sw1 = new CustomStopwatch();  
            sw1.Start();  
            int i = 0;  
            while (i != array.Length) Console.WriteLine(array[i++]);  
            sw1.Stop();  
  
            CustomStopwatch sw2 = new CustomStopwatch();  
            sw2.Start();  
            i = 0;  
            while (i != arrList.Count) Console.WriteLine(arrList[i++]);  
            sw2.Stop();  
  
            Console.Clear();  
            Console.WriteLine($"while on array Time elapsed: {sw1.ElapsedMilliseconds} Milliseconds, StartAt: {sw1.StartAt.Value}, EndAt: {sw1.EndAt.Value}");  
            Console.WriteLine($"while on list Time elapsed: {sw2.ElapsedMilliseconds} Milliseconds, StartAt: {sw2.StartAt.Value}, EndAt: {sw2.EndAt.Value}");  
            Console.ReadKey();  
        }  
    }  

 while循环
点击图片放大

while 循环的输出如下。

 while循环执行结果

列举(List)上的while循环更快。

对比for、foreach和while速度

最后我们对比一下forforeachwhile循环的执行时间。

  数组(Array) 列举(List)
for 663 ms 472 ms
foreach 3841 ms 498 ms
while 3950 ms 460 ms

可以看到,在数组里,for的速度是最快的,比其他的循环方法快了5倍那么多。但在列举里,各种循环方法的速度几乎无差异。

结论

本文涵盖数组(Array)和列举(List)上的 forforeachwhile 循环的速度比较。根据测试结果(在迭代方面),列举(List)上的循环速度更快。我认为这取决于数据和你使用数据的方式。我个人的建议是,出于以下原因,我们应该使用列表而不是数组。

  • 在数组中,我们需要知道数组大小,我们不能更改数组大小,列举在创建后可以增长。
  • 在列举中,我们不需要担心列举大小或索引越界异常。
  • 列举提供了许多有用的方法,如添加、查找等。
  • 易于阅读。

相关文章

标签: for  foreach  while  c-sharp循环  
x
  • 站长推荐
/* 左侧显示文章内容目录 */