pydocみて気になったライブラリを触ってみた。

たまたまネットはつながってないけど、ライブラリの調べものしたかったのでpydoc読んだ。

pydoc読むのは、ポート指定して簡易サーバ立ち上げるのがらくちん。

$ pydoc -p 8080
pydoc server ready at http://localhost:8080/

ひとまず、__builtin__.Functions. compileが気になるから触ってみる。

s = """for x in xrange(1, 100):
  print x"""  
c = compile(s, "", "exec")
exec c

compileするとcode objectが返る。
あー。メタプログラミングの際に事前にコンパイルしとくと高速化できるのかな?なるほど。

Profile

プロファイルライブラリ使った事なかったから、profile.Profileライブラリを使ってみる。
(cProfileとprofileが別々にあるのだけど、差が分からん...)

s = """for x in xrange(1, 100):
  print x"""  
from profile import Profile
Profile().run(s).print_stats()
3 function calls in 0.004 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.003    0.003    0.003    0.003 :0(setprofile)
        1    0.001    0.001    0.001    0.001 <string>:1(<module>)
        1    0.000    0.000    0.004    0.004 profile:0(for x in xrange(1, 100):
  print x)
        0    0.000             0.000          profile:0(profiler)
s = """for x in xrange(1, 100):
  print x"""  
from profile import Profile
compile(s, "", "exec")
c = compile(s, "", "exec")
Profile().run(c).print_stats()
3 function calls in 0.001 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.001    0.001    0.001    0.001 :1(<module>)
        1    0.000    0.000    0.001    0.001 profile:0(<code object <module> at 0x4a25c0, file "", line 1>)
        0    0.000             0.000          profile:0(profiler)

とやったところで、速度差はでなかった、そりゃそうだwww検証コードがひどすぎるw

再利用するコードについては事前コンパイルしよう。

追記

pdbが便利そうだから、今週使ってみる。

C言語とか

C書けないのだけど、業務でC使ってる友人が自宅に来たので、少し教えてもらった。
基礎構文は飛ばして、includeと構造体について自宅においてあったCの基本書片手に説明してもらった。

たまたま、EclipseにとPHP5.3のソースコードがチェックアウトされていたので、一緒にリーディング。
PHPではecho文、print文、新規追加されたgoto文あたりを。あとパーサーの定義を読む。


同じくチェックアウトされてたopenjdkやらPython3kも読んでみた。

openjdkでは基本クラスのobjectがCで書かれていたので目を通す。


ま、正直C使いじゃないから理解度低いけど>_<


勉強になる。サンキュー友よ。