Skip to content

Instantly share code, notes, and snippets.

@goedel-gang
Last active April 30, 2019 16:33
Show Gist options
  • Save goedel-gang/3e018e4009a1511c9239f7a35a7cd058 to your computer and use it in GitHub Desktop.
Save goedel-gang/3e018e4009a1511c9239f7a35a7cd058 to your computer and use it in GitHub Desktop.
Transposition
"""
Make a large matrix
"""
import argparse
import sys
import random
import string
UPDATE_INTERVAL = 100
BAR_WIDTH = 50
def get_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--width", type=int, default=10, help="width")
parser.add_argument("--height", type=int, default=10, help="height")
parser.add_argument("--size", type=int, default=8, help="size of each cell")
parser.add_argument("--out", type=argparse.FileType("w"), default="-",
help="output file")
return parser.parse_args()
def status(l, h):
print("\r{:5.0%} {} (line {})"
.format(l / h,
"[{}]".format(
("#" * int(BAR_WIDTH * l / h)).ljust(BAR_WIDTH)),
l),
file=sys.stderr, end="", flush=True)
def gen(w, h, s, out):
# lines are 1-indexed
for l in range(1, h + 1):
if l % UPDATE_INTERVAL == 0:
status(l, h)
print("\t".join("".join(random.choices(string.hexdigits, k=s))
for _ in range(w)),
file=out)
status(l, h)
print(file=sys.stderr)
if __name__ == "__main__":
args = get_args()
gen(args.width, args.height, args.size, args.out)
# because I want to time things
SHELL=bash
.PHONY: clean run destroy gist all
.FORCE:
DATAGEN_PYTHON = pypy3 # python3
TRANSPOSE_PYTHON = python3
HSFLAGS = -dynamic -O2 -optc-O3 #-funfolding-use-threshold=16
all: run
# Trick ghc into playing nicely, when I want to change flags
transpose: transpose.hs Makefile
touch transpose.hs
ghc $(HSFLAGS) $< -o $@
cy_transpose.c: transpose.py Makefile
cython -3 --embed $< -o $@
cy_transpose: cy_transpose.c Makefile
gcc -O3 $< -I/usr/include/python3.7m -lpython3.7m -o $@
data.dat: make_data.py
time $(DATAGEN_PYTHON) $< --width 4000 --height 4000 --size 10 --out $@
transposed_reaper.dat: data.dat .FORCE
time ~/builds/reaper/src/transpose --nozip -i $< -o $@
transposed_py.dat: data.dat .FORCE
time $(TRANSPOSE_PYTHON) transpose.py < $< > $@
transposed_hs.dat: data.dat transpose .FORCE
time ./transpose < $< > $@
transposed_cy.dat: data.dat cy_transpose .FORCE
time ./cy_transpose < $< > $@
run: transposed_py.dat transposed_hs.dat transposed_reaper.dat transposed_cy.dat
diff -q --from-file $^
clean:
-rm -f *.hi *.o *.out cy_transpose.c
destroy: clean
-rm -f tags transpose cy_transpose *.dat
gist: transpose.py transpose.hs make_data.py Makefile
mkdir -p gist
cp $^ gist
import Data.List
import qualified Data.ByteString.Char8 as B
main :: IO ()
main = do
stdin <- B.getContents
B.putStrLn $ (B.intercalate (B.pack "\n")
. map (B.intercalate (B.pack "\t"))
. transpose . map (B.split '\t')
. B.split '\n') stdin
"""
Transpose input file
"""
import sys
def transpose():
# data = map(str.split, sys.stdin)
sys.stdout.buffer.write(
b"\n".join(
map(b"\t".join,
zip(*(line.strip().split(b"\t") for line in sys.stdin.buffer)))))
sys.stdout.buffer.write(b"\n")
if __name__ == "__main__":
transpose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment