'cython'에 해당되는 글 1건

  1. 2013.12.15 Cython 설치 4
Coding/Python 삽질기2013. 12. 15. 04:02

Cython 설치 ( Mac )

  1. python download ( http://www.cython.org )
  2. sudo python setup.py install
Cython 실습

factorial_100000.pyx ]

def Factorial(int N):
    cdef int checkSize = 5000
    cdef int fi
    cdef int endN
    endN = N / checkSize
    facResult = 1
    for fi in range(1, endN + 1):
        facResult = facResult * reduce(lambda x, y : x * y, range((fi-1)*checkSize+1,fi * checkSize+1))
   
    if ( (endN * checkSize + 1) != (N+1) ):   
        facResult = facResult * reduce(lambda x, y : x * y, range(endN * checkSize + 1, N+1))  
    return facResult


[ setup.py ]

from distutils.core import setup

from Cython.Build import cythonize

setup(
  name = 'Factorial app',
  ext_modules = cythonize("factorial_100000.pyx"),


[ Build ]
python setup.py build_ext —inplace

# 이러면 SO 파일이 생김

[ Sandbox.py ]

import time

from factorial_100000 import Factorial

startTime = time.time()
factorialN = Factorial(10000)

print "runtime is %s"%(time.time() - startTime) 


[ 실행결과 ]



'Coding > Python 삽질기' 카테고리의 다른 글

[Notepad++] Python 실행하기  (0) 2016.08.28
100000! ( 100000 팩토리얼 ) & 1000000! ( 1000000 팩토리얼 )  (0) 2013.06.22
fibonacci 수열  (0) 2013.06.20
Posted by chobocho