FastDtw.CSharp 2.3.0

dotnet add package FastDtw.CSharp --version 2.3.0
                    
NuGet\Install-Package FastDtw.CSharp -Version 2.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FastDtw.CSharp" Version="2.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FastDtw.CSharp" Version="2.3.0" />
                    
Directory.Packages.props
<PackageReference Include="FastDtw.CSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FastDtw.CSharp --version 2.3.0
                    
#r "nuget: FastDtw.CSharp, 2.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FastDtw.CSharp@2.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FastDtw.CSharp&version=2.3.0
                    
Install as a Cake Addin
#tool nuget:?package=FastDtw.CSharp&version=2.3.0
                    
Install as a Cake Tool

FastDtw.CSharp

Fast Dynamic Time Warping (DTW) algorithm implementation in .NET C# with a focus on performance.

Reference: Fast DTW: Toward Accurate Dynamic Time Warping in Linear Time and Space

Usage

For double precision results:

double[] seriesA = new double[] {0, 1, 2, 3};
double[] seriesB = new double[] {1, 2, 3, 4, 5};

Dtw.GetScore(seriesA, seriesB);

// Starting from 2.2.0 version, the score can be normalized, e.g.:
Dtw.GetScore(seriesA, seriesB, NormalizationType.PathLength);

For single precision results:

float[] seriesA = new float[] { 41.98f, 41.65f, 42.01f, 42.35f, 44.4f };
float[] seriesB = new float[] { 95.07f, 93.5f, 96.67f, 96.28f, 102.47f, 94.24f, 95.12f, 87.06f };

Dtw.GetScore(seriesA, seriesB);

Weighted double precision DTW score. Available weighting approaches: Multiplicative, ArithmeticMean, HarmonicMean:

var seriesA = new double[] { 41.98, 41.65, 42.01, 42.35, 44.4 };
var weightsSeriesA = new double[] { 0.5, 0.6, 0.7, 0.8, 0.9 };
var seriesB = new double[] { 95.07, 93.5, 96.67, 96.28, 102.47, 94.24, 95.12, 87.06 };
var weightsSeriesB = new double[] { 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2 };

Dtw.GetWeightedScore(seriesA, seriesB, weightsSeriesA, weightsSeriesB, WeightingApproach.HarmonicMean);

To get warp path:

double[] seriesA = new double[] {0, 1, 2, 3};
double[] seriesB = new double[] {1, 2, 3, 4, 5};

Dtw.GetPath(seriesA, seriesB);

Starting from 2.3.0 version, the score can be constrained by a Sakoe-Chiba band. The warping path is not allowed to deviate from the diagonal further than the given band width, which reduces the complexity from O(nm) to O(nbandWidth):

double[] seriesA = new double[] {0, 1, 2, 3};
double[] seriesB = new double[] {1, 2, 3, 4, 5};

Dtw.GetScore(seriesA, seriesB, 100);

One must consider that the constrained score is a different measure, and it is greater than or equal to the unconstrained one. The band is widened by the length difference automatically, so a warping path always exists.

Starting from 2.3.0 version, several series can be scored in a single call. Subjects of equal length are processed in SIMD lanes and the work is spread over the available cores. The returned scores are identical to the ones obtained by calling .GetScore() in a loop. The batch methods are available when targeting .NET 6.0 and higher:

double[] query = new double[] {0, 1, 2, 3};
double[][] subjects = new double[][] { ... };

double[] scores = Dtw.GetScores(query, subjects);

To get the complete pairwise distance matrix, which is symmetric and has a zero diagonal:

double[,] matrix = Dtw.GetScoreMatrix(series);

To find the closest series, LB_Keogh lower bound is used to skip the candidates which cannot win:

double[] query = new double[] {0, 1, 2, 3};
double[][] subjects = new double[][] { ... };

int index = Dtw.GetNearestNeighbor(query, subjects, 16, out double distance);

The returned index is exactly the one obtained by scoring every subject with .GetScore() and taking the minimum, since the bound only removes the candidates whose distance is provably no better than the incumbent. The search is constrained by the band, and all series are required to be of the same length. The bound itself is available as .GetLowerBound(). It should be noted that the bound is tight on smooth series, and it is weak on noisy ones, where almost nothing is skipped.

Performance results

The comparison was performed to the following packages: FastDtw (v.1.1.4), NDtw v.0.2.3, and ADN.TimeSeries (v.1.3.0). The proposed implementation is at least 3 times faster and requires significantly smaller memory allocation compared to the listed libraries. The complete results are summarized below on Apple M2 macOS Sonoma 14.6.1 (.NET 8.0):

