Skip to content

Instantly share code, notes, and snippets.

View ilyapopov's full-sized avatar

Ilya Popov ilyapopov

View GitHub Profile

A common task is to print numbers using unit prefixes. In computing it is often sizes in bytes (such as "743 kB"), but also other quantities in science, engineering, etc, (such as "1.34 ns"). Therefore, I believe it will be beneficial to the whole community if facilities for formatting numbers with unit prefixes would be included in fmtlib and maybe even in the standard.

Hereafter I describe how do I see this feature:

  • Add new formats for integer and floating point numbers, that would add a unit prefix after the number (the unit itself, such as "B", would has to be included into the format string outside and just after of the "{}" placeholder). The usage would be like:
    fmt::print("{:.3?}B\n", 765432); // would produce "765.432 kB"
    fmt::print("{:.2?}s\n", 1.342784e-9); // would produce "1.34 ns"
  • Support three options: decimal prefixes (1024 -> "1.024 k"), binary prefixes with "i" (1024 -> "1.000 Ki") and binary prefixes without "i" (1024 -> "1.000 k")
/home/ipopov/devel/tmp/cppinclude/src/application/app_con.hpp should add these lines:
#include <memory> // for unique_ptr
#include <vector> // for vector
#include "json/ih/json_accessor.hpp" // for JsonAccessor
#include "model_includes/api/mi_analyzer.hpp" // for Analyzer
#include "model_includes/api/mi_model.hpp" // for Model
#include "model_includes/ih/mi_accessor.hpp" // for ModelIncludesAccessor
#include "parser/api/pr_parser.hpp" // for Parser
#include "parser/ih/pr_accessor.hpp" // for ParserAccessor
#include "project/api/prj_project.hpp" // for Project
Running target/release/deps/compare-0a5da1ab8ac70cbd
nth_element/floydrivest/1000
time: [6.9318 us 7.2481 us 7.5652 us]
change: [-5.7428% -0.6265% +5.1104%] (p = 0.82 > 0.05)
No change in performance detected.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) low mild
nth_element/order_stat/1000
time: [2.1336 us 2.2280 us 2.3263 us]
change: [+0.1353% +4.4454% +9.0469%] (p = 0.05 > 0.05)
#include <vector>
#include <chrono>
#include <iostream>
#include <curl/curl.h>
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
std::vector<char> *response = reinterpret_cast<std::vector<char> *>(userdata);
response->insert(response->end(), ptr, ptr+nmemb);
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>
int max_iostream(std::istream & f)
{
int x;
int max = -1;