12/16/2010

[読書]ケント・ベック 実装パターン 

Kent Beck の実装パターンを読んだ。
日常的にコードを書く人ではない、趣味の園芸な自分が読んでも楽しめる。

一通り読んだものの咀嚼出来ていないところがあるので、時間を見つけて査読したいな。

ちょっと前に、Junitのソースコードを眺めていたんだけど、この本読んでる最中に、
「ああ、ここの記述はあそこのソースでもやってたなぁ」と思いだした。
もう一度、Junitちゃんと読んでみようかなぁ。

コレクションAPIのパフォーマンス比較をするために、測定用フレームワークを作っていて付録Aで
そのコードを公開している、コード自体はとても小さいシンプルなものだけど、本書の中のパターン
が使われている。

後でWebで見たかったので写経してみた。
コメントいれてる。変なところがあったら御免なさい。

このListSerchクラスのserchメソッドの実行時間を計測すると
search 25.30 77.81 551.10 と結果出力される。
要素数が増加すると実行時間が増えている。



import java.util.ArrayList;
import java.util.List;

public class ListSerch {
private List<Integer> numbers;
private int probe;

public ListSerch(int size){
numbers = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
numbers.add(i);
probe = size / 2;
}
}
//ArrayListの真ん中を取得させる。
public void search(){
numbers.contains(probe);
}
}

ドライバークラスは以下。
テストしたいクラスに含まれるメソッドの配列を引数にしてMethodsTimerインスタンス化。
report()メソッドを実行する。
結果はコンソールに
メソッド名 1回実行での時間 10回実行での合計時間 100回実行での合計時間 ・・・合計が1秒こえるまでやる。

public class TestDriver {

public static void main(String[] args) throws Exception{
MethodsTimer tester = new MethodsTimer(ListSerch.class.getDeclaredMethods());
tester.report();
}
}



import java.lang.reflect.Method;

public class MethodsTimer {
public final Method[] methods;
private static final int MAXIMUM_SIZE = 100;
public static final int ONE_SECOND = 1000000000;

public MethodsTimer(Method[] methods){
this.methods = methods;
}

public void report() throws Exception{
for(Method each : methods){
System.out.print(String.format("%.2f\t", r.getMethodTime()));
for (int size = 1; size <= MAXIMUM_SIZE; size *=10)
{
MethodTimer r = new MethodTimer(size, each);
r.run();
System.out.print(r.getMethodTime() + "\t");
}
System.out.println();
}
}
}



import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class MethodTimer {
private final int size;
private final Method method;
private Object instance;
private long totalTime;
private int iterations;
private long overhead;

public MethodTimer(int size, Method method) throws Exception{
this.size=size;
this.method=method;
instance = createInstance();
}

public MethodTimer(int iterations) throws Exception{
this(0,MethodTimer.Overhead.class.getMethod("nothing", new Class[0]));
this.iterations = iterations;
}

private static MethodTimer overheadTimer(int iterations) throws Exception{
return new MethodTimer(iterations);
}

private Object createInstance() throws Exception {
Constructor<?> constructor = method.getDeclaringClass().getConstructor(
new Class[] { int.class });
return constructor.newInstance(new Object[] { size });
}

double getMethodTime(){
return (double)(totalTime - overhead) / (double)iterations;
}


void run() throws Exception {
iterations = 1;
while (true) {
totalTime = computeTotalTime();
if (totalTime > MethodsTimer.ONE_SECOND)
break;
iterations *= 2;
}
overhead = overheadTimer(iterations).computeTotalTime();
}



private long computeTotalTime() throws Exception {
long start = System.nanoTime();
for (int i = 0; i < iterations; i++) {
method.invoke(instance, new Object[0]);
}
return System.nanoTime() - start;
}

public static class Overhead {
public Overhead(int size) { }
public void nothing() { }
}

}

10/06/2010

Pydevのデバッガーがエラーになる件

EclipseでPythonを触ってみようとして、はまったのでメモしておく

python 3.1
pydev 1.6.2
実行環境は別途落としてきた3.1を利用。

debug実行時にソースにダブルバイトのコメントが存在した場合、

#あ
n=1
print("hoge")


エラーは以下

pydev debugger: starting
Traceback (most recent call last):
File "C:\util\ide\eclipse36\eclipse\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\pydevd.py", line 1147, in
debugger.run(setup['file'], None, None)
File "C:\util\ide\eclipse36\eclipse\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\pydevd.py", line 925, in run
line = stream.readline() #Should not raise an exception even if there are no more contents
UnicodeDecodeError: 'cp932' codec can't decode bytes in position 55-56: illegal multibyte sequence

encodeはutf-8に設定してるのだが?pydevd.pyのエラー箇所をみてる。

