Skip to content

Instantly share code, notes, and snippets.

View benwaffle's full-sized avatar
🍝
best spaghetti in town

Ben Iofel benwaffle

🍝
best spaghetti in town
View GitHub Profile
@benwaffle
benwaffle / Makefile
Created August 1, 2014 20:42
rdtsc demo
all:
gcc -std=c99 -O1 main.c
@benwaffle
benwaffle / main.c
Created August 18, 2014 01:08
clock drift
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
inline uint64_t rdtsc() {
uint32_t low, high;
__asm__ __volatile__("rdtsc" : "=a"(low), "=d"(high));
return (uint64_t)low | ((uint64_t)high << 32);
}
@benwaffle
benwaffle / Makefile
Last active August 29, 2015 14:05
view a stack trace in C
CFLAGS=-g
LDFLAGS=-lunwind -lunwind-x86_64
all: stacktrace
@benwaffle
benwaffle / Makefile
Created September 9, 2014 18:01
boot and display a string
all:
nasm boot.asm -f bin -o boot.bin
@benwaffle
benwaffle / prob_benchmark.h
Last active August 29, 2015 14:07 — forked from Prince781/prob_benchmark.h
benchmark flip() and flop()
#ifndef _PROB_BENCHMARK_H_
#define _PROB_BENCHMARK_H_
#define _GNU_SOURCE
#include <stdio.h>
#include <time.h>
#define KNRM "\x1B[0m" // normal console color
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
@benwaffle
benwaffle / Makefile
Last active August 29, 2015 14:09
print ascending numbers without my own state variables
LDFLAGS=-lunwind -lunwind-x86_64
all: asc
@benwaffle
benwaffle / JS.java
Created November 26, 2014 05:35
tiny nashorn (javascript) interpreter
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JS {
public static void main(String[] args) {
ScriptEngine se = new ScriptEngineManager().getEngineByName("nashorn");
@benwaffle
benwaffle / Main.scala
Last active August 29, 2015 14:11
complex wsdl types to scala
import scala.xml.XML
object Main extends App {
def getType(w: String) = w dropWhile ( _ != ':' ) drop(1) capitalize
val xml = XML.loadFile("Sample.wsdl")
val types = (xml \ "types" \ "schema" \ "complexType")
types map { complextype =>
println("case class %s(".format(complextype.attribute("name").get))
@benwaffle
benwaffle / demo.asm
Created December 30, 2014 05:27
complex program
section .data
msg db "hello world", 0x0
section .text
extern gtk_init
extern gtk_widget_show_all
extern gtk_window_new
extern gtk_window_resize
extern gtk_container_add
extern gtk_label_new
@benwaffle
benwaffle / Memoize.java
Last active August 29, 2015 14:12
memoize a java function
import java.util.*;
import java.util.function.Function;
public class Memoize {
static interface F<T,R> extends Function<T,R> {}
// not thread safe
static <T,R> F<T, R> memoize(F<T, R> f) {
Map<T, R> cache = new HashMap<>();
return (x) -> {