Skip to content

Instantly share code, notes, and snippets.

(defcustom server-use-tcp nil
"If non-nil, use TCP sockets instead of local sockets."
:set #'(lambda (sym val)
(unless (featurep 'make-network-process '(:family local))
(setq val t)
(unless load-in-progress
(message "Local sockets unsupported, using TCP sockets")))
(when val (random t))
(set-default sym val))
:group 'server
@zed
zed / join_vs_plus.py
Created November 6, 2010 15:03
time performance: string.join vs. string.__add__
#!/usr/bin/env python
from profilestats import profile # $ pip install profilestats
def join_join(a, b):
return ','.join([a, b])
def join_plus(a, b):
return a + ',' + b
@profile
@zed
zed / override_instance_method.py
Created November 23, 2010 22:20
Example on how how to override instance method in Python.
#!/usr/bin/env python
"""Example on how how to override instance method in Python.
See http://stackoverflow.com/q/4243586
"""
import types
from operator import methodcaller
class Spam:
@zed
zed / parallel_stdout_processes.py
Created November 27, 2010 01:49
Run all processes in parallel. Group output by process groups and order within groups
#!/usr/bin/env python
"""Run all processes in parallel.
Group output by process groups and order within groups
"""
import shlex
import subprocess
import sys
import threading
@zed
zed / .gitignore
Created November 28, 2010 15:38
Functional approach to Sieve of Eratosthenes in Python.
__pycache__/
*.pyc
*.out
@zed
zed / config
Created December 2, 2010 21:32
[svn-remote "svn"] section of a repo created with git `svn clone --stdlayout ...`
[svn-remote "svn"]
url = https://svn.example.com
fetch = path/to/your_project/trunk:refs/remotes/trunk
branches = path/to/your_project/branches/*:refs/remotes/*
tags = path/to/your_project/tags/*:refs/remotes/tags/*
@zed
zed / iter_gcd.py
Created December 8, 2010 17:04
test function call overhead in Python
# http://stackoverflow.com/questions/4305518/why-is-equivalent-python-code-so-much-slower/4381575#4381575
# iter_gcd -- Python 2.x version, with gcd function call
# Python 3.x version uses range instead of xrange
from sys import argv,stderr
def gcd(m, n):
if n > m:
m, n = n, m
while n != 0:
rem = m % n
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
42.87 5.17 5.17 4201 0.00 0.00 PyEval_EvalFrameEx
18.74 7.43 2.26 82071657 0.00 0.00 long_divrem
5.72 8.12 0.69 111023712 0.00 0.00 PyObject_Malloc
5.22 8.75 0.63 82071657 0.00 0.00 l_divmod
4.56 9.30 0.55 94120476 0.00 0.00 binary_op1
@zed
zed / gist:735259
Created December 9, 2010 20:13
software/hardware versions
$ python -V
Python 2.6.6
$ pypy -V
Python 2.5.2 (79529, Nov 25 2010, 23:04:06)
[PyPy 1.4.0]
$ jython -V
Jython 2.5.1
@zed
zed / dp.py
Last active January 19, 2022 00:24
Find height, width of the largest rectangle containing all 0's in the matrix
#!/usr/bin/env python
"""Find height, width of the largest rectangle containing all 0's in the matrix.
The algorithm for `max_size()` is suggested by @j_random_hacker [1].
The algorithm for `max_rectangle_size()` is from [2].
The Python implementation [3] is dual licensed under CC BY-SA 3.0
and ISC license.
[1]: http://stackoverflow.com/questions/2478447/find-largest-rectangle-containing-only-zeros-in-an-nn-binary-matrix#comment5169734_4671342