bcmyers / num-format

A Rust crate for producing string representations of numbers, formatted according to international standards
Apache License 2.0
123 stars 22 forks source link

saw you present at RustNYC, thought I'd introduce and share some python stats #16

Open Dowwie opened 5 years ago

Dowwie commented 5 years ago

Hey Brian I watched you present at RustNYC. Cool project.

I was curious how num_format performed relative to Python and so put together a tiny benchmark to find out. It seems that non-buffered num_format is approximately on par with that of standard string formatting in Python, formatting 1_000_000 at roughly 54ns median with Python 2 whopping nanoseconds faster at 52ns median. However, num_format benches buffered at 30 picoseconds. Nice. :+1:

I didn't study the source in Python but maybe it's making good use of an underlying C library and CFFI. Rust is usually at least 10x faster than CPython so it could likely be that. Any how, here's how you can verify on your own. You simply need to install the perf library for python using pip:

import perf

def format_million():
    "{:,}".format(1_000_000)

def run_bench():
    runner = perf.Runner()
    runner.timeit("format_million",
                  "format_million()",
                  "from __main__ import format_million",
                  inner_loops=10)

if __name__ == "__main__":
    run_bench()

run this at the command line, assuming you've named the above code pyformat.py:

python3 pyformat.py --append=result.json --affinity=5

and to get more summary stats, once you have the result.json file:

python3 -m perf stats result.json

see you at a future meetup

-Darin

bcmyers commented 5 years ago

Cool. Will look into this, especially since Python is my second favorite language to play around with. Definitely seems like this kind of functionality in Python is probably written in C, not Python itself, in which case one would expect it to be pretty fast. Perhaps I can learn something from their implementation, though. In any case, thanks!