Code Monkey home page Code Monkey logo

parse-integers-benchmark's Introduction

parse-integers-benchmark

Benchmarking reading and parsing integers from a file in C++ using one thread only.

The methods benchmarked are:

  • getchar-inline - call std::getchar and parse each int char-by-char.
  • scanf - call std::scanf to parse each int.
  • scanf-multi - call std::scanf to parse 64 ints in one call.
  • iostream - call std::istream::operator>>(int&) to parse each int. No multi version is possible.
  • mmap-inline - mmap the entire file into memory and parse each int char-by-char.
  • mmap-charconv - mmap the entire file into memory and parse each int using std::from_chars.

Building and running

git clone https://github.com/max0x7ba/parse-integers-benchmark.git
cd parse-integers-benchmark
make -r run_parse_integers_fast

The benchmark compiles in C++17 mode by default. You can search-replace gnu++17 to gnu++11 in Makefile to make it work with C++11.

Example outputs

Ubuntu 18.04.4 LTS, g++-8.3.0, Intel Core i9-9900KS CPU @ 5.1GHz, C++17 charconv.
---- Best times ----
seconds,    percent, method
0.123534698,  100.0, scanf
0.121618955,   98.4, iostream
0.104887812,   84.9, scanf-multi
0.047167165,   38.2, getchar-inline
0.030817239,   24.9, mmap-charconv
0.025470340,   20.6, mmap-inline
Ubuntu 18.04.2 LTS, g++-8.2.0, Intel Core i7-7700K CPU @ 5GHz
---- Best times ----
seconds,    percent, method
0.133155952,  100.0, iostream
0.102128208,   76.7, scanf
0.082469185,   61.9, scanf-multi
0.048661004,   36.5, getchar-inline
0.025320109,   19.0, mmap-inline
CentOS release 6.10, g++-6.3.0, Intel Core i7-4790 CPU @ 3.6GHz
---- Best times ----
seconds,    percent, method
0.167985515,  100.0, getchar-inline
0.147258495,   87.7, scanf
0.137161991,   81.7, iostream
0.118859546,   70.8, scanf-multi
0.034033769,   20.3, mmap-inline

Notes

  • mmap is a relatively expensive operation so that it may only be faster for relatively large files. Benchmark on your file size.
  • getchar-inline and mmap-inline use a similar inline method of parsing an int from a stream of char, so that the time difference can be attributed to the latency of accessing the next char. mmap method maps the entire file into memory and reads it sequentially, which is the best case scenario for the CPU memory prefetcher.

parse-integers-benchmark's People

Contributors

max0x7ba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.