LINQ性能测试

例子参照网址:

https://blog.csdn.net/weixin_34185512/article/details/94654128

测试结果是:linq的执行效率基本上都要比用基础代码编写的foreach要慢一点。(跟上一篇一样的结论)

1.测试一代码如下:

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace CloudCodeTest.Controllers
{
    public class LINQPerformanceExpController : Controller
    {
        // GET: LINQPerformanceExp
        public ActionResult Index()
        {
            List<string> xxx = new List<string>() { "aaaa", "bbbb", "aaabbb", "ccc", "aaaccc" };
            System.Diagnostics.Stopwatch watchLinq = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch watchLinqList = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch watchForeach = new System.Diagnostics.Stopwatch();
            watchLinq.Start();
            for (int i = 0; i < 100000; i++)
            {
                var linq = xxx.Select(x => "000" + x);
            }
            watchLinq.Stop();
            var watchLinqUseTime = (double)watchLinq.ElapsedMilliseconds;

            watchLinqList.Start();
            for (int i = 0; i < 100000; i++)
            {
                var linq = xxx.Select(x => "000" + x).ToList();
            }
            watchLinqList.Stop();
            var watchLinqListUseTime = (double)watchLinqList.ElapsedMilliseconds;

            watchForeach.Start();
            for (int i = 0; i < 100000; i++)
            {
                var yyy = new List<string>();
                foreach (var item in xxx) yyy.Add("000" + item);
            }
            watchForeach.Stop();
            var watchForeachUseTime = (double)watchForeach.ElapsedMilliseconds;
            ViewBag.watchLinqUseTime = watchLinqUseTime;
            ViewBag.watchLinqListUseTime = watchLinqListUseTime;
            ViewBag.watchForeachUseTime = watchForeachUseTime;
            return View();
        }
    }
}

测试结果如下:

 2.测试二,加where代码如下:

  //测试二:用上Select().Where().GroupBy().Select()等过滤条件
            System.Diagnostics.Stopwatch watchLinqWhereList = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch watchforeachWhere = new System.Diagnostics.Stopwatch();
            watchLinqWhereList.Start();
            for (int i = 0; i < 100000; i++)
            {
                var linq = xxx.Select(x => "000" + x).Where(x => x.IndexOf("aaa") != -1).ToList();
            }
            watchLinqWhereList.Stop();
            var watchLinqWhereUseTime = (double)watchLinqWhereList.ElapsedMilliseconds;

            watchforeachWhere.Start();
            for (int i = 0; i < 100000; i++)
            {
                var yyy = new List<string>();
                foreach (var item in xxx)
                {
                    var tmp = "000" + item;
                    if (tmp.IndexOf("aaa") != -1) yyy.Add(tmp);
                }
            }
            watchforeachWhere.Stop();
            var watchForeachWhereUseTime = (double)watchforeachWhere.ElapsedMilliseconds;
            ViewBag.watchLinqWhereUseTime = watchLinqWhereUseTime;
            ViewBag.watchForeachWhereUseTime = watchForeachWhereUseTime;

测试二结果如下:

 

posted @ 2022-04-02 16:15  katesharing  阅读(157)  评论(0编辑  收藏  举报