新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版用于讨论编程和软件设计的技巧
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 编程心得 』 → for循环C#与C/C++的性能区别。 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7178 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: for循环C#与C/C++的性能区别。 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     DavidPotter 帅哥哟,离线,有人找我吗?
      
      
      等级:大三暑假(ITELS考了6.5分!)
      文章:150
      积分:852
      门派:Lilybbs.net
      注册:2006/3/7

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给DavidPotter发送一个短消息 把DavidPotter加入好友 查看DavidPotter的个人资料 搜索DavidPotter在『 编程心得 』 的所有贴子 点击这里发送电邮给DavidPotter 引用回复这个贴子 回复这个贴子 查看DavidPotter的博客楼主
    发贴心情 for循环C#与C/C++的性能区别。

    以C#为例的3种循环:
    int [] foo = new int[100];
    Loop1:
    foreach(int i in foo)
     Console.WriteLine(i.ToString());

    Loop2:
    for(int i=0; i<foo.Length; i++)
     Console.WriteLine(foo[i].ToString());

    Loop3:
    int len=foo.Length;
    for(int i=0; i<len; i++)
     Console.WriteLine(foo[i].ToString());
    In C/C++, we know that compared to Loop3, Loop2 does not need compute the length of foo again and again.
    However,
    different with C/C++
    the performance of Loop3 is worse than Loop2.
    C#code runs in a safe, managed enviroment. Every memory location is checked, including indexes.
    For Loop3:
    generated by complier:
    int len=foo.length;
    for(int i=0; i<len; i++)
    {
     if(i<foo.Length)
      Console.WriteLine(foo[i].ToString());
     else
      throw new IndexOutOfRangeException();
    }

    ref: [Bill Wagner] Effective C#-50 Specific ways to improve Your C#.


       收藏   分享  
    顶(1)
      




    ----------------------------------------------
    Don‘t try so hard, the best things come when you least expect them to.

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/5/19 10:02:00
     
     netjian 帅哥哟,离线,有人找我吗?白羊座1986-4-16
      
      
      头衔:智能入门者
      等级:大四(GRE考了1600分!)
      文章:198
      积分:1332
      门派:IEEE.ORG.CN
      注册:2007/5/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给netjian发送一个短消息 把netjian加入好友 查看netjian的个人资料 搜索netjian在『 编程心得 』 的所有贴子 点击这里发送电邮给netjian  引用回复这个贴子 回复这个贴子 查看netjian的博客2
    发贴心情 
    Get it ,the one more "if" may reduce the C# efficiency.
    I remember .  be in case of using it any more.

    ----------------------------------------------
    长江后浪,无坚不摧。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/2/17 11:39:00
     
     GoogleAdSense白羊座1986-4-16
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 编程心得 』 的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/2 18:45:51

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms