Skip to content

Instantly share code, notes, and snippets.

@zed
zed / gist:779350
Created January 14, 2011 08:08
`while True` vs. `while 1` on Python 2.x
>>> def loop():
... total = 0
... while 1:
... total += 1
...
>>> import dis
>>> dis.dis(loop)
3 0 LOAD_CONST 1 (0)
3 STORE_FAST 0 (total)
@zed
zed / .gitignore
Created January 17, 2011 16:08
What is faster x**.5 or math.sqrt(x) in Python?
.tox/
/MANIFEST
@zed
zed / cymatmul.pyx
Created January 22, 2011 04:47
pip install cython && python test_matmul.py
# see http://conference.scipy.org/proceedings/SciPy2009/paper_2/
import threading
cimport cython
import numpy as np
cimport numpy as np
ctypedef np.float64_t dtype_t
def matmul(A, B, out=None):
@zed
zed / .gitignore
Created March 5, 2011 20:02
what code gcc generates for `x/2` and `x*.5`
*.o
@zed
zed / gist:875795
Created March 18, 2011 09:03
Time performance: `a.prod()` vs. `reduce(lambda x,y:x*y, a)`
In [1]: import numpy as np
In [2]: a = np.r_[.1:4:40j]
In [3]: a
Out[3]:
array([ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1,
1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2,
2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3,
3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. ])
@zed
zed / cydoctest.py
Created March 26, 2011 02:11
doctest support for cython
#!/usr/bin/env python
"""
Cython-compatible wrapper for doctest.testmod().
Usage example, assuming a Cython module mymod.pyx is compiled.
This is run from the command line, passing a command to Python:
python -c "import cydoctest, mymod; cydoctest.testmod(mymod)"
(This still won't let a Cython module run its own doctests
when called with "python mymod.py", but it's pretty close.
@zed
zed / demo.py
Created April 13, 2011 02:04
how to define an elementwise function in pycuda
#!/usr/bin/env python
import pycuda.gpuarray as gpuarray
import pycuda.autoinit
from pycuda.elementwise import ElementwiseKernel
# define elementwise `add()` function
add = ElementwiseKernel(
"float *a, float *b, float *c",
"c[i] = a[i] + b[i]",
@zed
zed / .gitignore
Created September 22, 2011 18:14
Build 1d array of unknown size in Cython
/build/
*.so
*.cpp
/cachegrind.out.profilestats
/profilestats.prof
/build1darray.s
@zed
zed / get-all-post.sh
Created September 29, 2011 18:27
Get all posts from del.icio.us
#!/bin/sh
#
# Get all posts from del.icio.us
# See http://www.delicious.com/help/api
#
USER=$1
OUT=posts-${USER}.xml
[ -z "$USER" ] && echo "USAGE: $0 USERNAME" && exit 2
@zed
zed / .gitignore
Created October 2, 2011 11:21
naive quicksort in Cython #qsort
#
/cachegrind.out.profilestats
/profilestats.prof