主題:erlang靜態資料查詢方式的一種構想 [url]
解決這個問題有2種方式:
1. 函式匹配
2. per module constant pool
針對這個問題我做了個試驗, 構建乙個atom->int的查詢。
yu-fengdemacbook-2:~ yufeng$ cat t.erl
-module(t).
-export([start/1, start/2]).
start([a1, a2])->
start(list_to_integer(atom_to_list(a1)), a2).
start(n, meth)->
start = erlang:now(),
dotimes(n, case meth of m->fun dict_lookup/1; f->fun fun_lookup/1 end),
stop = erlang:now(),
erlang:display( n / time_diff(start, stop)).
dotimes(0, _) ->
done;
dotimes(n, f) ->
f(n),
dotimes(n - 1, f).
time_diff(, ) ->
(b1 - a1) * 1000000 + (b2 - a2) + (b3 - a3) / 1000000.0 .
dict_lookup(i) ->
= dict:find(list_to_atom("x" ++ integer_to_list(i)), h1:get_dict()) .
fun_lookup(i) ->
i = h2:lookup(list_to_atom("x" ++ integer_to_list(i))).
echo "done."yu-fengdemacbook-2:~ yufeng$ cat make_dict
#!/opt/local/bin/escript
main([a])->
n = list_to_integer(a),
l = [ || x<-lists:seq(1, n)],
d = dict:from_list(l),
io:format("-module(h1).~n-export([get_dict/0]).~nget_dict()->~n",),
erlang:display(d),
io:format(".~n"),
ok.yu-fengdemacbook-2:~ yufeng$ cat make_fun
#!/opt/local/bin/escript
main([a])->
n = list_to_integer(a),
io:format("-module(h2).~n-export([lookup/1]).~n",),
[io:format("lookup(~p)->~p;~n",[list_to_atom("x" ++ integer_to_list(x)), x]) || x<-lists:seq(1, n)],
io:format("lookup(_)->err.~n", ),
ok.yu-fengdemacbook-2:~ yufeng$ head h1.erl
-module(h1).
-export([get_dict/0]).
get_dict()->
,,}}
.yu-fengdemacbook-2:~ yufeng$ head h2.erl
-module(h2).
-export([lookup/1]).
lookup(x1)->1;
lookup(x2)->2;
lookup(x3)->3;
lookup(x4)->4;
lookup(x5)->5;
lookup(x6)->6;
lookup(x7)->7;
lookup(x8)->8;
yu-fengdemacbook-2:~ yufeng$ cat test.sh
#!/bin/bash
#opt=+native
opt=
echo "build $1..."
echo "make h1..."
./make_dict $1 >h1.erl
echo "make h2..."
./make_fun $1 >h2.erl
echo "compile h1..."
erlc $opt h1.erl
echo "compile h2..."
erlc $opt h2.erl
echo "compile t..."
erlc $opt t.erl
echo "running..."
echo "map..."
erl -s t start $1 m -noshell -s erlang halt
echo "fun..."
erl -s t start $1 f -noshell -s erlang halt
yu-fengdemacbook-2:~ yufeng$ ./test.sh 10000
build 10000...
make h1...
make h2...
compile h1...
compile h2...
compile t...
running...
map...
2.767323e+05
fun...
2.656819e+05
done.
在10000條記錄的情況下 每個查詢幾個us, 速度不是很快.
結果發現 函式和constant pool在處理上上差不多快的。在實踐中根據需要採用[[x12|12],[x32|32],[x52|52],[x72|72],[x92|92]],[[x13|13],[x33|33],[x53|53],[x73|73],[x93|93]],[x4|4][x14|14],[x24|24],[x34|34],[x44|44],[x54|54],[x64|64],[x74|74],[x84|84],[x94|94]],[[x5|5],[x15|15],[x25|25],[x35|35],[x45|45],[x55|55],[x65|65],[x75|75],[x85|85],[x95|95]],[[x6|6],[x16|16],[x26|26],[x36|36],[x46|46],[x56|56],[x66|66],[x76|76],[x86|86],[x96|96]],[[x7|7],[x17|17],[x27|27],[x37|37],[x47|47],[x57|57],[x67|67],[x77|77],[x87|87],[x97|97]],[[x8|8],[x18|18],[x28|28],[x38|38],[x48|48],[x58|58],[x68|68],[x78|78],[x88|88],[x98|98]],[[x9|9],[x19|19],[x29|29],[x39|39],[x49|49],[x59|59],[x69|69],[x79|79],[x89|89],[x99|99]],,,,,,[x20|20][x40|40],[x60|60],[x80|80],[x100|100]],[[x1|1],[x21|21],[x41|41],[x61|61],[x81|81]],[[x2|2],[x22|22],[x42|42],[x62|62],[x82|82]],[[x3|3],[x23|23],[x43|43],[x63|63],[x83|83]],,,,,,,,,,,,}}}
.yu-fengdemacbook-2:~ yufeng$ head h2.erl
-module(h2).
-export([lookup/1]).
lookup(x1)->1;
lookup(x2)->2;
lookup(x3)->3;
lookup(x4)->4;
lookup(x5)->5;
lookup(x6)->6;
lookup(x7)->7;
lookup(x8)->8;
yu-fengdemacbook-2:~ yufeng$ cat test.sh
#!/bin/bash
#opt=+native
opt=
echo "build $1..."
echo "make h1..."
./make_dict $1 >h1.erl
echo "make h2..."
./make_fun $1 >h2.erl
echo "compile h1..."
erlc $opt h1.erl
echo "compile h2..."
erlc $opt h2.erl
echo "compile t..."
erlc $opt t.erl
echo "running..."
echo "map..."
erl -s t start $1 m -noshell -s erlang halt
echo "fun..."
erl -s t start $1 f -noshell -s erlang halt
yu-fengdemacbook-2:~ yufeng$ ./test.sh 10000
build 10000...
make h1...
make h2...
compile h1...
compile h2...
compile t...
running...
map...
2.767323e+05
fun...
2.656819e+05
done.
在10000條記錄的情況下 每個查詢幾個us, 速度不是很快.
結果發現 函式和constant pool在處理上上差不多快的。在實踐中根據需要採用把。
django shell 方式資料查詢
一。table.objects.get 方法是從資料庫的取得乙個匹配的結果,返回乙個物件,如果記錄不存在的話,它會報錯。而且該方法為完全匹配查詢。若通過該方法進行模糊查詢,會報 doesnotexist event matching query does not exist.的異常。舉例 1.完全匹...
hibernate資料查詢的幾種方式
1.使用主鍵id載入物件 load get 2.通過物件導航,比如通過stu.getteam 得到team的例項 3 使用hql 4使用qbc query by criteria 5直接使用sql語句取得記錄集 一般都使用後面三種方式.注意.hql是物件導向的查詢.語法和sql是基本一樣的.不區分大...
hibernate資料查詢的幾種方式
1.使用主鍵id載入物件 load get 2.通過物件導航,比如通過stu.getteam 得到team的例項 3 使用hql 4使用qbc query by criteria 5直接使用sql語句取得記錄集 一般都使用後面三種方式.注意.hql是物件導向的查詢.語法和sql是基本一樣的.不區分大...