No release in over 3 years
An educational Ruby C extension implementing the Eisel-Lemire algorithm for string-to-float conversion. This gem demonstrates why this optimization, despite being ~2.6x faster for complex numbers, was NOT submitted to Ruby core: it regresses performance by ~9% on simple numbers (the common case). Use this gem to learn about float parsing algorithms and their tradeoffs, or if you specifically work with high-precision scientific data.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

FastFloatLemire

Build Status Gem Version

Eisel-Lemire algorithm for string-to-float conversion in Ruby.

About

This gem implements the Eisel-Lemire algorithm with additional fast paths for common number formats.

Number Type vs String#to_f
Simple decimals ("1.5", "3.14") ~7% faster
Prices ("9.99", "19.95") ~3% faster
Scientific ("1e5") ~6% slower
Complex ("3.141592653589793") ~2.8x faster

Optimizations

The implementation includes several fast paths that bypass the full Eisel-Lemire algorithm:

  1. Small integer fast path - handles "5", "42", "-123" (up to 3 digits)
  2. Simple decimal fast path - handles "1.5", "9.99", "199.95" (up to 3+3 digits)
  3. Exact power-of-10 fast path - uses precomputed exact powers of 10 (10^0 to 10^22)
  4. Removed overhead - no strlen(), single whitespace skip

These optimizations are based on insights from Nigel Tao's Eisel-Lemire blog post.

Installation

gem install fast_float_lemire

Or add to your Gemfile:

gem 'fast_float_lemire'

Usage

require 'fast_float_lemire'

FastFloatLemire.parse("3.141592653589793")  #=> 3.141592653589793
FastFloatLemire.parse("1.5")                #=> 1.5
FastFloatLemire.parse("1e10")               #=> 10000000000.0

# Bulk parsing
FastFloatLemire.parse_array(["1.0", "2.0", "3.14"])  #=> [1.0, 2.0, 3.14]

Benchmarks

Run benchmarks with:

bundle exec ruby benchmarks/comparison.rb

References

License

MIT