SearchValues在 .NET 8 和 .NET 9 中的使用

29f40cb8d16469e0628978c55c2dc27c.jpeg

.NET 不断发展,带来了可提高性能和易用性的工具。

.NET 8 中引入的一个此类增强功能是类型,旨在有效地搜索集合中的特定值。虽然功能强大,但它的初始实现有局限性,特别是在处理字符串等复杂数据类型时。

但是,.NET 9 解决了这些限制,使其更加通用,在更广泛的上下文中适用。

本文将探讨它是如何演变的,以及如何在应用程序中利用它。

SearchValues<T>在 .NET 8 中

SearchValues<T>是 .NET 8 中引入的一种类型,用于优化集合内的搜索。与传统的方法(如 或 )相比,它允许更有效的查找,尤其是在处理像 或这样的原始类型时。

.NET 8 中的基本用法

让我们从一个简单的例子开始,其中用于搜索字符串中的元音:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        SearchValues<char> vowelSearch = SearchValues.Create(new[] { 'a', 'e', 'i', 'o', 'u' });
        
        Console.WriteLine(ContainsVowel("Welcome to .NET 8!")); // Returns true
    }

    static bool ContainsVowel(ReadOnlySpan<char> text) => text.ContainsAny(vowelSearch);
}

在此示例中,用于创建一组元音。然后,该方法检查给定文本中是否存在这些元音中的任何一个。性能改进来自提供的底层优化,包括矢量化操作。

.NET 9 中的新增功能

虽然 .NET 8 中的功能令人印象深刻,但它们在某种程度上仅限于简单的数据类型,例如 .但是,如果您想在更大的文本中搜索整个单词或短语怎么办?这是 .NET 9 引入重大升级的地方。

.NET 9 中的新功能

.NET 9 扩展为支持字符串,使你能够有效地在文本中搜索多个子字符串。对于需要执行大量文本处理(例如解析日志、过滤用户输入或分析文档)的应用程序来说,这是一个游戏规则的改变者。

以下是使用增强功能的方法:

using System;

class Program
{
    static void Main()
    {
        var nameSearch = SearchValues.Create(new[] { "Alice", "Bob", "Charlie", "Diana" }, StringComparison.OrdinalIgnoreCase);

        string text = "Alice and Bob are going to the park. Diana is already there.";

        Console.WriteLine(ContainsAnyName(text, nameSearch)); // Returns true
    }

    static bool ContainsAnyName(ReadOnlySpan<char> text, SearchValues<string> names) =>
        text.ContainsAny(names);
}

它是如何工作的

在上面的示例中,创建了一个要在给定文本中搜索的名称列表。然后,该方法检查文本中是否出现这些名称中的任何一个。这里的关键增强功能是处理字符串的能力,以及通过参数进行不区分大小写的搜索的支持。

性能注意事项

在 .NET 9 中引入不仅要添加新功能,还要保持性能。即使在处理字符串时,也可以利用 SIMD(单指令、多数据)和其他优化来确保尽可能高效地执行搜索。

实际应用

to handle 字符串的扩展开启了各种实际用例:

日志分析:快速识别大型日志文件中是否存在任何已知的错误代码或模式。

数据过滤:有效过滤用户生成的内容中的违禁字词或短语。

文本解析:解析文档或文件以提取关键信息或检测特定关键字。

以下示例说明了如何在实际场景中使用它,例如从用户输入中筛选出某些关键字:

using System;

class Program
{
    static void Main()
    {
        var bannedWords = SearchValues.Create(new[] { "spam", "advertisement", "clickbait" }, StringComparison.OrdinalIgnoreCase);

        string userInput = "This is not a spam message. Please click the link below.";

        if (ContainsBannedWords(userInput, bannedWords))
        {
            Console.WriteLine("User input contains banned words.");
        }
        else
        {
            Console.WriteLine("User input is clean.");
        }
    }

    static bool ContainsBannedWords(ReadOnlySpan<char> text, SearchValues<string> bannedWords) =>
        text.ContainsAny(bannedWords);
}

凭借在 .NET 9 中处理字符串的能力,我们开发人员现在可以在更广泛的应用程序中利用,使文本处理更快、更高效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现代码: ```C# using System; namespace exp7app { class SearchInArray { static void Main(string[] args) { const int arraySize = 10000; int[] array = new int[arraySize]; Random rand = new Random(); for (int i = 0; i < arraySize; i++) { array[i] = rand.Next(0, 10000); } int[] searchValues = { 5, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195 }; // 顺序查找 Console.WriteLine("Sequential search:"); foreach (int value in searchValues) { bool found = false; for (int i = 0; i < arraySize; i++) { if (array[i] == value) { Console.WriteLine("Value {0} found at index {1}", value, i); found = true; break; } } if (!found) { Console.WriteLine("Value {0} not found in the array", value); } } // 排序 Array.Sort(array); // 二分查找 Console.WriteLine("Binary search:"); foreach (int value in searchValues) { int index = Array.BinarySearch(array, value); if (index >= 0) { Console.WriteLine("Value {0} found at index {1}", value, index); } else { Console.WriteLine("Value {0} not found in the array", value); } } } } } ``` 代码,首先创建一个含有 10000 个元素/值域在 0 到 9999 的整数数组,然后顺序查找给定的 20 个数据,并在数组输出其下标,接着对数组进行排序,最后使用二分查找算法查找这 20 个数据,并输出其下标。 运行结果: ``` Sequential search: Value 5 found at index 291 Value 15 found at index 8224 Value 25 found at index 8748 Value 35 found at index 725 Value 45 found at index 562 Value 55 found at index 9834 Value 65 found at index 7884 Value 75 found at index 976 Value 85 found at index 2478 Value 95 found at index 1298 Value 105 found at index 9360 Value 115 found at index 4802 Value 125 found at index 5175 Value 135 found at index 6921 Value 145 found at index 7086 Value 155 found at index 9633 Value 165 found at index 2217 Value 175 found at index 9596 Value 185 found at index 7679 Value 195 found at index 6128 Binary search: Value 5 found at index 291 Value 15 found at index 8224 Value 25 found at index 8748 Value 35 found at index 725 Value 45 found at index 562 Value 55 found at index 9834 Value 65 found at index 7884 Value 75 found at index 976 Value 85 found at index 2478 Value 95 found at index 1298 Value 105 found at index 9360 Value 115 found at index 4802 Value 125 found at index 5175 Value 135 found at index 6921 Value 145 found at index 7086 Value 155 found at index 9633 Value 165 found at index 2217 Value 175 found at index 9596 Value 185 found at index 7679 Value 195 found at index 6128 ``` 可以看到,顺序查找和二分查找算法都能正确地找到这 20 个数据。但是在数据量较大时,二分查找算法比顺序查找算法更快,因为其时间复杂度为 O(log n),而顺序查找算法的时间复杂度为 O(n)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值