Method/Library Series length Mean Ratio Gen0 Gen1 Gen2 Allocated Alloc Ratio
FastDtw.CSharp.GetScore() 8163x8089 151.6 ms 1.0 - - - 131.3 KB 1.0
FastDtw.CSharp.GetScoreF() 8163x8089 130.9 ms 0.86 - - - 66.8 KB 0.51
FastDtw.CSharp.GetPath() 8163x8089 182.8 ms 1.21 333 333 333 528.8 MB 4,027.0
FastDtw.Distance() 8163x8089 450 ms 2.97 - - - 529.7 MB 4,034.0
ADN.TimeSeries 8163x8089 2.8 s 18.3 1000 1000 1000 1.06 GB 8,046.7
NDtw 8163x8089 980.7 ms 6.47 343 K 178 K 7 K 2.11 GB 16,103.2
FastDtw.CSharp.GetScore() 500x500 0.331 ms 1.0 1.46 - - 16.0 KB 1.0
FastDtw.CSharp.GetScoreF() 500x500 0.327 ms 0.99 0.49 - - 8.07 KB 0.5
FastDtw.CSharp.GetPath() 500x500 0.391 ms 1.18 339.4 335.4 332.5 2.03 MB 126.7
FastDtw.Distance() 500x500 1.209 ms 3.65 347.7 339.8 330.1 2.09 MB 129.9
ADN.TimeSeries 500x500 8.791 ms 26.5 562.5 562.5 484.4 4.02 MB 249.9
NDtw 500x500 2.931 ms 8.85 968.8 460.9 230.5 8.10 MB 504.3
FastDtw.CSharp.GetScore() 10x10 125.5 ns 1.0 0.047 - - 392 B 1.0
FastDtw.CSharp.GetScoreF() 10x10 120.0 ns 0.96 0.028 - - 232 B 0.59
FastDtw.CSharp.GetPath() 10x10 258.6 ns 2.06 0.174 - - 1.46 KB 3.71
FastDtw.Distance() 10x10 758.9 ns 6.05 0.33 - - 2.76 KB 7.0
ADN.TimeSeries 10x10 2,648.8 ns 21.1 0.252 - - 2.11 KB 5.4
NDtw 10x10 1,192.5 ns 9.5 0.696 - - 5.83 KB 14.9

One must consider which method to use to get the best performance. Specifically, if you are interested in a DTW warp score only .GetScore() is sufficient since it has the smallest memory allocation and the fastest processing time. The .GetPath() method still requires larger allocations, since the step taken in every cell has to be retained until the path is traced back. Additionally, the single precision overload will decrease memory allocations since single precision variable is used instead of double. The implementation is slightly different when targeting .NET 6.0 and higher compared to .NET Standard. The results are summarized below on Intel Core i7-9750H CPU, Windows 11):

Method Runtime Series length Mean Gen0 Allocated
GetScore() .NET 8.0 8163x8089 181.5 ms - 130.8 KB
GetScore() .NET481 8163x8089 232.3 ms - 133.4 KB
GetScore() .NET 8.0 500x500 0.54 ms 1.95 16.2 KB
GetScore() .NET481 500x500 0.84 ms 4.88 33.1 KB
GetScore() .NET 8.0 10x10 292.9 ns 0.078 488 B
GetScore() .NET481 10x10 792.4 ns 0.187 1.18 KB
Method Runtime Series length Mean Gen0 Gen1 Gen2 Allocated
GetPath() .NET 8.0 8163x8089 213.8 ms 500.0 500.0 500.0 516.4 MB
GetPath() .NET481 8163x8089 411.1 ms 1000.0 1000.0 1000.0 516.4 MB
GetPath() .NET 8.0 500x500 1.45 ms 498.05 498.05 498.05 2.0 MB
GetPath() .NET481 500x500 1.68 ms 498.05 498.05 498.05 2.0 MB
GetPath() .NET 8.0 10x10 538.0 ns 0.247 - - 1.5 KB
GetPath() .NET481 10x10 1209.2 ns 0.3557 - - 2.2 KB

Warp path memory

Up to 2.2.0 version the complete cost matrix was kept as double[n, m], although it was only needed so that the backtracking step could determine which of the three predecessors had won. Starting from 2.3.0 version that decision is recorded directly as a single byte per cell, and only two rolling rows of costs are retained. The returned score and the returned path are unchanged. The measurements below were taken directly rather than with BenchmarkDotNet, on Apple M2 (.NET 10.0):

Series length Allocated, 2.2.0 Allocated, 2.3.0 Mean, 2.2.0 Mean, 2.3.0
8163x8089 504.3 MB 63.6 MB 118.9 ms 110.5 ms
4000x4000 122.3 MB 15.5 MB 28.1 ms 22.9 ms
2000x2000 30.6 MB 4.0 MB 7.1 ms 5.4 ms
500x500 1.9 MB 0.3 MB 0.4 ms 0.4 ms

It should be noted that the allocation is reduced by a factor of eight, whereas the processing time is affected only marginally, since at large series lengths the method is limited by writing the step matrix rather than by the computation itself. The .GetPath() rows of the tables above were obtained before this change, and they still reflect the 2.2.0 behaviour.

License

FastDtw.CSharp is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.3.0 191 8/22/2026
2.2.0 1,956 10/29/2024
2.1.0 346 9/14/2024
2.0.1 301 9/3/2024
2.0.0 299 8/15/2024
1.1.1 303 7/14/2024
1.1.0 516 4/4/2023
1.0.0 880 12/5/2022