postgresql10をcentos7にインストール
# yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm # yum install postgresql10-devel postgresql10-contrib # /usr/pgsql-10/bin/postgresql-10-setup initdb postgres=# create role username with createdb login password '***'; postgres=# create database dbname OWNER username;
elixirでマルチコアCPUプログラミング
プログラミングErlangのマルチコアCPUプログラミングの章にあるpmapのコードをelixirで書いた
https://github.com/iyoo14/map_utilgithub.com
結果
100個のリストの各数値の
フィボナッチ数の計算
は非効率な再帰版の場合は並列の方が早いが
効率のよい末尾再帰の場合は並列の方が遅い
1000個の数値のリストを含んだ100個のリストを作る
各リストの数値の並び替え
は並列の方が遅い
となった。
elixirインストール
# cd /usr/local/src/
# wget http://erlang.org/download/otp_src_20.0.tar.gz
# tar xfzv otp_src_20.0.tar.gz
# cd otp_src_20.0
# ./configure
# make
# make install# cd /usr/local/lib
# git clone https://github.com/elixir-lang/elixir.git
# cd elixir
# make clean test
VIM バイナリ編集
$ vim -b :%!xxd 元に戻す :%!xxd -r
GOで二分探索木
GOで二分探索木を書いてみた。
GOのリストとパフォーマンス比較。
RANDOMSが対象のデータ構造(二分探索木、リスト)
SEARCHSが検索する値のリスト
ランダムにRANDOMSに値を入れて
SEARCHSの値でRANDOMSを検索する。
1回目 RAMDOMS len is 50 SEARCHS len is 100 BINARI SEARCH start: insert. end : insert time: 6.375µs. start: search. end : search result num: 50, time: 25.241µs. GOLIST start: insert. end : insert time: 184ns. start: search. end : search result num: 50, time: 8.266µs. 2回目 RAMDOMS len is 122 SEARCHS len is 244 BINARI SEARCH start: insert. end : insert time: 18.031µs. start: search. end : search result num: 122, time: 37.397µs. GOLIST start: insert. end : insert time: 522ns. start: search. end : search result num: 122, time: 63.044µs 3回目 RAMDOMS len is 500 SEARCHS len is 1000 BINARI SEARCH start: insert. end : insert time: 124.748µs. start: search. end : search result num: 500, time: 140.488µs. GOLIST start: insert. end : insert time: 7.284µs. start: search. end : search result num: 500, time: 578.013µs.
値の個数が少ない場合はリストの方がパフォーマンスがよいが
値の個数が120個あたりで処理速度が逆転する。
pythonでtsvからsqlを作る
タブ区切りのデーターファイルからsqlを生成する。
id¥tname¥tmsg
から
insert into table ( id,name,msg ) values (%s,%s,%s),(%s,%s,%s),(%s,%s,%s)
psycopg2のプレースホルダーの形式でバルクinsert文へ
引数にファイル名を渡す
python hoge.py data.tsv
import sys cols = ['id', 'name', 'msg'] def main(): try: for d in parse_line(): print make_str(d) except IOError as e: print 'IOError!!!' print e except Exception as e: print 'Exeption' print "Unexpected error:", sys.exc_info()[0] print e def make_str(data): f = [] for x in cols: f.append('%s') fs = "(" + ",".join(f) + ")" fss = [] cond = [] for td in data: fss.append(fs) for d in td: cond.append(d) sql = "insert into table ( {0} ) values {1} ".format(",".join(cols), ",".join(fss)) print cond return sql def parse_line(): nums = 0 nmax = 3 ds = [] for line in open(sys.argv[-1]): d = line[:-1] if nums == nmax: yield ds ds = [] nums = 0 ds.append(d.split("\t")) nums += 1 yield ds if __name__ == '__main__': main()