Skip to content

Instantly share code, notes, and snippets.

View benhoyt's full-sized avatar

Ben Hoyt benhoyt

View GitHub Profile
@benhoyt
benhoyt / goversions.txt
Created April 13, 2024 04:54
Go version performance - table of results
| Go version | binary size (MB) | countwords (s) | sumloop (s) |
| ---------- | ---------------- | -------------- | ----------- |
| 1.0 | 3.27 | 8.20 | 9.47 |
| 1.1 | 3.95 | 7.57 | 9.54 |
| 1.2 | 5.50 | 25.51 | 9.36 |
| 1.3 | 4.27 | 2.85 | 1.99 |
| 1.4 | 4.32 | 3.03 | 2.13 |
| 1.5 | 4.73 | 2.16 | 1.16 |
| 1.6 | 4.70 | 2.15 | 1.18 |
| 1.7 | 3.94 | 1.86 | 0.63 |
@benhoyt
benhoyt / goversions.py
Created April 13, 2024 04:53
Measure Go version performance with GoAWK
import csv
import os
import subprocess
import time
print('| Go version | binary size (MB) | countwords (s) | sumloop (s) |')
print('| ---------- | ---------------- | -------------- | ----------- |')
@benhoyt
benhoyt / scan.go
Created December 20, 2023 08:34
Proposed Rows.ScanRow implementation (cut-down version)
package model
import (
"database/sql"
"errors"
"fmt"
"reflect"
"sync"
)
@benhoyt
benhoyt / markov.py
Created November 11, 2023 15:45
Generate text from an input using a simple Markov chain generator
import collections, random, sys, textwrap
# Build possibles table indexed by pair of prefix words (w1, w2)
w1 = w2 = ''
possibles = collections.defaultdict(list)
for line in sys.stdin:
for word in line.split():
possibles[w1, w2].append(word)
w1, w2 = w2, word
@benhoyt
benhoyt / multiply.go
Created September 19, 2023 20:11
Tiny Go web server to multiply two integers
package main
import (
"fmt"
"net/http"
"strconv"
)
func main() {
http.HandleFunc("/multiply", func(w http.ResponseWriter, r *http.Request) {
@benhoyt
benhoyt / csvify.awk
Last active January 23, 2023 08:54
csvify: try to parse CSV with regular AWK
BEGIN {
FS = ","
}
{
nf = csvify(fields)
for (i=1; i<=nf; i++) {
printf "|%s|\n", fields[i]
}
@benhoyt
benhoyt / glob.go
Created June 8, 2022 01:25
Simple glob matcher in Go
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
if len(os.Args) != 2 {
@benhoyt
benhoyt / gist:66e33fcce9094cc11ba7e2d10bfd7657
Created May 1, 2022 22:04
Go switch jump tables - before and after performance using GoAWK
# 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 December 16, 2021 02:22
See how fast we can make direct-threaded code in C (using computed goto)
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 September 23, 2021 00:12
Benchmark of three ways to do optional fields in Go structs
package client
func IntPtr(n int) *int { return &n }
type FooArgsPtr struct {
UserID *int
User string
GroupID *int
Group string
}