Skip to content

Instantly share code, notes, and snippets.

@rklemme
rklemme / LazyCache.java
Created May 17, 2012 09:46
Lazy cache with as few locking as possible
package clj;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* The cache works with as few locking as possible. Lookup is done in two steps
* on cache miss:
* <ol>
* <li>On a cache miss a retriever is inserted into the cache which will obtain
@rklemme
rklemme / pathname-distribute.rb
Created April 22, 2012 12:15
Insert one character directory names to distribute files across sub directories. This can be used to avoid having too many files in a single directory.
require 'pathname'
class Pathname
# Insert directory parts for leading characters of the basename,
# an optional integer argument specifies the limit.
def distribute(levels = nil)
dir, basename = split
name, ext = basename.to_s.split /\./, 2
pn = dir
@rklemme
rklemme / find-duplicates.rb
Created April 22, 2012 11:08
Benchmark for determining duplicate positions
require 'benchmark'
REP = 100_000
dat = [1, 2, 3, 2, 4, 5, 4, 4]
Benchmark.bmbm 25 do |x|
x.report "Jan low level" do
REP.times do
dups = {}
@rklemme
rklemme / hash-count.rb
Created January 18, 2012 13:28
Couting occurrencies in an Enumerable
require 'benchmark'
# create some data
DAT = (3..7).map do |i|
Array.new(10 ** i) { rand 100 }
end
2.times do
Benchmark.bm 15 do |b|
DAT.each do |ar|
@rklemme
rklemme / BBCompactPerfTest.java
Created December 16, 2011 15:31
Microbench Test
package nio;
import java.nio.ByteBuffer;
import java.util.Random;
public final class BBCompactPerfTest {
private static final int SEQ = 1000000;
private static final int REP = 100;
@rklemme
rklemme / semaphore.rb
Created November 14, 2011 08:16
Simple semaphore implementation
require 'monitor'
# Simple semaphore implementation using Monitor and ConditionVariable
class Semaphore
def initialize(initial_value = 0)
@val = initial_value
@lock = Monitor.new
@available = ConditionVariable.new
end
@rklemme
rklemme / copy-trans.rb
Created October 28, 2011 08:37
Update newer files from one dir to another dir
#!/usr/bin/env ruby19
# Read a directory tree and convert newer sources
# on the fly.
require 'pathname'
EXTENSIONS=%w{.jpg .JPG}
@rklemme
rklemme / bytes-readable.sh
Created October 21, 2011 09:04
Convert the first argument (which must be only digits) to a scaled output with KB, MB or GB.
#!/bin/dash
# convert argument into human readable format
# argument must be numeric and without sign.
num=$1
expr "$num" : '[0-9]\+$' >/dev/null || { echo "ERROR: must be digits only: '$num'">&2; exit 1;}
scale=3 # scale for bc
#!/usr/bin/env ruby19
require 'set'
keys = File.read(ARGV.shift).scan(/\w+/).to_set
index = Hash.new {|h,word| h[word] = Set.new}
ARGF.each do |line|
line.scan /\w+/ do |word|
@rklemme
rklemme / full-update.sh
Created October 13, 2011 07:14
Update all packages on debian based system
#!/bin/sh -fe
# Do a complete apt update to the current
# state of affairs.
test `id -u` -eq 0 || exec sudo "$0" "$@"
set -x
apt-get update
apt-get autoremove --yes