This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import timeit | |
| import numpy | |
| import scipy.ndimage.morphology | |
| def f0(a, kernel): | |
| b = numpy.convolve(a, kernel, mode="same") > 1 | |
| b[1:] |= b[:-1] | |
| b[:-1] |= b[1:] | |
| return b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyRECache(dict): | |
| def __missing__(self, regex): | |
| return self.setdefault(regex, re.compile(regex)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In [1]: import numpy | |
| In [2]: a = numpy.random.randint(100, size=100) | |
| In [3]: ((25 < a) & (a < 100)).sum() | |
| Out[3]: 72 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> class A(object): | |
| ... i = 3 | |
| ... a = [i for x in range(3)] | |
| ... | |
| >>> class A(object): | |
| ... i = 3 | |
| ... a = list(i for x in range(3)) | |
| ... | |
| Traceback (most recent call last): | |
| File "<stdin>", line 1, in <module> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In [1]: a = array.array("i", range(100000)) | |
| In [2]: b = array.array("i", range(100000)) | |
| In [3]: %timeit for i, x in enumerate(a): b[i] = 2 * x | |
| 10 loops, best of 3: 20.6 ms per loop | |
| In [4]: a = range(100000) | |
| In [5]: b = range(100000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import timeit | |
| def inter_reduce(sets): | |
| return reduce(set.intersection, sets) | |
| def inter_for(sets): | |
| sets = iter(sets) | |
| result = next(sets) | |
| for s in sets: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def f(list_of_lists): | |
| sets = {} | |
| for lst in list_of_lists: | |
| s = set(lst) | |
| for x in set(lst): | |
| if x in sets: | |
| t = sets[x] | |
| for y in t: | |
| sets[y] = s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In [3]: consume = collections.deque(maxlen=0).extend | |
| In [4]: def f(a): | |
| ...: consume(x for x in a) | |
| ...: | |
| In [5]: def g(a): | |
| ...: consume(a) | |
| ...: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Tmimings on a Linux box, 32-bit kernel, 32-bit userland | |
| Python version: 2.7 3.1 | |
| -s"a=10**20;n=3" "(a>>n)&1" 0.224 usec per loop 0.161 usec per loop | |
| -s"a=10**20;n=3" "not not(a & (1 << n))" 0.155 usec per loop 0.183 usec per loop | |
| -s"a=10**200;n=3" "(a>>n)&1" 0.39 usec per loop 0.303 usec per loop | |
| -s"a=10**200;n=3" "not not(a & (1 << n))" 0.158 usec per loop 0.177 usec per loop | |
| -s"a=10**10;n=3" "(a>>n)&1" 0.223 usec per loop 0.153 usec per loop | |
| -s"a=10**10;n=3" "not not(a & (1 << n))" 0.158 usec per loop 0.18 usec per loop | |
| -s"a=10**9;n=3" "(a>>n)&1" 0.0734 usec per loop 0.152 usec per loop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <unistd.h> | |
| #include <stdio.h> | |
| int main(int argc, const char **argv) | |
| { | |
| char c[10240] = {0}; | |
| int fd; | |
| sscanf(argv[1] + 8, "%i", &fd); // skip "/dev/fd/" | |
| read(fd, c, 10239); |