Skip to content

Instantly share code, notes, and snippets.

View 9thbit's full-sized avatar

Barry Hurley 9thbit

View GitHub Profile
@9thbit
9thbit / timing_existance_check_list_versus_set.py
Created September 15, 2016 08:32
Times checks for UUIDs in a large list versus a large set
from uuid import uuid4
import timeit
def build_uuid_list(num_uuids):
return [uuid4() for i in xrange(num_uuids)]
def main():
setup = """
@9thbit
9thbit / shufflecnf.py
Created December 12, 2014 14:25
Python code to shuffle a SAT instance in CNF format. Renames the variables, re-orders the clauses, and re-orders the literals in each clause.
from math import copysign
import random
import sys
import os
def shufflecnf(cnffilename, seed, outputfile):
random.seed(seed)
numvar, numclauses = None, None
@9thbit
9thbit / gist:8364073
Created January 10, 2014 22:37
Benchmarking two methods of computing nCr.
# Benchmarking two methods of computing nCr.
# ncr1(1000, 5) 2.25343108177
# ncr2(1000, 5) 2.01950907707
# ncr1(1000, 500) 2.98434901237
# ncr2(1000, 500) 3.22741794586
# ncr1(1000000, 10) 3.69585800171
# ncr2(1000000, 10) 4.68463897705
@9thbit
9thbit / nomnomnom.py
Created December 14, 2013 11:47
Chews up CPU and RAM. Fills the current available memory with random integers and repeatedly sorts and shuffles them. Was used to stress test some RAM modules.
from multiprocessing import Pool, cpu_count
import random
import time
import sys
import os
import re
MEMINFO = "/proc/meminfo"
@9thbit
9thbit / Join Line With Next.applescript
Last active December 12, 2015 09:49 — forked from gingi/Join Line With Next.applescript
Join lines in BBEdit by placing a space character between the two lines.
tell application "BBEdit"
tell window 1
set currentLine to endLine of selection
set nextLine to currentLine + 1
-- Delete starting after the last non-whitespace character in the current line
set findDeleteStart to find "\\s*$" options {search mode:grep} ¬
searching in line currentLine
if found of findDeleteStart then
set firstCharacter to characterOffset of found object of findDeleteStart
@9thbit
9thbit / SortingWithRandomizedTies.py
Last active July 1, 2019 16:17
Sorting a Python dictionary with randomized tie breaker
import random
def compare_with_ties(a, b):
diff = cmp(a, b)
return diff if diff else random.choice([-1, 1])
a = {'a': 1, 'b': 2, 'c': 1, 'd': 2, 'e': 1}
print "Initial dictionary:\n", str(a.items())
@9thbit
9thbit / gist:1351468
Created November 9, 2011 13:47
Dynamically allocate a 2D matrix of ints
int **int_matrix_alloc(int n, int m){
int *a, **aa, i;
a = (int *)calloc(n * m, sizeof(int));
aa = (int **)calloc(n, sizeof(int *));
for(i=0;i<n;i++){
aa[i] = &a[i * m];
}
return aa;
}
@9thbit
9thbit / gist:1284256
Created October 13, 2011 13:46
Reverse the order of pages in a postscript file
pstops 1:-0 input_file.ps output_file.ps
@9thbit
9thbit / gist:1281789
Created October 12, 2011 16:48
Benchmarks for popping from a python list
# Benchmarks for popping from a list
# If you want to always pop the first element of a list, then don't use pop(0)
# Instead reverse the list once and use pop.
python -m timeit "a=range(1000000)" "a.reverse()" "while a: a.pop()"
python -m timeit "a=range(1000000)" "while a: a.pop(0)"
@9thbit
9thbit / gist:1229402
Created September 20, 2011 15:26
MS SQL Server Utility Snippets
---- Enable Ad hoc Queries ----
sp_configure 'show advanced options', 1
reconfigure
sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure
-------------------- DATE RELATED --------------------
---- Replace GETDATE() with your fieldname if needed.