From a30b491b5624a7be6f49352659ae18bc48c1c106 Mon Sep 17 00:00:00 2001 From: tommy3001 Date: Sat, 15 Feb 2014 17:58:40 +0100 Subject: [PATCH] indentation fixed --- docs/scenarios/speed.rst | 84 ++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index af8f355..5056086 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -74,46 +74,48 @@ With Cython you are also able to call C-functions and realize strong typing of v .. code-block:: python - def primes(int kmax): - cdef int n, k, i - cdef int p[1000] - result = [] - if kmax > 1000: - kmax = 1000 - k = 0 - n = 2 - while k < kmax: - i = 0 - while i < k and n % p[i] != 0: - i = i + 1 - if i == k: - p[k] = n - k = k + 1 - result.append(n) - n = n + 1 - return result + def primes(int kmax): + cdef int n, k, i + cdef int p[1000] + result = [] + if kmax > 1000: + kmax = 1000 + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] != 0: + i = i + 1 + if i == k: + p[k] = n + k = k + 1 + result.append(n) + n = n + 1 + return result + This implementation of an algorithm to find prime numbers has some additional commands instead of the next one, which is implemented in pure Python: .. code-block:: python - def primes( kmax): - p= range(1000) - result = [] - if kmax > 1000: - kmax = 1000 - k = 0 - n = 2 - while k < kmax: - i = 0 - while i < k and n % p[i] != 0: - i = i + 1 - if i == k: - p[k] = n - k = k + 1 - result.append(n) - n = n + 1 - return result + def primes( kmax): + p= range(1000) + result = [] + if kmax > 1000: + kmax = 1000 + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] != 0: + i = i + 1 + if i == k: + p[k] = n + k = k + 1 + result.append(n) + n = n + 1 + return result + The only difference between the both algorithm is this part: @@ -124,10 +126,9 @@ Strong typing with Cython: #primes function with additional Cython code: def primes(int kmax): - cdef int n, k, i - cdef int p[1000] - result = [] - + cdef int n, k, i + cdef int p[1000] + result = [] Normal variable definition in Python: @@ -135,9 +136,8 @@ Normal variable definition in Python: #primes in standard Python syntax: def primes( kmax): - p= range(1000) - result = [] - + p= range(1000) + result = [] What is the difference? In the upper Cython version you can see the definitions of the variable types like in standard C. For example `cdef int n,k,i` in line 3. This additional type definition (e.g. integer) allows the Cython compiler to generate