Skip to content

Instantly share code, notes, and snippets.

View nthery's full-sized avatar

Nicolas Thery nthery

  • Grenoble - France
View GitHub Profile
@nthery
nthery / strip_c_comments.c
Last active January 10, 2020 12:31
filter out traditional C comments
/*
* Filter out traditional C comments.
*
* C++ comments are not filtered out.
*/
#include <stdio.h>
enum States {
WAITING_FOR_OPENING_SLASH,
@nthery
nthery / string_copy_vs_move.cpp
Last active September 30, 2020 17:43
benchmark string copy vs move ctor
#include <benchmark/benchmark.h>
#include <string>
static void Copy(benchmark::State& state) {
// Code inside this loop is measured repeatedly
for (auto _ : state) {
std::string src("a very long string to defeat small string optimization");
std::string dst{src};
benchmark::DoNotOptimize(dst);
}
@nthery
nthery / std_set_vs_flat_set.cpp
Created October 19, 2020 08:11
compare performance of std::set and boost::container::flat_set
// clang++ -std=c++17 -Wall -Wextra -O2 -lbenchmark flat_set_vs_std_set.cpp
/*
Run on (4 X 2300 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x2)
L1 Instruction 32 KiB (x2)
L2 Unified 256 KiB (x2)
L3 Unified 4096 KiB (x1)
Load Average: 2.59, 5.88, 5.91
@nthery
nthery / std_function_variance.cpp
Created November 12, 2020 06:13
covariance and contravariance in std::function
// Covariance and contra-variance in std::function
// See
// https://quuxplusone.github.io/blog/2019/01/20/covariance-and-contravariance/
#include <functional>
struct B {};
struct D : B {};
// std::function is covariant on return types.
@nthery
nthery / rooibos.cpp
Last active December 14, 2020 18:32
tee-like utility demonstrating usage of WIN32 pipes
// tee-like utility demonstrating usage of pipes.
//
// This program was originally intended to demonstrate overlapped i/o
// and WaitForMultipleObjects() but this proved to be quite complex and
// there is no need to wait concurrently on the pipe and child process
// as the child first emits its output (in the pipe) *then* terminates.
//
// The following command:
// rooibos foo.out foo bar
// is equivalent to:
@nthery
nthery / @.txt
Last active January 20, 2021 00:24
For your eyes only
May the Source be with you.
@nthery
nthery / checksum.cpp
Created January 13, 2021 11:18
quick-and-dirty program to compare performance of stdio and mmap reads
/*
* Quick-and-dirty program to compare performance of stdio vs mmap i/o.
*/
#ifdef NDEBUG
#undef NDEBUG // assert used for error handling
#endif
#ifdef __linux__
@nthery
nthery / heterogeneous_lookup.cpp
Last active January 25, 2021 15:40
C++14 allows to lookup into a map or set without creating a temporary key when the provided needle is convertible to the key
// See https://www.bfilipek.com/2019/05/heterogeneous-lookup-cpp14.html
#include <map>
#include <string>
std::map<std::string, int> m1;
int f1() {
// Constructs a temporary string for key.
auto it = m1.find("foo");
@nthery
nthery / gotac.go
Created February 14, 2021 08:07
Gotac prints lines from standard input or files in command-line in reverse order
// Gotac prints lines from standard input or files in command-line in reverse order.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
@nthery
nthery / init_vs_assign.cpp
Created February 14, 2021 10:18
Show difference between initialization and assignment when calling function that returns a user-defined type.
// Show difference between initialization and assignment when calling function
// that returns a user-defined type.
struct S {
S();
~S();
S(const S&);
S(S&&);
S& operator=(const S&);
S& operator=(S&&);