Skip to content

Instantly share code, notes, and snippets.

View benhoyt's full-sized avatar

Ben Hoyt benhoyt

View GitHub Profile
@benhoyt
benhoyt / gist:2647005
Created May 9, 2012 17:32
Speed of list comprehension vs for loop with append
# On my Windows 7 64-bit machine running CPython 2.6
C:\>python -m timeit -s "lst = [1] * 100" "out = [x for x in lst if x]"
100000 loops, best of 3: 6.55 usec per loop
C:\>python -m timeit -s "lst = [1] * 100" "out = []" "for x in lst:" " if x:" " out.append(x)"
100000 loops, best of 3: 14.7 usec per loop
@benhoyt
benhoyt / gist:3870305
Created October 11, 2012 05:11
Bouncing ball with rotation
"""Simple bouncing ball demo."""
import sys
import pygame
pygame.init()
size = (1024, 768)
speed = [1, 1]
@benhoyt
benhoyt / generate_key.py
Created July 28, 2016 15:38
Python function to generate a random string (key) of length chars
"""Function to generate a random string (key) of length chars."""
import binascii
import os
def generate_key(length=40, get_bytes=os.urandom):
"""Return a randomly-generated key of length chars.
>>> len(generate_key())
@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 / 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 / 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 / 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 / snakes_and_ladders.py
Last active May 20, 2018 09:02
Calculate the average number of moves in a snakes and ladders game
"""Calculate the average number of moves in a snakes and ladders game.
Because as a parent one gets roped into these board (boring?) games
every so often, and I wanted to calculate the average duration of a
snakes and ladders game. Turns out it's about 36 moves (though
admittedly that's for a single-player game). :-)
> python snakes_and_ladders.py
Played 10000 rounds, averaged 36.0559 moves, max 324 moves, took 0.508s
"""
@benhoyt
benhoyt / thread_test.py
Created November 3, 2016 13:53
Test how many threads we can run at once
"""Test how many threads we can run at once."""
import itertools
import threading
import time
import sys
import requests
@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 (