返回介绍

Cython version

发布于 2025-02-25 23:44:03 字数 1761 浏览 0 评论 0 收藏 0

For more control over the translation to C, most Python scientific developers will use the Cython package. Essentially, this is a language that resembles Python with type annotations. The Cython code is then compiled into native code tranaparently. The great advantage of Cythonn over ther approaches are:

  • A Python program is also valid Cython program, so optimization can occur incrementally
  • Fine degree of control over degree of optimization
  • Easy to use - handles details about the C compiler and shared library generation
  • Cythonmagic extension comes built into IPyhton notebook
  • Can run parallel code with the nogil decorator
  • Fully optimized code runs at thee same speed as C in most cases
%load_ext cythonmagic
The Cython magic has been moved to the Cython package, hence
%load_ext cythonmagic is deprecated; please use %load_ext Cython instead.

Though, because I am nice, I'll still try to load it for you this time.
%%cython

import numpy as np
cimport cython
from libc.math cimport sqrt

@cython.boundscheck(False)
@cython.wraparound(False)
def pdist_cython(double[:, ::1] xs):
    cdef int n = xs.shape[0]
    cdef int p = xs.shape[1]
    cdef double tmp, d
    cdef double[:, ::1] D = np.empty((n, n), dtype=np.float)
    for i in range(n):
        for j in range(n):
            d = 0.0
            for k in range(p):
                tmp = xs[i, k] - xs[j, k]
                d += tmp * tmp
            D[i, j] = sqrt(d)
    return np.asarray(D)
print pdist_cython(A)
%timeit pdist_cython(xs)
[[ 0.  5.]
 [ 5.  0.]]
100 loops, best of 3: 7.09 ms per loop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文