if not IS_PY3K:
execfile(file, globals, locals) #execute the script
else:
stream = open(file)
try:
encoding = None
#Get encoding!
for i in range(2):
line = stream.readline() #Should not raise an exception even if there are no more contents
#Must be a comment line
if line.strip().startswith('#'):
#Don't import re if there's no chance that there's an encoding in the line
if 'coding' in line:
import re
p = re.search(r"coding[:=]\s*([-\w.]+)", line)
if p:
encoding = p.group(1)
break
finally:
stream.close()


の line = stream.readline()でエラー。
その上で、cp932でstream = open(file)してしまってるためっぽい。
openしてcodingを調べたいのに、cp932で開いてエラーになる

ソース先頭2行目までに、# coding: utf-8
があれば、stream = open(file, encoding='utf-8')のとこだけの変更でよいが、
encoding='utf-8'もいれといた。


if not IS_PY3K:
execfile(file, globals, locals) #execute the script
else:
#stream = open(file) #2010.10.6 cahnge
stream = open(file, encoding='utf-8')
try:
#encoding = None #2010.10.6 cahnge
encoding='utf-8'
#Get encoding!
for i in range(2):
line = stream.readline() #Should not raise an exception even if there are no more contents
#Must be a comment line
if line.strip().startswith('#'):
#Don't import re if there's no chance that there's an encoding in the line
if 'coding' in line:
import re
p = re.search(r"coding[:=]\s*([-\w.]+)", line)
if p:
encoding = p.group(1)
break
finally:
stream.close()

if encoding:
stream = open(file, encoding=encoding)
else:
stream = open(file)
try:
contents = stream.read()
finally:
stream.close()


他にもっと、ちゃんとしたやり方がある筈なのだが、初心者なのでよくわからん。
動くようになったから、とりあえずこれでいいや。


2011-01-16 追記
バグトラッカに報告された模様、有難いことです。
http://suzumizaki.blog6.fc2.com/blog-entry-157.html
私も世の中に、貢献できるような人間になりたい。。

5/02/2010

ヒツジヤ殺人未遂事件

ヒツジヤは洋傘を売ってる小さなお店。
昔住んでた街の商店街の一角にポツンとあって、猫が出入りしてた。
それをHPに記事を書いたのが4年前くらい。

先日、新聞記事を見ててびっくり、

2010年2月19日午前11時50分ごろ、大阪府東大阪市足代1丁目の洋傘販売店「ヒツジヤ」
において、男性2人(60歳代と40歳代)が、店主の74歳の男に包丁(刃渡り10センチ)
で切りつけられるという事件が発生した。

 ~朝日新聞より


僕は2006年に大阪を離れたのだけど、勝手なもので
なんとなく、その後の物語が進んだのは僕の方でだけで
他はそのままな気がしていたのだけれど、
あの時間が止まっていそうなヒツジヤですら、
殺人未遂事件がおきちゃうのだから、何が起きててもおかしくないなぁ。
と考えさせられるものがあった。

2/16/2010

good

内容:

"create or replace function get_key (p_col in lookup_table.col1%type) return lookup_table.col2%type as v_result lookup_table.col2%type; begin select col2 into v_result from lookup_table where col1 = p_col; return v_result; end get_key; / Then you can use that function in your SQL*Loader control file, like so: Code: [Select all] [Show/ hide]load data infile 'data.txt' into table dwh_table fields terminated by ',' trailing nullcols (col1, col2 "get_key (:col1)")"
- OraFAQ Forum: Server Utilities » SQL Loader & lookup tables issueGoogle サイドウィキで表示

1/26/2010

Blogger Syntax Highlighter

今更ながらコードハイライトを試してみました。
良いですね。

参考URL
http://www.kuribo.info/2008/06/blogger-syntax-highlighter.html


import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
execfile(filename)

1/05/2010

賃金と給与の違い

丁度知りたかったので良ポスト
内容: 賃金と給与の違い。CRの間接費予定配賦は何故予定?Google サイドウィキで表示

何か、リンク先が表示できなくなっちゃった?もう一度調べる。(11/5/5)

  • 賃金と給与の違い
元々調べたかった理由は勘定科目

「給料手当」という単語は主に販売管理費の科目
「賃金」または「賃金手当」という単語は製造原価の労務費


製造業の場合に、製造現場で働く人の人件費を労務費といい、「賃金」とすることが多い。
一般事務などを別棟で行っている人、現場での管理監督者の人件費を「給料手当」と呼び、これを区別する。

製造原価性があるかないかで判断したりする。

  • CRの間接費予定配布は何故予定?
何故、調べたかったんだっけか?

1/02/2010

2010年元旦

あけましておめでとう御座います。

今年はアウトプット重視な年にしたいです。
情報収集->思索->アウトプットという流れを可視化して、
腐ってる部分を改善したいのです。
長年、ほってきたことですが、
人間、昨日より今日のほうが進歩していると思いたい。
というか、生きる楽しみなどというのはそんなもんですよね。

昨年、後ろ向きだったように思うので
色々なことに前向きでいきたいですね。