Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Hoyt benhoyt

View GitHub Profile
@benhoyt
benhoyt / glob.go
Created Jun 8, 2022
Simple glob matcher in Go
View glob.go
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
if len(os.Args) != 2 {
@benhoyt
benhoyt / gist:66e33fcce9094cc11ba7e2d10bfd7657
Created May 1, 2022
Go switch jump tables - before and after performance using GoAWK
View gist:66e33fcce9094cc11ba7e2d10bfd7657
# NOTES
"before" is Go version go1.19-dd97871282, before the switch jump tables commits
"after" is Go version go1.19-78bea702cd, after the switch jump tables commits
# GOAWK'S GO MICROBENCHMARKS
$ benchstat -sort=delta -geomean benchmarks_before.txt benchmarks_after.txt
name old time/op new time/op delta
IncrDecr-8 141ns ± 1% 160ns ± 2% +13.59% (p=0.008 n=5+5)
IfStatement-8 147ns ± 1% 157ns ± 2% +6.59% (p=0.008 n=5+5)
@benhoyt
benhoyt / repeat-while.diff
Last active Dec 16, 2021
See how fast we can make direct-threaded code in C (using computed goto)
View repeat-while.diff
static void* prog[] = {
- // loop:
&&i_pushvar0, // pushvar i
&&i_pushnum, (void*)100000000, // pushnum 100000000
&&i_jge, (void*)5, // jge end
+ // loop:
&&i_pushvar0, // push i
&&i_addvar1, // addvar s
&&i_incvar0, // incvar i
- &&i_jmp, (void*)((long long)-10), // jmp loop
@benhoyt
benhoyt / client.go
Created Sep 23, 2021
Benchmark of three ways to do optional fields in Go structs
View client.go
package client
func IntPtr(n int) *int { return &n }
type FooArgsPtr struct {
UserID *int
User string
GroupID *int
Group string
}
@benhoyt
benhoyt / mt.py
Created Sep 21, 2021
Quick performance test of Python 3.10's "match" vs "if...elif"
View mt.py
"""Quick performance tests comparing "match" with "if...elif".
See:
https://benhoyt.com/writings/python-pattern-matching/
https://news.ycombinator.com/item?id=28601616
# Enum switch with match:
$ python3.10 -m timeit -s 'import mt' -c 'mt.enum_match(mt.FileType.FILE)'
1000000 loops, best of 5: 356 nsec per loop
$ python3.10 -m timeit -s 'import mt' -c 'mt.enum_match(mt.FileType.SYMLINK)'
@benhoyt
benhoyt / countwords.fs
Created Mar 12, 2021
Forth: print frequencies of unique words in stdin, most frequent first
View countwords.fs
200 constant max-line
create line max-line allot \ Buffer for read-line
wordlist constant counts \ Hash table of words to count
variable num-uniques 0 num-uniques !
\ Allocate space for new string and copy bytes, return new string.
: copy-string ( addr u -- addr' u )
dup >r dup allocate throw
dup >r swap move r> r> ;
@benhoyt
benhoyt / markdown.diff
Created Oct 7, 2020
Diff to override goldmark's code block output
View markdown.diff
diff --git a/internal/markdown/markdown.go b/internal/markdown/markdown.go
index a729b9f..94d29c1 100644
--- a/internal/markdown/markdown.go
+++ b/internal/markdown/markdown.go
@@ -13,8 +13,11 @@ import (
"bytes"
"github.com/yuin/goldmark"
+ "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
@benhoyt
benhoyt / flattenjson.go
Created Jul 7, 2020
Little Go tool to flatten JSON input
View flattenjson.go
// 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
@benhoyt
benhoyt / python-stdlib.md
Created Oct 17, 2019
Overview of (parts of) the Python standard library
View python-stdlib.md

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 / join.awk
Created Nov 20, 2018
AWK program to compare time complexity of joining strings
View join.awk
# 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]