Skip to content

Instantly share code, notes, and snippets.

View amosr's full-sized avatar

Amos Robinson amosr

View GitHub Profile
@amosr
amosr / linpack.c
Created November 25, 2012 22:56
#include inside a function definition?!
// from linpack benchmark. source: http://www.netlib.org/benchmark/linpackc
/*----------------------*/
REAL second()
{
#include <sys/time.h>
#include <sys/resource.h>
struct rusage ru;
module Main where
import qualified Data.Vector.Unboxed as U
import Data.List
import System.Random
import System.CPUTime
import Text.Printf
import Control.Exception
main :: IO ()
@amosr
amosr / Complex.swift
Created July 20, 2014 08:53
I'm trying to write a Complex type that works for any floaty thing, but can't really get it
import Foundation;
protocol Num {
typealias T;
class func plus(T, T) -> T;
class func mul(T, T) -> T;
class func negate(T) -> T;
class func sub(T, T) -> T;
}
@amosr
amosr / List.hs
Last active August 29, 2015 14:16
ddc php backend
module List
-- (actually List.ds but changed extension for syntax highlighting)
import foreign c value
q_string_concat : String -> String -> String
where
-- | A `Maybe` may contain a value, or not.
data Maybe (a : Data) where
Nothing : Maybe a
Just : a -> Maybe a
@amosr
amosr / sapling_icicle
Last active October 12, 2015 02:03
sapling icicle
When dealing large data sets that do not fit in memory, it is crucial to limit the number
of accesses and iterations over the data set.
However, in a high level language it may not be immediately obvious how many iterations a
particular program will require.
The number of iterations becomes even less obvious in the presence of heuristic and
statistics-based optimisations, as used by traditional databases: a small tweak to the query
or even modifying the number of rows in a table can cause drastic changes to the query plan.
With the advent of "big data", and as data sets continue to grow, a high level language with
predictable runtime characteristics becomes more necessary.
@amosr
amosr / gist:8989c5c65e684bf76c2d
Created November 13, 2015 22:13
beating grep
$ ls -lah asx.psv
-rw-r--r-- 1 amos staff 59G 13 Nov 18:07 asx.psv
$ time grep -v EntryError asx.psv > /dev/null
real 14m33.885s
user 14m10.081s
sys 0m22.390s
@amosr
amosr / read.c
Created November 18, 2015 02:27
12 instruction case-insensitive string equality (for reading booleans)
int read_bool (char *p)
{
static const uint64_t true_mask = 0x00000000ffffffff;
static const uint64_t true_bits = 0x0000000065757274;
static const uint64_t false_mask = 0x000000ffffffffff;
static const uint64_t false_bits = 0x00000065736c6166;
static const uint64_t to_lower = 0x2020202020202020;
uint64_t next8 = *(uint64_t *)p | to_lower;
/*
attempt at writing a faster memcmp
*/
static iint_t INLINE memcmp8 (const char *as, const char* bs, iint_t len)
{
uint64_t *a = (uint64_t*)as;
uint64_t *b = (uint64_t*)bs;
while (len > 8) {
if (*a != *b) {
return 1;
#include <stdio.h>
#include <string.h>
#include <time.h>
typedef long long int uint64_t;
int cmp8_mask (const char *as, const char* bs, uint64_t len)
{
uint64_t rem = len;
while (rem > 8) {
@amosr
amosr / postgres.md
Last active November 26, 2015 03:37
Playing with postgres

Comparing the Icicle against Postgres

Using the ASX data. SQL to create table and index is:

create table asx (company char(3), date date, open double precision, high double precision, low double precision, close double precision, volume double precision, adjclose double precision);