Skip to content

Instantly share code, notes, and snippets.

@temoto
temoto / Benchmark output
Created October 2, 2012 18:58
Go index (for range) micro benchmark
BenchmarkStringIndex1First 100000000 20.0 ns/op
BenchmarkStringIndex2First 100000000 19.8 ns/op
BenchmarkStringIndex1Last 50000000 59.4 ns/op
BenchmarkStringIndex2Last 50000000 45.7 ns/op
ok indexbench 9.413s
@temoto
temoto / gist:3745854
Created September 18, 2012 20:58
pacaur -S bup-git failure
$ pacaur -S --needed bup-git
:: Package(s) bup-git not found in repositories, trying AUR...
:: pylibacl is available in community
:: python2-pyxattr is available in community
AUR Targets (1): bup-git
Proceed with installation? [Y/n]
:: Edit bup-git PKGBUILD? [Y/n] n
@temoto
temoto / cp.go
Created September 17, 2012 21:35
cp benchmarks
package main
import (
"flag"
"fmt"
"io"
"os"
"path"
"runtime"
)
$ echo "pAkH5a9ubW3Etf8SDO548F6ZjuMKNc0I4Cia1lonDv0.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTM0NzkxMTYxNywicGFnZSI6eyJpZCI6IjI4NzQzNTk4MTM2OTUwNyIsImxpa2VkIjpmYWxzZSwiYWRtaW4iOnRydWV9LCJ1c2VyIjp7ImNvdW50cnkiOiJkZSIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MTMsIm1heCI6MTd9fX0" |cut -d. -f2 |base64 -d |json_pp
base64: invalid input
{
"algorithm" : "HMAC-SHA256",
"page" : {
"admin" : true,
"liked" : false,
"id" : "287435981369507"
},
"user" : {
@temoto
temoto / cpu-perf-test.cxx
Created September 3, 2012 20:07
Various elementary CPU operations benchmark
# gcc -O3 -o cpu-perf-test -lrt cpu-perf-test.cxx && (for x in {1..7}; do sleep 0.3s; ./cpu-perf-test; done) && rm cpu-perf-test
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
//const char *op_tag = "id "; long op(long x) { return x; }
const char *op_tag = "*2 "; long op(long x) { return x * 2; }
//const char *op_tag = "<<1"; long op(long x) { return x << 1; }
//const char *op_tag = "*27"; long op(long x) { return x * 27; }
@temoto
temoto / test015.py
Created August 26, 2012 14:10
gevent reraise exception
import gevent
#...
def test_reraise_01():
try:
gevent.spawn(lambda: 1/0).join()
assert False, "ZeroDivisionError excepted"
except ZeroDivisionError:
pass
@temoto
temoto / gist:3397886
Created August 19, 2012 21:36
Linux loop device problem
# mount -t iso9660 -o loop,ro,noexec ~temoto/Downloads/archlinux-2012.06-1-archboot-x86_64.iso /tmp/arch/
mount: /tmp/arch/: mount failed: No such file or directory
$ ls -l ~/Downloads/archlinux-2012.06-1-archboot-x86_64.iso
-rw-r--r-- 1 temoto users 375390208 Aug 20 00:39 /home/temoto/Downloads/archlinux-2012.06-1-archboot-x86_64.iso
$ ls -ld /tmp/arch
drwxr-xr-x 2 root root 4096 Aug 20 01:10 /tmp/arch
@temoto
temoto / sqlalchemy_postgresql_union_fix.py
Created July 5, 2012 20:43
SQLAlchemy PostgreSQL UNION with ORDER/LIMIT monkey patch fix
# Problem: sa.union_all(*qs).order_by('y') will generate invalid SQL like this:
# SELECT ... ORDER BY x LIMIT 10 UNION ALL SELECT ... ORDER BY x LIMIT 10 ORDER BY y
# Order in inner queries could be required for LIMIT or DISTINCT ON (field).
#
# Solution: sqlalchemy should put all inner queries that contain order by or limit
# clauses into parenthesis, like this:
# SELECT ... UNION ALL (SELECT ... ORDER BY x LIMIT 10) ORDER BY y
#
# sqlalchemy.sql.compiler.SQLCompiler.visit_compound_select
# Fixed to surround subqueries with parens if they contain ORDER BY or LIMIT or OFFSET.
@temoto
temoto / pg-transaction.py
Created June 26, 2012 15:55
[Python] psycopg2 'execute block of code in transaction' context manager
@contextmanager
def transaction(pool='common_write'):
"""Context manager.
Executes block inside DB transaction. Returns cursor.
At the end of the block, the connection is returned to pool.
>>> with transaction() as cursor:
... rows = cursor.execute(...).fetchall()
... process(rows)
... cursor.execute(...)
@temoto
temoto / main.go
Created June 26, 2012 06:04
Eve Online Market Data Relay
package main
import (
cache "code.google.com/p/vitess/go/cache"
"fmt"
zmq "github.com/alecthomas/gozmq"
"hash"
"hash/fnv"
"os"
"time"