Skip to content

Instantly share code, notes, and snippets.

@rianhunter
rianhunter / test_sem.c
Created September 1, 2019 18:44
Shows proper usage of sem_init()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <semaphore.h>
#include <time.h>
#include <unistd.h>
@rianhunter
rianhunter / hemiola.py
Last active September 29, 2016 00:40
Hemiola
import asyncio
import anushri
OUTPUT_CLIENT = 20
BPM = 70
BEAT_LENGTH = 60 / BPM
WHOLE_LENGTH = BEAT_LENGTH * 4
NOTE_16 = WHOLE_LENGTH / 16
@rianhunter
rianhunter / unintuitive_sqlite_behavior.py
Created March 23, 2016 06:40
Code that unintuitively generates a "database is locked" error with the Python sqlite module
#!/usr/bin/env python3
# This code demonstrates a code sequence that will cause the Python sqlite
# to immediately generate a "database is locked" exception, even with a large
# timeout
import sqlite3
import time
import threading
@rianhunter
rianhunter / random-read.sh
Created January 18, 2016 18:16
Cron job to read randomly from disk
#!/bin/sh
for a in /dev/sd*1
do
SECTORS=$(/sbin/blockdev --getsize $a)
SECTOR=$(python -c "import random; print random.randint(0, $SECTORS - 1);")
dd if=$a of=/dev/null iflag=direct skip=${SECTOR} bs=512 count=1 2>/dev/null &
done
wait
@rianhunter
rianhunter / pyudev_and_urwid.py
Created September 24, 2015 04:17
Using pyudev asynchronous monitoring with urwid
import queue
import os
import pyudev
import urwid
text = urwid.Text("")
loop = urwid.MainLoop(urwid.Filler(text))
inter_thread_queue = queue.Queue()
@rianhunter
rianhunter / frobable.hpp
Last active August 29, 2015 14:27
Translation of Go's implementation of interfaces to C++11
// this is the header that does all the heavy-lifting
// NB: lots of boilerplate
// runtime function table
struct FrobableTable {
int (*frob)(const void *ptr);
};
// forward declaration of the implementation
template<class FrobableImplType>
@rianhunter
rianhunter / call_overhead.c
Last active December 21, 2023 23:25
Indirect vs Direct Function Call Overhead in C/C++
/*
This benchmark shows how indirect function calls have nearly
the same overhead costs as direct function calls.
This is comparing apples to apples, factoring out the savings
due to inlining optimizations that direct calls usually afford.
From this, it seems that inlining and other generic interprocedual
optimizations are the main drivers of direct function call optimization,
not the direct call itself.
@rianhunter
rianhunter / test.cc
Created March 29, 2014 19:54
C++ compilers can manipulate and create string literals at compile time
#include <type_traits>
#include <cstring>
#include <cstdio>
template <size_t N>
class StaticallySizedString {
char _str[N + 1];
public: