关于 ReadOnlySpan<T>
using System;
using System.Linq; namespace BenchmarkAndSpanExample
{
public class NameParser
{
public string GetLastName(string fullName)
{
var names = fullName.Split(" "); var lastName = names.LastOrDefault(); return lastName ?? string.Empty;
} public string GetLastNameUsingSubstring(string fullName)
{
var lastSpaceIndex = fullName.LastIndexOf(" ", StringComparison.Ordinal); return lastSpaceIndex == -
? string.Empty
: fullName.Substring(lastSpaceIndex + );
} public ReadOnlySpan<char> GetLastNameWithSpan(ReadOnlySpan<char> fullName)
{
var lastSpaceIndex = fullName.LastIndexOf(' ');
return lastSpaceIndex == -
? ReadOnlySpan<char>.Empty
: fullName.Slice(lastSpaceIndex + );
}
}
}
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order; namespace BenchmarkAndSpanExample
{
[RankColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
public class NameParserBenchmarks
{
private const string FullName = "Steve J Gordon";
private static readonly NameParser Parser = new NameParser(); [Benchmark(Baseline = true)]
public void GetLastName()
{
Parser.GetLastName(FullName);
} [Benchmark]
public void GetLastNameUsingSubstring()
{
Parser.GetLastNameUsingSubstring(FullName);
} [Benchmark]
public void GetLastNameWithSpan()
{
Parser.GetLastNameWithSpan(FullName);
}
}
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<NameParserBenchmarks>();
}
}
关于 ReadOnlySpan<T>的更多相关文章
- .Net Core中使用ref和Span<T>提高程序性能
一.前言 其实说到ref,很多同学对它已经有所了解,ref是C# 7.0的一个语言特性,它为开发人员提供了返回本地变量引用和值引用的机制. Span也是建立在ref语法基础上的一个复杂的数据类型,在文 ...
- C#的发展历程第五 - C# 7开始进入快速迭代道路
C# 7开始,C#加快了迭代速度,多方面的打磨让C#在易用性,效率等各方面都向完美靠近.另外得益于开源,社区对C#的进步也做了很大共享.下面带领大家看看C# 7的新特性.其中一部分是博主已经使用过,没 ...
- C# - Span 全面介绍:探索 .NET 新增的重要组成部分
假设要公开特殊化排序例程,以就地对内存数据执行操作.可能要公开需要使用数组的方法,并提供对相应 T[] 执行操作的实现.如果方法的调用方有数组,且希望对整个数组进行排序,这样做就非常合适.但如果调用方 ...
- Span<T>
Introduction Span<T> is a new type we are adding to the platform to represent contiguous regio ...
- C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】
C#7.2——编写安全高效的C#代码 2018-11-07 18:59 by 沉睡的木木夕, 123 阅读, 0 评论, 收藏, 编辑 原文地址:https://docs.microsoft.com/ ...
- KMP algorithm challenge string.Contains
KMP: public int KMP (ReadOnlySpan<char> content, ReadOnlySpan<char> span) { _next = new ...
- 实际体验Span<T> 的惊人表现
前言 最近做了一个过滤代码块功能的接口.就是获取一些博客文章做文本处理,然后这些博客文章的代码块太多了,很多重复的代码关键词如果被拿过来处理,那么会对文本的特征表示已经特征选择会有很大的影响.所以需要 ...
- .NET Core 的 Span<T> 学习与使用笔记
一.阅读材料 All About Span: Exploring a New .NET Mainstay Span<T> - byte to int conversions Span< ...
- C#7.2——编写安全高效的C#代码
原文地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/write-safe-efficient-code?view=netcore-2.1 值类型的优 ...
随机推荐
- OA传SAP设置(备忘)
package com.seeyon.apps.ext.kk.flow.hc; import java.rmi.RemoteException; import java.text.SimpleDate ...
- java基础(17):包装类、System、Math、Arrays、大数据运算
1. 基本类型包装类 大家回想下,在第三篇文章中我们学习Java中的基本数据类型时,说Java中有8种基本的数据类型,可是这些数据是基本数据,想对其进行复杂操作,变的很难.怎么办呢? 1.1 基本类型 ...
- js键盘事件以及键盘事件拦截
一.键盘事件 onkeydown: 按下键盘时触发 onkeypress: 按下有值的键时触发 注意: onkeypress按下 Ctrl.Alt.Shift.Meta 这样无值的键,这个事件不会触发 ...
- Zuul 1.x 的工作原理
Zuul简介 Zuul在微服务架构中,可以作为提供动态路由,监控,弹性,安全等边缘服务的框架.在Netflix,被用作所有请求到达streaming application的前门.Zuul使用一系列不 ...
- 自定义滚动条样式纯(css)
啥都不说先看图: 注: 只适合chrom,不适用IE和fireFox 下面展示代码: <html lang="en"> <head> <meta ch ...
- 深浅拷贝的应用-copy、mutableCopy
ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController //如果想让li ...
- iOS开发时获取第一响应者
上篇中提到键盘相应时间中用到了获取当前第一响应者的方法是苹果的是有方法,无法上传到App Store,本文将介绍一种非常简单的且未用到私有API的方法来获取当前第一响应者. 实现思路:用到的iOS A ...
- swift(四)swift的广义匹配
//swift的广义匹配 let x = switch x { ...: println("个位数") ...: println("十位数") default: ...
- Android开发总体布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- 6.JavaCC官方入门指南-例1
例1:整数加法运算 在这个例子中,我们将判断如下输入的式子是否是一个合法的加法运算: 99 + 42 + 0 + 15 并且在输入上面式子的时候,数字与加号之间的任何位置,都是可以有空格或者换 ...