Skip to content

Instantly share code, notes, and snippets.

Ben Hoyt benhoyt

Block or report user

Report or block benhoyt

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@benhoyt
benhoyt / gist:2647005
Created May 9, 2012
Speed of list comprehension vs for loop with append
View gist:2647005
# 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 Oct 11, 2012
Bouncing ball with rotation
View gist:3870305
"""Simple bouncing ball demo."""
import sys
import pygame
pygame.init()
size = (1024, 768)
speed = [1, 1]
@benhoyt
benhoyt / gist:4044946
Created Nov 9, 2012
Speed up os.walk() significantly by using file attributes from FindFirst/Next or readdir
View gist:4044946
"""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 / generate_key.py
Created Jul 28, 2016
Python function to generate a random string (key) of length chars
View generate_key.py
"""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 Jun 27, 2017
Add COMPARE_IS_NONE opcode to CPython for performance
View is_none_bytecode.diff
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 Oct 28, 2017
Speed test of tiny virtual dom and merge algorithm
View tiny-vdom-speed.html
<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 / sliding_window_sort.py
Created Feb 22, 2017
Efficient sliding-window sorting of time-series data in CSV file (in Python)
View sliding_window_sort.py
"""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 / test_dom.html
Created Oct 25, 2017
Test speed of various methods of building DOM
View test_dom.html
<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 Nov 29, 2017
Simple HTTP server with regex-based router in Go
View server.go
// Simple HTTP server with regex-based router
package main
import (
"fmt"
"net/http"
"regexp"
)
@benhoyt
benhoyt / connect4.go
Last active Jan 25, 2018
Connect 4 solver
View connect4.go
// A little "Connect Four" game
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
You can’t perform that action at this time.