zoukankan      html  css  js  c++  java
  • dotnet core 黑科技·String

    本文来告诉大家 dotnet core 里面使用的黑科技,如何提高String.IndexOf(char)的性能

    Performance Improvements in .NET Core有说道哪些提高性能的代码,所以我就去看了一下,发现有一些黑科技。

    里面包括了 Concat 的提升和很多 linq 的提升,我准备在自己的 WPF 项目使用这些代码,因为现在的项目没有使用 .net Framework 4.7 。

    感觉垃圾微软把很多功能放在一个 Framework 让很多开发者无法升级

    本文主要来让大家看一下 IndexOf 的黑科技

    修改的提交在Improve performance of String.IndexOf(char) and String.LastIndexOf(char) by bbowyersmyth

             public unsafe int IndexOf(char value, int startIndex, int count)
             {
                 if (startIndex < 0 || startIndex > Length)
                     throw new ArgumentOutOfRangeException("startIndex", SR.ArgumentOutOfRange_Index);
     
                 if (count < 0 || count > Length - startIndex)
                     throw new ArgumentOutOfRangeException("count", SR.ArgumentOutOfRange_Count);
     
                 fixed (char* pChars = &_firstChar)
                 {
                     char* pCh = pChars + startIndex;
    -                for (int i = 0; i < count; i++)
    +
    +                while (count >= 4)
    +                {
    +                    if (*pCh == value) goto ReturnIndex;
    +                    if (*(pCh + 1) == value) goto ReturnIndex1;
    +                    if (*(pCh + 2) == value) goto ReturnIndex2;
    +                    if (*(pCh + 3) == value) goto ReturnIndex3;
    +
    +                    count -= 4;
    +                    pCh += 4;
    +                }
    +
    +                while (count > 0)
                     {
                         if (*pCh == value)
    -                        return i + startIndex;
    +                        goto ReturnIndex;
    +
    +                    count--;
                         pCh++;
                     }
    -            }
     
    -            return -1;
    +                return -1;
    +
    +                ReturnIndex3: pCh++;
    +                ReturnIndex2: pCh++;
    +                ReturnIndex1: pCh++;
    +                ReturnIndex:
    +                return (int)(pCh - pChars);
    +            }
             }
    

    可以看到.net Framework 的代码是使用循环

     fixed (char* pChars = &_firstChar)
     {
     	char* pCh = pChars + startIndex;
     	for (int i = 0; i < count; i++)
     	{
     		if (*pCh == value)
     		{
     			return i + startIndex;
     		}
    
     		pCh++;
     	}
     } 
     
    

    代码很简单,但是优化只有就使用了很黑的

             fixed (char* pChars = &_firstChar)
                {
                    char* pCh = pChars + startIndex;
    
                    while (count >= 4)
                    {
                        if (*pCh == value) goto ReturnIndex;
                        if (*(pCh + 1) == value) goto ReturnIndex1;
                        if (*(pCh + 2) == value) goto ReturnIndex2;
                        if (*(pCh + 3) == value) goto ReturnIndex3;
    
                        count -= 4;
                        pCh += 4;
                    }
    
                    while (count > 0)
                    {
                        if (*pCh == value)
                            goto ReturnIndex;
    
                        count--;
                        pCh++;
                    }
    
                    return -1;
    
                    ReturnIndex3: pCh++;
                    ReturnIndex2: pCh++;
                    ReturnIndex1: pCh++;
                    ReturnIndex:
                    return (int)(pCh - pChars);
    

    为什么需要使用这样的方法,因为这样可以直接塞满寄存器,做判断会快很多。这和具体编译有关

    测试代码可以使用IndexOfChar.cs

    如果想看大神的测试,Measuring Performance Improvements in .NET Core with BenchmarkDotNet

    我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新

    如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入

    知识共享许可协议
    本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

  • 相关阅读:
    Warning! PATH is not properly set up...
    用rvm切换ruby
    Mac下多版本JDK安装
    iOS开发 密码里面含有特殊字符如何处理传给后端
    Cornerstone版本回退160013错误
    iOS 11 Xcode9 tableview点击cell上的按钮cell自动往上跳动
    iOS 获取全部字体的Fontfamily和FontName
    iOS WKWebView 点击超链接跳转至Safari
    iOS 11在window上加视图不显示
    Java并发(2)
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086736.html
Copyright © 2011-2022 走看看