当前位置: 首页 > news >正文

青岛模板做网站建设企业网站哪家好

青岛模板做网站,建设企业网站哪家好,云南省建设厅网站地址,一个做网站的团队需要哪些人员2.Parallel.ForEach() 的使用 从 ForEach() 这个名字可以看出该方法是用来遍历泛型集合的#xff0c;新建一个 ASP.NET Core Web应用的项目#xff0c;如下#xff1a; 在 Index.cshtml.cs 文件中增加一个 UserInfo.cs 的类#xff0c;代码如下#xff1a; public class U…2.Parallel.ForEach() 的使用 从 ForEach() 这个名字可以看出该方法是用来遍历泛型集合的新建一个 ASP.NET Core Web应用的项目如下 在 Index.cshtml.cs 文件中增加一个 UserInfo.cs 的类代码如下 public class UserInfo{ public int UserId { get; set; }public string UserName { get; set; }} 在 Index.cshtml.cs 文件的 IndexModel 类中添加测试方法 ParallelForEachDemo()代码如下 public void OnGet(){ParallelForEachDemo();} public string DemoStr; //定义页面取值的变量public void ParallelForEachDemo(){//构造泛型集合数据ListUserInfo userList new ListUserInfo{new UserInfo{ UserId1,UserName张三 },new UserInfo{ UserId2,UserName李四 },new UserInfo{ UserId3,UserName王五 },new UserInfo{ UserId4,UserName赵六 },new UserInfo{ UserId5,UserName大师兄 }};//foreach 循环时间统计string allName1 string.Empty;Stopwatch sw1 new Stopwatch();sw1.Start(); //计时开始foreach (UserInfo user in userList){allName1 user.UserName ,;Thread.Sleep(10);//模拟一个耗时操作以免看不到效果}sw1.Stop(); //计时结束//Parallel.ForEach 循环时间统计string allName2 string.Empty; Stopwatch sw2 new Stopwatch();sw2.Start(); //计时开始Parallel.ForEach(userList, user //多线程遍历{allName2 user.UserName ,;Thread.Sleep(10);//模拟一个耗时操作以免看不到效果});sw2.Stop(); //计时结束//记录花费时间DemoStr string.Format(foreach 循环花费时间为{0}Parallel.ForEach 循环花费时间为{1}, sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds);} 在 Index.cshtml.cs 中输出 DemoStr 变量的值代码如下 div classtext-centerh1 classdisplay-4Welcome/h1br /p Model.DemoStr /p /div 编译后运行结果如下 显然使用多线程更快单线程和多线程的时间差距大概是5倍左右不同CPU时间不同这里仅代表本次执行结果。 3.Parallel.Invoke() 的使用 在 Index.cshtml.cs 文件的 IndexModel 类中新增方法分别访问 bing.com360.cnbaidu.com 这3个网站 然后统计响应的字符数代码如下 /// summary/// Thread.CurrentThread.ManagedThreadId 用于显示当前的线程ID/// /summary/// param namefrom/param/// param nameurl/parampublic void CountString(string from, string url){long cnt 0;HttpWebRequest request WebRequest.CreateHttp(url); //根据给定的网址创建一个请求HttpWebResponse response (HttpWebResponse)request.GetResponse(); //得到响应对象cnt response.ContentLength; //获取响应内容长度//用table标签格式化输出便于查看ContentStr trtd from /tdtd Thread.CurrentThread.ManagedThreadId /td td url /tdtd cnt /td/tr;return;} 单线程顺序执行3次函数 /// summary/// 单线程执行3次函数取三个不同网址的内容/// /summary/// returns/returnspublic string SingleTotal(){Stopwatch sw1 new Stopwatch();sw1.Start();CountString(single, http://www.bing.com);CountString(single, http://www.360.cn);CountString(single, http://www.baidu.com);sw1.Stop();return sw1.ElapsedMilliseconds.ToString();} 多线程并发执行3次函数 public string MultiTotal(){Stopwatch sw2 new Stopwatch();sw2.Start(); // 使用Lambda表达式构造 Action这里可以传多个方法来并行执行不限于3个Parallel.Invoke(          ()CountString(Multi, http://www.bing.com), ()CountString(Multi, http://www.360.cn), ()CountString(Multi, http://www.baidu.com));sw2.Stop();return sw2.ElapsedMilliseconds.ToString();} 在 OnGet() 函数中分别调用单线程方法和多线程方法  public string DemoStr; //定义页面取值的变量来显示执行时间public string ContentStr; //定义页面取值变量来显示函数执行情况public void OnGet(){string time1 SingleTotal(); //单线程花费时间string time2 MultiTotal(); //多线程花费时间DemoStr string.Format(单线程花费时间为{0}多线程花费时间为{1}, time1, time2);//用表格来展示数据更清晰ContentStr table border1 width600 stylemargin:0 auto; trtd类型/tdtd线程ID/tdtd网址/tdtd响应内容长度/td/tr ContentStr /table; } 在 Index.cshtml 中修改代码如下 div classtext-centerh1 classdisplay-4Welcome/h1br /p Model.DemoStr /pbr /p Html.Raw(Model.ContentStr) /p /div 编译后运行结果如下  显然使用多线程时花费的时间更少。 从表格的执行明细中也可以看到使用多线程的时候3个方法分别是3个不同线程来执行的。
http://www.w-s-a.com/news/739987/

相关文章:

  • 国家城乡住房和建设部网站西安私人网站
  • 天津高端网站定制seo实战教程
  • 网站文章怎么做才能被快速收录网站备案核验系统
  • 子网站建设方案l建设银行网站
  • 免费看舆情网站网站备案用户名忘了怎么办
  • 地方门户网站的分类网站的方案
  • 沧州哪里做网站网站的建设是什么
  • 设计公司海报秦皇岛seo网站推广
  • 网站导航规划wordpress做漫画
  • jsp体育用品网站建设wordpress 10万篇文章
  • 沈阳做微信和网站的公司网站在线支付接口
  • 重庆整合网络营销百度seo快速提升排名
  • 设计师网站外网百度分析工具
  • 旅游网站建设技术解决方案wordpress主题安装后找不到
  • 网站图片文字排版错误管理系统界面设计
  • 网站建设 台州广州惠科互联网技术有限公司
  • 网站页面尺寸大小四川鸿业建设集团网站
  • 做女朋友的网站局网站建设方案word
  • 做阿里国际网站会有成效吗科技网站有哪些
  • 高端公司网站建设北京两学一做网站
  • 黄埔网站建设设计wordpress 文件夹改名
  • 怎么什么软件可以吧做网站最火的二十个电商app
  • wordpress theme sage网站seo优化加推广
  • 建设一个大型电影网站公司网站建设工作总结
  • 传奇网站一般怎么做的宇泽佛山网站建设
  • google网站入口电商运营十大基础知识
  • 建设公司网站的细节中国建设网网站
  • 重庆美邦建网站宝安网页设计
  • 建网站的地址十堰做网站
  • 怎么评判一个网站做的好与坏专做情侣装网站