Skip to content

Consider using ReadOnlySpan<char> instead of string as input parameter #15

Open
@GiottoVerducci

Description

@GiottoVerducci

This works at least for the static version as ReadOnlySpan (for the instance version, you need a ref struct instead of a class).

The code change is very simple, you just have to replace

public static int Distance(string value1, string value2)

with

public static int Distance(ReadOnlySpan<char> value1, ReadOnlySpan<char> value2)

The boring part is making it compatible with the various frameworks, and defaulting to string when the Span feature is not available.

This would allow the use of the algorithm on chunks of text (rather than having to split the string).

Performances are about the same on .NET8:

// * Summary *

BenchmarkDotNet v0.13.12, Windows 10 (10.0.19045.5371/22H2/2022Update)
AMD Ryzen 9 7950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK 9.0.101
[Host] : .NET 8.0.12 (8.0.1224.60305), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
DefaultJob : .NET 8.0.12 (8.0.1224.60305), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

Method Mean Error StdDev Rank
Fastenshtein 104.7 us 0.26 us 0.25 us 1
FastenshteinSpan 110.4 us 0.95 us 0.89 us 2
FastenshteinStatic 134.0 us 0.66 us 0.62 us 4
FastenshteinStaticSpan 126.9 us 0.80 us 0.71 us 3

// * Hints *
Outliers
BenchmarkSmallWordsSingleThread.FastenshteinStatic2: Default -> 1 outlier was removed (130.25 us)

(for the non static version, I only change the parameter type of the second string)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions