阵列与列表性能问题 [英] array vs.list performance issue

查看:42
本文介绍了阵列与列表性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
阵列与列表的性能

我想知道哪种方法是执行此任务的更好方法.

I want to know which one is better way to do this task.

string[] week = new string[7]
  week[0] = "Sunday";
  week[1] = "Monday";
  week[2] = "Tuesday";

  foreach (string day in week)
   {
     //Some task
   }


List<string> week = new List<string>();
 list.Add("Sunday");
 list.Add("Monday");
 list.Add("Tuesday");

 foreach (string day in list) 
 {
    //Some Task
 }

是否存在性能问题?或其他更好的方法.

Is there any performance issue?Or any other better way.Thanks.

推荐答案

第一个可能会做得更好,但效果会稍微好一些.原因是,即使该List后面有一个数组,该列表上的迭代也必须经过更多层方法调用才能获取值,而该数组几乎是 直接内存寻址.差异将非常小,以至于您必须迭代数千次才能对其进行测量.这就是所谓的微观优化,这通常被认为是浪费精力.

The first one will probably perform better, but only ever so slightly. The reason is that even though there is an array behind that List, the iteration over the list has to go through a few more layers of method calls to get the values, whereas the array is almost direct memory addressing. The difference will be so small that you would have to iterate thousands of times to measure it. This is what is called micro-optimization, and it is generally considered a waste of effort.

这篇关于阵列与列表性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