Coder Social home page Coder Social logo

frepr's Introduction

frepr

https://travis-ci.org/wemoloh/frepr.svg?branch=master

Fast float repr() for CPython using https://github.com/google/double-conversion. Approximately 8X faster than the built-in function.

Quick Start

>>> import frepr
>>> frepr.frepr(-1.8397892359791307e+134)
'-1.8397892359791307e+134'
>>> from timeit import timeit
>>> timeit('repr(-1.8397892359791307e+134)')
2.7296602272476758
>>> frepr.install()
>>> timeit('repr(-1.8397892359791307e+134)')
0.35723431229251545
>>> frepr.uninstall()

API

frepr.frepr(value)

Returns shortest string representation of a floating point value. The argument doesn't have to be a float, as long as it is coercible to one (i.e. it has a __float__() method).

frepr.install()

Monkey-patches the Python runtime. Subsequent calls to repr() on floats will use the faster algorithm. This is safe to call multiple times.

frepr.uninstall()

Reverses the monkey-patching done by install(). Subsequent calls to repr() will use the original built-in algorithm. This is safe to call multiple times, even if install() was not called first.

Features

  • On average, approximately 8X faster than the standard function. (This result obtained on Python 3.6.0 64-bit/Windows 7/Intel Core i3 @ 2.93GHz. Test your own system with tests/perf.py).
  • Speeds up the standard json package (and any other library that relies on repr()).
  • Output is guaranteed to contain the shortest string of decimal digits necessary to uniquely identify the original IEEE 754 floating-point value.
  • Guaranteed to satisfy value == ast.literal_eval(frepr(value)).
  • Tested on CPython 2.6 through 3.6; probably works on other versions too.

Limitations

  • Currently, there is no support for alternative Python implementations (e.g. PyPy).
  • Output can be slightly different from the native function, due to floating-point rounding (but see accuracy guarantees above).
  • Has no effect on other built-in methods of converting floats to strings (e.g. str(), str.format(), "%f", "%g").

See Also

frepr's People

Contributors

wemoloh avatar

Stargazers

 avatar Ben Samuel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.