[Cython] To Add datetime.pxd to cython.cpython

Zaur Shibzukhov szport at gmail.com
Sun Mar 3 20:16:43 CET 2013


2013/3/3 Zaur Shibzukhov <szport at gmail.com>:
> 2013/3/3 Zaur Shibzukhov <szport at gmail.com>:
>> 2013/3/2 Stefan Behnel <stefan_ml at behnel.de>:
>>> Hi,
>>>
>>> the last pull request looks good to me now.
>>>
>>> https://github.com/cython/cython/pull/189
>>>
>>> Any more comments on it?
>>
>> As was suggested earlier, I added `import_datetime` inline function to
>> initialize PyDateTime C API instead of direct usage of "non-native" C
>> macros from datetime.h.
>> Now you call `import_array ()` first in the same way as is done with `numpy`.
>>  This approach looks natural in the light of experience with numpy.
>>
>  I make some performance comparisons. Here example for dates.
>
> # test_date.pyx
> --------------------
>
> Here test code:
>
> from cpython.datetime cimport import_datetime, date_new, date
>
> import_datetime()
>
> from datetime import date as pydate
>
> def test_date1():
>     cdef list lst = []
>     for year in range(1000, 2001):
>         for month in range(1,13):
>             for day in range(1, 20):
>                 d = pydate(year, month, day)
>                 lst.append(d)
>     return lst
>
>
> def test_date2():
>     cdef list lst = []
>     for year in range(1000, 2001):
>         for month in range(1,13):
>             for day in range(1, 20):
>                 d = date(year, month, day)
>                 lst.append(d)
>     return lst
>
> def test_date3():
>     cdef list lst = []
>     cdef int year, month, day
>     for year in range(1000, 2001):
>         for month in range(1,13):
>             for day in range(1, 20):
>                 d = date_new(year, month, day)
>                 lst.append(d)
>     return lst
>
> def test1():
>     l = test_date1()
>     return l
>
> def test2():
>     l = test_date2()
>     return l
>
> def test3():
>     l = test_date3()
>     return l
>
> Here are timings:
>
> (py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
> mytests.test_date import test1" "test1()"
> 50 loops, best of 5: 83.2 msec per loop
> (py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
> mytests.test_date import test2" "test2()"
> 50 loops, best of 5: 74.7 msec per loop
> (py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
> mytests.test_date import test3" "test3()"
> 50 loops, best of 5: 20.9 msec per loop
>
> OSX 10.6.8 64 bit python 3.2
>

More acurate test...

# coding: utf-8

from cpython.datetime cimport import_datetime, date_new, date

import_datetime()

from datetime import date as pydate

def test_date1():
    cdef list lst = []
    cdef int year, month, day
    for year in range(1000, 2001):
        for month in range(1,13):
            for day in range(1, 20):
                d = pydate(year, month, day)
                lst.append(d)
    return lst


def test_date2():
    cdef list lst = []
    cdef int year, month, day
    for year in range(1000, 2001):
        for month in range(1,13):
            for day in range(1, 20):
                d = date(year, month, day)
                lst.append(d)
    return lst

def test_date3():
    cdef list lst = []
    cdef int year, month, day
    for year in range(1000, 2001):
        for month in range(1,13):
            for day in range(1, 20):
                d = date_new(year, month, day)
                lst.append(d)
    return lst

def test1():
    l = test_date1()
    return l

def test2():
    l = test_date2()
    return l

def test3():
    l = test_date3()
    return l

Timings:

(py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
mytests.test_date import test1" "test1()"
50 loops, best of 5: 83.3 msec per loop
(py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
mytests.test_date import test2" "test2()"
50 loops, best of 5: 74.6 msec per loop
(py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
mytests.test_date import test3" "test3()"
50 loops, best of 5: 20.8 msec per loop

Shibzukhov Zaur


More information about the cython-devel mailing list