Skip to content

Instantly share code, notes, and snippets.

View benhoyt's full-sized avatar

Ben Hoyt benhoyt

View GitHub Profile
@benhoyt
benhoyt / is_none_bytecode.diff
Created June 27, 2017 20:56
Add COMPARE_IS_NONE opcode to CPython for performance
b66bbc41ce52efe667af0ba47a6098216b758236
diff --git a/Include/opcode.h b/Include/opcode.h
index 99c3b0ef81..dceedc662a 100644
--- a/Include/opcode.h
+++ b/Include/opcode.h
@@ -12,6 +12,8 @@ extern "C" {
#define ROT_THREE 3
#define DUP_TOP 4
#define DUP_TOP_TWO 5
+#define COMPARE_IS_NONE 6
@benhoyt
benhoyt / test_dom.html
Created October 25, 2017 01:07
Test speed of various methods of building DOM
<html>
<head>
<title>Test speed of various methods of building DOM</title>
</head>
<body>
Test speed of various methods of building DOM
</body>
<script>
@benhoyt
benhoyt / tiny-vdom-speed.html
Created October 28, 2017 18:17
Speed test of tiny virtual dom and merge algorithm
<html>
<head><title>More speed tests</title></head>
<body>
<h1>More speed tests</h1>
<div id="main"></div>
<script>
/*
<ul>
{% for item in items %}
<li>item: {{ item }}</li>
@benhoyt
benhoyt / server.go
Last active November 29, 2017 18:55
Simple HTTP server with regex-based router in Go
// Simple HTTP server with regex-based router
package main
import (
"fmt"
"net/http"
"regexp"
)
@benhoyt
benhoyt / connect4.go
Last active October 26, 2023 22:40
Connect 4 solver
// A little "Connect Four" game
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
@benhoyt
benhoyt / edit_distance.py
Created May 8, 2018 14:06
Calculate edit distance with simple (memoized) recursive algorithm
def distance(s, t, cache=None):
"""Return minimum edit distance between s and t, where an edit
is a character substitution, deletion, or addition.
"""
if not s:
return len(t)
if not t:
return len(s)
if cache is None:
@benhoyt
benhoyt / mandelbrot.go
Created September 21, 2018 01:07
Go program to print the Mandelbrot set on stdout
// Print the Mandelbrot set on stdout
package main
import (
"fmt"
"math/cmplx"
)
const (
@benhoyt
benhoyt / join.awk
Created November 20, 2018 00:59
AWK program to compare time complexity of joining strings
# AWK program to compare time complexity of joining strings using a
# simple O(N^2) algorithm and a slightly more complex O(N log N) one.
# Join array elements, separated by sep: O(N^2) version
function join1(a, sep, i, s) {
for (i = 1; i+1 in a; i++) {
s = s a[i] sep
}
if (i in a) {
s = s a[i]
@benhoyt
benhoyt / python-stdlib.md
Created October 17, 2019 00:08
Overview of (parts of) the Python standard library

I'm going to demo a bunch of Python builtin and stdlib functions. There's a lot to get through, so I'll be going fast, but please stop me and ask questions as we go. The goal is to give you a taste of Python's power and expressivity if you're not a Python person, or maybe teach you a few new tricks if you are already.

Built-in functions

# enumerate: iterate with index *and* item
>>> strings = ['123', '0', 'x']
>>> for i, s in enumerate(strings):
...     print(f'{i} - {s}')  # f-strings!
@benhoyt
benhoyt / flattenjson.go
Created July 7, 2020 05:06
Little Go tool to flatten JSON input
// Flatten JSON input
//
// Example:
//
// $ echo '{"user":"Ben","ints":[true,false,null],"sub":{"x":1,"y":2}}' | go run flattenjson.go
// _.ints[0] = true
// _.ints[1] = false
// _.ints[2] = null
// _.sub.x = 1
// _.sub.y = 2