Skip to content

Instantly share code, notes, and snippets.

@suzaku
suzaku / test_sync
Last active December 21, 2015 04:09
O_DSYNC vs. O_SYNC vs. Call fsync manually vs. no sync
In [19]: fd = os.open('t1', os.O_CREAT | os.O_DSYNC | os.O_WRONLY)
In [20]: %timeit os.write(fd, 'a')
1000 loops, best of 3: 232 us per loop
In [21]: fd2 = os.open('t2', os.O_CREAT | os.O_SYNC | os.O_WRONLY)
In [22]: %timeit os.write(fd2, 'a')
1000 loops, best of 3: 737 us per loop
@suzaku
suzaku / system.py
Created August 25, 2013 07:48
system in Python
import os
import sys
def my_system(command):
pid = os.fork()
if pid == -1:
return -1
elif pid == 0:
os.execv('/bin/sh', ['/bin/sh', '-c', command])
@suzaku
suzaku / readme.md
Last active December 22, 2015 13:58
string concatenation performance experiment

method1-6 are taken from this article

method7 is added because I believe using generator expression would be faster

to run this experiment, you'll need to install line_profiler, memory_profiler and psutil first, all of which are just one pip install away.

after installing these profiling tools, we can run stest.py like this:

kernprof.py -l -v stest.py method1
@suzaku
suzaku / clean_movies.js
Created May 2, 2014 23:34
豆瓣电影首页[正在热映]中去掉分数低于7的条目
(function () {
var hot_movies = document.querySelectorAll('.ui-slide-item');
[].forEach.call(
hot_movies,
function (e) {
var rate = e.getAttribute('data-rate');
rate = parseInt(rate, 10);
if (!isNaN(rate) && rate < 7) {
e.remove();
}
[etcd] Jul 31 03:41:53.475 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:42:24.976 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:44:00.477 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:44:32.225 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:45:36.475 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:46:10.975 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:46:43.976 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:47:12.563 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:47:16.475 INFO | 20634ec121f9479ca7a6cd50a5efcc0b:
@suzaku
suzaku / gist:76c8f47b52e5229e56d7
Created January 3, 2015 01:58
`mtr -u --report -c 10 douban.com` from HK
$ mtr -u --report -c 10 douban.com
Start: Sat Jan 3 09:57:13 2015
HOST: diablo Loss% Snt Last Avg Best Wrst StDev
1.|-- 10.255.254.34 0.0% 10 0.4 0.4 0.3 0.6 0.0
2.|-- 203.90.233.1 0.0% 10 1.0 1.3 0.7 4.8 1.1
3.|-- 202.97.122.101 0.0% 10 4.8 3.5 1.7 5.4 1.2
4.|-- 202.97.61.37 0.0% 10 39.9 39.0 37.5 40.9 0.8
5.|-- 202.97.53.233 0.0% 10 38.9 39.7 38.5 40.9 0.5
6.|-- 202.97.53.93 0.0% 10 37.6 39.9 37.6 42.5 1.7
7.|-- ??? 100.0 10 0.0 0.0 0.0 0.0 0.0
import random
import datetime as dt
import requests
from pyquery import PyQuery as pq
s = requests.Session()
d = {'form_email': 'xxx', 'form_password': 'yyy'}
today = dt.datetime.today()
login_url = 'https://accounts.douban.com/login'
@suzaku
suzaku / gist:8be8250980e9da7e18cf
Created May 26, 2015 08:15
Simulation of the Monty Hall problem
import random
def setup_doors():
doors = [0] * 3
doors[random.choice(range(3))] = 1
return doors
def select():
return random.choice(range(3))
s = "s = {:c}{}{:c}{:c}print(s.format(34, s, 34, 10))"
print(s.format(34, s, 34, 10))
In [74]: timeit.timeit(stmt='del dll[10000]', setup='from collections import deque;dll = deque(range(10**5))', number=100)
Out[74]: 0.002535041014198214
In [75]: timeit.timeit(stmt='del l[10000]', setup='l = list(range(10**5))', number=100)
Out[75]: 0.006009762000758201