Skip to content

Instantly share code, notes, and snippets.

@khayrov
khayrov / Makefile
Created December 26, 2010 23:05
Micro-optimized (but algorithmically inefficient) prime numbers finder
CFLAGS=-O2 -ffast-math -march=native
CXXFLAGS=$(CFLAGS)
ifneq (, $(findstring 64, $(shell uname -m)))
asm_file=prime64.S
x86_64=1
else
asm_file=prime32.S
endif
@khayrov
khayrov / ulam.hs
Created May 13, 2011 07:56
Ulam spiral visualization
import Data.List
import Graphics.Gloss
import Graphics.Gloss.Data.Point
infixl 6 |+|
(x, y) |+| (x', y') = (x + x', y + y')
spiral = scanl (|+|) (0, 0) steps
where
@khayrov
khayrov / simplecalc.hs
Created May 15, 2011 17:20
very simple expression parser
expr :: String -> (Double, String)
expr s = expr1 x rest where (x, rest) = term s
expr1 lhs ('+' : s) = expr1 (lhs + x) rest where (x, rest) = term s
expr1 lhs ('-' : s) = expr1 (lhs - x) rest where (x, rest) = term s
expr1 lhs s = (lhs, s)
term s = term1 x rest where (x, rest) = factor s
term1 lhs ('*' : s) = term1 (lhs * x) rest where (x, rest) = factor s
term1 lhs ('/' : s) = term1 (lhs / x) rest where (x, rest) = factor s
term1 lhs s = (lhs, s)
factor ('(' : s) = (x, rest) where (x, ')' : rest) = expr s
@khayrov
khayrov / wcl.c
Created November 11, 2011 15:34
fast wc -l
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BUF_SIZE 1048576
@khayrov
khayrov / capture.c
Created March 6, 2012 12:21
Packet capture
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
struct B;
struct A
{
struct B *ptr;
};
struct B
@khayrov
khayrov / Makefile
Created July 21, 2012 20:40
stock quote parsing with Ragel
CFLAGS=-O2
CXXFLAGS=-O2
all: parser
parser.cpp: parser.rl
ragel -G2 $^ -o $@
parser.o: parser.cpp RawQuote.h
@khayrov
khayrov / Lexer.java
Created August 28, 2012 16:25 — forked from lucassmagal/Lexer.java
Ragel with Java
// line 1 "Lexer.rl"
public class Lexer {
// line 19 "Lexer.rl"
// line 10 "Lexer.java"
private static byte[] init__simple_lexer_actions_0()
@khayrov
khayrov / Makefile
Created September 10, 2012 06:55
luabind example: export Bullet vector algebra and inheriting C++ classes in Lua
CXXFLAGS=-O2 -Wall -g -fPIC $(shell pkg-config --cflags lua5.1 luabind) -I/usr/local/include/bullet
LDFLAGS=-lstdc++ $(shell pkg-config --libs lua5.1 luabind) -lLinearMath
all: luavector
luavector: luavector.o
luavector.o: luavector.cpp
.PHONY: clean
@khayrov
khayrov / dropcache.c
Created January 28, 2013 12:05
Try to remove file from page cache
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>