初めてのパイパイ

タイトルが釣りっぽくなってしまったw

最近 Python をいじっていて、PyPy なるものの存在を知り、気になったので、ゴニョゴニョしたという次第。

まずはインストール。brew でお手軽に。

$ brew install pypy
Warning: It appears you have MacPorts or Fink installed.
Software installed with other package managers causes known problems for
Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again.
==> Downloading http://bitbucket.org/pypy/pypy/downloads/pypy-1.6-osx64.tar.bz2
######################################################################## 100.0%
/usr/local/Cellar/pypy/1.6.0: 2642 files, 74M, built in 14 seconds

せっかくなのでベンチマークしたいなぁと思い、適当なプログラムを探す。
使用したのは、「Go, C , Pythonの簡単なベンチマークとってみた」 で使われていた、エストラネテスの篩い。(指定された整数以下の全ての素数を発見するための単純なアルゴリズム by wikipedia)
それぞれ5回実行して、ExecTime の平均で見てみることにする。

python pypy
1回目 5.62618708611 1.10052704811
2回目 5.61147499084 1.09992599487
3回目 5.82915711403 1.1193728447
4回目 5.71332597733 1.12490606308
5回目 5.62336587906 1.09711885452
平均 5.68070220947 1.10837016106

こんなにも違うのかーという結果に。だいたい5分の1。
インストールしてしまえば、実行は簡単なので、本業務でも使ってみたいなぁ、と。

ちなみに、今回の実行環境は以下の通り。
About This Mac

$ python --version
Python 2.6.1
$ pypy --version
Python 2.7.1 (dcae7aed462b, Aug 17 2011, 09:46:15)
[PyPy 1.6.0 with GCC 4.0.1]

PyPy の紹介としては、以下のスライドがステキでした。

—-

以下、使用したプログラムを転載しておきます。

#!/usr/bin/python
# encoding:utf-8
import time

MaxNum = 1000000

def main():
    start = time.time()
    prime_box = [0 for i in range(MaxNum)]
    prime_box[0], prime_box[1] = 1,1
    for i in range(MaxNum)[2:]:
        j = 1
        while i * (j+1) < MaxNum :
            prime_box[i*(j+1)] = 1
            j+=1
    end = time.time()

    # for n,i in enumerate(prime_box):
    #     if i == 0 : print n

    print "Start:",start, "   End:", end
    print "ExecTime:",end-start     

main()

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*


次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img localsrc="" alt="">