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

Zaur Shibzukhov szport at gmail.com
Mon Mar 4 07:24:30 CET 2013


2013/3/3 Zaur Shibzukhov <szport at gmail.com>:
> 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

Yet another performance comparison for `time`:

# coding: utf-8

from cpython.datetime cimport import_datetime, time_new, time

import_datetime()

from datetime import time as pytime

def test_time1():
    cdef list lst = []
    cdef int hour, minute, second, microsecond
    for hour in range(0, 24):
        for minute in range(0,60):
            for second in range(0, 60):
                for microsecond in range(0, 100000, 50000):
                    d = pytime(hour, minute, second, microsecond)
                    lst.append(d)
    return lst


def test_time2():
    cdef list lst = []
    cdef int hour, minute, second, microsecond
    for hour in range(0, 24):
        for minute in range(0,60):
            for second in range(0, 60):
                for microsecond in range(0, 100000, 50000):
                    d = time(hour, minute, second, microsecond)
                    lst.append(d)
    return lst

def test_time3():
    cdef list lst = []
    cdef int hour, minute, second, microsecond
    for hour in range(0, 24):
        for minute in range(0,60):
            for second in range(0, 60):
                for microsecond in range(0, 100000, 50000):
                    d = time_new(hour, minute, second, microsecond, None)
                    lst.append(d)
    return lst

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

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

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

Timings:

(py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
mytests.test_time import test1" "test1()"
50 loops, best of 5: 72.2 msec per loop
(py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
mytests.test_time import test2" "test2()"
50 loops, best of 5: 64.7 msec per loop
(py32)zbook:mytests $ python -m timeit -n 50 -r 5 -s "from
mytests.test_time import test3" "test3()"
50 loops, best of 5: 13 msec per loop

Sure the same kind of results might expect for `datetime` too.

Shibzukhov Zaur


More information about the cython-devel mailing list