csFastFloat 4.1.5

dotnet add package csFastFloat --version 4.1.5                
NuGet\Install-Package csFastFloat -Version 4.1.5                
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="csFastFloat" Version="4.1.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add csFastFloat --version 4.1.5                
#r "nuget: csFastFloat, 4.1.5"                
#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.
// Install csFastFloat as a Cake Addin
#addin nuget:?package=csFastFloat&version=4.1.5

// Install csFastFloat as a Cake Tool
#tool nuget:?package=csFastFloat&version=4.1.5                

csFastFloat : a fast and accurate float parser

.NET

C# port of Daniel Lemire's fast_float fully ported from C++ to C#. In older versions (5.0 and under) it is up to 9 times faster than the standard library in some cases while providing exact results.

Note

An important portion of our work had been merged to .NET Runtime within version 7.0 of the .NET Framework.

Benchmarks

We use the realistic files in /data. The mesh.txt data file contains numbers that are easier to parse whereas the canada.txt data file is representative of a more challenging scenario. Synthetic.txt contains 150 000 random floats. We compare the Double.Parse() function from the runtime library with our FastFloat.ParseDouble() function. The ParseNumberString() only function parses the string itself without any float computation: it might represent an upper bound on the possible performance. Most recent benchmark done with .NET 9.0


BenchmarkDotNet v0.14.0, Rocky Linux 9.4 (Blue Onyx)
Intel Xeon Gold 6338 CPU 2.00GHz, 2 CPU, 128 logical and 64 physical cores
.NET SDK 9.0.100
  [Host]   : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  .NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

Job=.NET 9.0  Runtime=.NET 9.0  

| Method                              | FileName           | Mean        | Min         | Ratio | MFloat/s | MB/s     |
|------------------------------------ |------------------- |------------:|------------:|------:|---------:|---------:|
| FastFloat.TryParseDouble()          | data/canada.txt    |  2,618.8 us |  2,614.8 us |  0.23 |    42.50 |   798.54 |
| Double.Parse()                      | data/canada.txt    | 11,216.7 us | 11,191.3 us |  1.00 |     9.93 |   186.57 |
|                                     |                    |             |             |       |          |          |
| FastFloat.TryParseDouble()          | data/mesh.txt      |    957.3 us |    949.3 us |  0.24 |    76.92 |   653.13 |
| Double.Parse()                      | data/mesh.txt      |  4,016.3 us |  4,005.7 us |  1.00 |    18.23 |   154.78 |
|                                     |                    |             |             |       |          |          |
| FastFloat.TryParseDouble()          | data/synthetic.txt |  3,141.1 us |  3,136.1 us |  0.22 |    47.83 |   899.86 |
| Double.Parse()                      | data/synthetic.txt | 14,575.3 us | 14,541.5 us |  1.00 |    10.32 |   194.07 |

In this repo FastFloatTestBench we demonstrate a concrete performance gain obtained with FastFloat.ParseDouble() with the CSVHelper library. This is one of the fastest CSV parser available.

Requirements

.NET Standard 2.0 or newer. Under .NET 5 framework or newer, the library takes advantage of the new Math.BigMul() and Sse4.1 (SIMD) functions.

Compile and testing

As this library targets multiple framework, you can specify the target framework version with -f parameter :

dotnet build -c Release -f net9.0
dotnet test -f net9.0

If you omit the target framework and you don't have both .net 8.0 and dotnetcore 3.1 SDKs installed you may experience an error when building or running tests.

The set of unit tests in /TestcsFastFloat project combines unit tests from many libraries. It includes tests used by the Go Team. Additional info on Nigel Tao's work can be found here. It also include strtod test file that can be found here.

Some unit tests are based on Approvals.net library. They require a diff tool installed on your computer. Tests will be automatically skiped if no diff tool is found.

Usage

Two functions are available: FastDoubleParser.ParseDouble and FastFloatParser.ParseFloat. Since v3.0, TryParse pattern is supported for each function.

String, char * and ReadOnlySpan<char> are supported inputs.

using csFastFloat;


double x;
float y;
double z;
double answer = 0;
foreach (string l in lines)
{
        x = FastDoubleParser.ParseDouble(l);
        FastDoubleParser.TryParseDouble(l, out z);
        
        y = FastFloatParser.ParseFloat(l);
}

Input strings are expected to be valid UTF-16.

Trailing content in the string is ignored. You may pass an optional out int characters_consumed parameter FastDoubleParser.ParseDouble(l, out int characters_consumed) if you wich to check how many characters were processed. Some users may want to fail when the number of characters consumed does not match the string length.

For UTF-8 or ASCII inputs, you may pass a ReadOnlySpan<byte> argument. You can also pass an optional out int characters_consumed parameter to track the number of characters consumed by the number pattern.

Credit

This library was the main project in my master's degree under the direction of Professor Daniel Lemire at TELUQ University. A special thanks to Egor Bogatov and all contributors for their really meaningful contribution.

Reference

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.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.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on csFastFloat:

Package Downloads
Sep

World's Fastest .NET CSV Parser. Modern, minimal, fast, zero allocation, reading and writing of separated values (csv, tsv etc.). Cross-platform, trimmable and AOT/NativeAOT compatible.

GeometryDashAPI

API for game Geometry Dash

PlistAPI

Plist API that allows to serialize and deserialize directly as a specific objects

DNV.Sesam.Sif.Decoder

Package Description

CSVParse

An ultra-fast CSV parser.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on csFastFloat:

Repository Stars
nietras/Sep
World's Fastest .NET CSV Parser. Modern, minimal, fast, zero allocation, reading and writing of separated values (`csv`, `tsv` etc.). Cross-platform, trimmable and AOT/NativeAOT compatible.
stark-lang/stark
Main repository for the stark compiler frontend + vscode integration + language specs
Version Downloads Last updated
4.1.5 43 11/21/2024
4.1.4 1,251,944 4/20/2024
4.1.3 298 4/20/2024
4.1.2 2,644 4/9/2024
4.1.0 1,437,286 11/6/2021
4.0.0 865 10/22/2021
3.0.0 2,337 3/15/2021
2.0.0 978 3/5/2021
1.0.0 1,112 2/22/2021