List<int> list = new List<int>();
            List<int> list2 = new List<int>();
            string x;
            for (int i = 0; i < 100; i++)
            {
                list.Add(i);
            }
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < list.Count; i++)
            {
                list2.Add(new Random().Next(list[i]));
                for (int j = 0; j < list2.Count; j++)
                {
                    Debug.WriteLine(list2[i]);
                }
            }
            watch.Stop();
            x = watch.Elapsed.ToString();            list2.Clear();            watch.Start();
            foreach (int i in list)
            {
                list2.Add(new Random().Next(i));
                foreach (int j in list2)
                {
                    Debug.WriteLine(j);
                }
            }
            watch.Stop();            MessageBox.Show("for用时:" + x + "\nforeach用时:" + watch.Elapsed.ToString());无论我怎么测试,始终都是for的效率高将近一半。
除了更明晰的循环之外,Effective为什么会推荐选择?

解决方案 »

  1.   

    【问2】
    [align=center]为什么list的效率比array高?[/align]
      

  2.   

    我试了500和1000,还是for用的时间少一半。。
    期待高手
      

  3.   

    抱歉,我的是实体书。我建议你购买一本。我这本已经有2年历史了。今天我温习ing...
      

  4.   

    恩没错。我也尝试在循环里不断存取。最终还是for不可避免的战胜。
    那么,原因在何?还是因为占用内存的关系?
      

  5.   

    首先, 你的 StopWatch 是使用有问题, StopWatch.Stop 是不会清空及时的, 所以摆在后面的一定比前面的慢, 修改一下你的代码
    其次, 你少考虑了 GC 的存在, 后面的测试项目运行的时候, 无可避免地受到 GC 干扰
                List<int> list = new List<int>();
                List<int> list2 = new List<int>();            string x;
                for (int i = 0; i < 100; i++)
                {
                    list.Add(i);
                }
                Stopwatch watch1 = new Stopwatch();
                watch1.Start();
                for (int i = 0; i < list.Count; i++)
                {
                    list2.Add(new Random().Next(list[i]));
                    for (int j = 0; j < list2.Count; j++)
                    {
                        Debug.WriteLine(list2[i]);
                    }
                }
                watch1.Stop();
                x = watch1.Elapsed.ToString();            list2.Clear();            Stopwatch watch2 = new Stopwatch();            watch2.Start();
                foreach (int i in list)
                {
                    list2.Add(new Random().Next(i));
                    foreach (int j in list2)
                    {
                        Debug.WriteLine(j);
                    }
                }
                watch2.Stop();
                Console.WriteLine(String.Format("For:{0}, foreach:{1}", watch1.Elapsed.ToString(), watch2.Elapsed.ToString()));            Console.Read();
    运行结果: for 5.42 ; foreach: 6.33如果颠倒一下顺序, 让 foreac 摆在前面 运行结果: for 5.70 ; foreach: 0.0000033, foreach 的优势极度明显测试环境: Athlon 64 X2 Dual, 3G 内存
      

  6.   

    To CGabriel        static void Main(string[] args)
            {
                for (int a = 0; a < 10; a++)
                {
                    List<int> list = new List<int>();
                    List<int> list2 = new List<int>();                string x;
                    for (int i = 0; i < 100; i++)
                    {
                        list.Add(i);
                    }
                    Stopwatch watch1 = new Stopwatch();
                    watch1.Start();
                    for (int i = 0; i < list.Count; i++)
                    {
                        list2.Add(new Random().Next(list[i]));
                        for (int j = 0; j < list2.Count; j++)
                        {
                            Debug.WriteLine(list2[i]);
                        }
                    }
                    watch1.Stop();
                    x = watch1.Elapsed.ToString();                list2.Clear();                Stopwatch watch2 = new Stopwatch();                watch2.Start();
                    foreach (int i in list)
                    {
                        list2.Add(new Random().Next(i));
                        foreach (int j in list2)
                        {
                            Debug.WriteLine(j);
                        }
                    }
                    watch2.Stop();
                    Console.WriteLine(String.Format("For    :{0}\r\nForeach:{1}\r\n\r\n", watch1.Elapsed.ToString(), watch2.Elapsed.ToString()));
                }
                Console.Read();
            }
    我加了个循环,得到了个意外的结果。比如我加粗的地方。第一次测试
    For    :00:00:02.5987313
    Foreach:00:00:02.8021921

    For    :00:00:02.9548989
    Foreach:00:00:01.3903855

    For    :00:00:01.5541060
    Foreach:00:00:01.7608133
    For    :00:00:02.0111915
    Foreach:00:00:02.2695184
    For    :00:00:02.5334541
    Foreach:00:00:02.7055843
    For    :00:00:02.9683019
    Foreach:00:00:03.3733007
    For    :00:00:03.6787001
    Foreach:00:00:03.9624871
    For    :00:00:04.3212027
    Foreach:00:00:04.7248767
    For    :00:00:05.1451293
    Foreach:00:00:05.5856065
    For    :00:00:05.9521592
    Foreach:00:00:06.3559976