Skip to content

Instantly share code, notes, and snippets.

View benhoyt's full-sized avatar

Ben Hoyt benhoyt

View GitHub Profile
@benhoyt
benhoyt / atomic_counter.py
Created August 3, 2016 13:12
An atomic, thread-safe incrementing counter for Python
"""An atomic, thread-safe incrementing counter."""
import threading
class AtomicCounter:
"""An atomic, thread-safe incrementing counter.
>>> counter = AtomicCounter()
>>> counter.increment()
@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 / gist:4044946
Created November 9, 2012 09:59
Speed up os.walk() significantly by using file attributes from FindFirst/Next or readdir
"""Speed up os.walk() significantly by using file attributes that
FindFirst/Next give us instead of doing an extra stat(). Can also do the same
thing with opendir/readdir on Linux.
This is doubly useful when the user (caller of os.walk) is doing *another*
stat() to get say the file sizes.
On my tests (Windows 64-bit) our walk() is about 5x as fast as os.walk() for
large directory trees, and 9x as fast if you're doing the file size thing.
Note that these timings are "once it's in the cache", not first-time timings.
@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 / sliding_window_sort.py
Created February 22, 2017 20:49
Efficient sliding-window sorting of time-series data in CSV file (in Python)
"""Efficient sliding-window sorting of time-series data in CSV file.
Demo for http://stackoverflow.com/a/42398981/68707
Tested on Python 3.5.
"""
import collections
import csv
import datetime
@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 / 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 / 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]
}