You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 26, 2022. It is now read-only.
1. Download the newest version of PyPy (skip if you're not planning on using PyPy).
@@ -49,150 +23,59 @@ the different runtimes as needed.
49
23
which will use Python 3.7 and install cython and numpy automatically.
50
24
51
25
## Tests
52
-
Each one of these programs has three different versions of calculating primes.<br>
53
-
Using variable `N` as the number being tested for primality:<br>
54
-
For prime numbers, we already know the following:
55
-
* All prime numbers are odd numbers, except for 2.
56
-
* All even numbers are divisible by 2, and therefore not prime.
57
-
* We only need to start with 2 as the first prime number, then loop from 3
58
-
to `N`, incrementing by 2 for each iteration to test only odd numbers.
59
-
60
-
#### Default Test
61
-
* Check divisibility of `N` by each number less than `N`.
62
-
63
-
#### Half of N Bounding Test
64
-
* Check divisibility of `N` by each number less than `math.floor((N / 2))`:
65
-
* This method should theoretically be 2x as fast as the
66
-
[default test method](#default).
67
-
68
-
#### Square Root of N Bounding Test
69
-
* Check divisibility of `N` by each number less than `math.floor(sqrt(N))`:
70
-
* This method should theoretically be log(N) as fast as the
71
-
[default test method](#default).
72
-
73
-
# Implementations
74
-
### CPython Variants
75
-
#### CPython through Functions
76
-
* Defined in [Find_Nth_Prime_Python.py](Find_Nth_Prime_Python.py)
77
-
* Loops over all numbers from `0` to `max_num` as variable `N`, calls
78
-
the test method functions for each of those numbers.
79
-
* Since we're calling the each test method functions `N` times each, this means
80
-
we have function call overhead of `max_num` calls per test method.
81
-
82
-
#### CPython Inline
83
-
* Defined in [Find_Nth_Prime_Python_Inline.py](Find_Nth_Prime_Python_Inline.py)
84
-
* Loops over all numbers from `0` to `max_num` as variable `N`, calls
85
-
the test method functions for each of those numbers.
86
-
* Since we're calling the each test method functions `N` times each, this means
87
-
we have function call overhead of `max_num` calls per test method.
88
-
89
-
#### CPython through Functions with Lambdas
90
-
* Defined in [Find_Nth_Prime_Python_Lambda.py](Find_Nth_Prime_Python_Lambda.py)
91
-
* Only calls the calculation function once with a `max_num` as a parameter.
92
-
* The calculation function handles all the logic that `Find_Nth_Prime_Python.py`'s the calculation functions have, but all using a single lambda function inside the calculation function.
93
-
* This reduces the function call overhead, which can lead to significant time
94
-
savings.
95
-
96
-
#### CPython through Functions with Lambdas
97
-
* Defined in [Find_Nth_Prime_Python_Inline_Lambda.py](Find_Nth_Prime_Python_Inline_Lambda.py)
98
-
* Only calls the calculation function once with a `max_num` as a parameter.
99
-
* The calculation function handles all the logic that `Find_Nth_Prime_Python.py`'s the calculation functions have, but all using a single lambda function inside the calculation function.
100
-
* This reduces the function call overhead, which can lead to significant time
101
-
savings.
102
-
103
-
#### CPython with LRU Caching
104
-
* Defined in [Find_Nth_Prime_Python_LRU.py](Find_Nth_Prime_Python_LRU.py)
105
-
* Similar to the [Python](#cpython) in that the
106
-
test method functions are each called `max_num` amount of times, but this
107
-
implementation caches the results of previous function calls to speed up the
108
-
calculation of the current function call.
109
-
* Despite the supposed function call overhead
110
-
111
-
#### CPython with Numpy Arrays
112
-
* Defined in [Find_Nth_Prime_Python_Numpy.py](Find_Nth_Prime_Python_Numpy.py)
113
-
* Same as [Python](#cpython), but uses Numpy arrays instead of Python lists.
114
-
115
-
#### CPython with Numpy Arrays and Lambdas
116
-
* Defined in [Find_Nth_Prime_Python_Numpy_Lambdas.py](Find_Nth_Prime_Python_Numpy_Lambdas.py)
117
-
* Same as [Python with Lambdas](#cpython-with-lambdas), but uses Numpy arrays
118
-
instead of Python lists.
119
-
120
-
#### CPython with Numpy Arrays and LRU Caching
121
-
* Defined in [Find_Nth_Prime_Python_Numpy_LRU.py](Find_Nth_Prime_Python_Numpy_LRU.py)
122
-
* Same as [Python with Lambdas](#cpython-with-lambdas), but uses Numpy arrays
123
-
instead of Python lists.
124
-
125
-
#### CPython Cython
126
-
* Defined in [Find_Nth_Prime_Cython.pyx](Find_Nth_Prime_Cython.pyx)
127
-
* Same as [Python](#cpython), but compiled using Cython.
128
-
129
-
#### CPython Cython with Lambdas
130
-
* Defined in [Find_Nth_Prime_Cython_Lambda.pyx](Find_Nth_Prime_Cython_Lambda.pyx)
131
-
* Same as [Python with Lambdas](#cpython-with-lambdas), but compiled using Cython.
132
-
133
-
#### CPython Cython with LRU Caching
134
-
* Defined in [Find_Nth_Prime_Cython_LRU.pyx](Find_Nth_Prime_Cython_LRU.pyx)
135
-
* Same as [Python with LRU Caching](#cpython-with-lru-caching), but compiled using Cython.
136
-
137
-
#### CPython Cython with Numpy Arrays
138
-
* Defined in [Find_Nth_Prime_Cython_Numpy.pyx](Find_Nth_Prime_Cython_Numpy.pyx)
139
-
* Same as [Cython](#cython), but uses Numpy arrays instead of Python lists.
140
-
141
-
#### CPython Cython with Numpy Arrays and Lambdas
142
-
* Defined in [Find_Nth_Prime_Cython_Numpy_Lambda.pyx](Find_Nth_Prime_Cython_Numpy_Lambda.pyx)
143
-
* Same as [Cython with Lambdas](#cython-with-lambdas), but uses Numpy arrays
144
-
instead of Python lists.
145
-
146
-
#### CPython Cython with Numpy Arrays and LRU Caching
147
-
* Defined in [Find_Nth_Prime_Cython_Numpy_LRU.pyx](Find_Nth_Prime_Cython_Numpy_LRU.pyx)
148
-
* Same as [Cython with Lambdas](#cython-with-lambdas), but uses Numpy arrays
149
-
instead of Python lists.
150
-
151
-
#### CPython Optimized Cython
152
-
* Defined in [Find_Nth_Prime_Optimized.pyx](Find_Nth_Prime_Optimized.pyx)
153
-
* Uses manual static typing for variables for slightly better performance.
154
-
155
-
#### CPython Optimized Cython with Lambdas
156
-
* Defined in [Find_Nth_Prime_Optimized_Lambda.pyx](Find_Nth_Prime_Optimized_Lambda.pyx)
157
-
* Uses manual static typing for variables for slightly better performance.
158
-
159
-
#### CPython Optimized Cython with LRU Caching
160
-
* Defined in [Find_Nth_Prime_Optimized_LRU.pyx](Find_Nth_Prime_Optimized_LRU.pyx)
161
-
* Uses manual static typing for variables for slightly better performance.
162
-
163
-
#### CPython Optimized Cython with Numpy Arrays
164
-
* Defined in [Find_Nth_Prime_Optimized_Numpy.pyx](Find_Nth_Prime_Optimized_Numpy.pyx)
165
-
* Uses manual static typing for variables for slightly better performance.
166
-
167
-
#### CPython Optimized Cython with Numpy Arrays and Lambdas
168
-
* Defined in [Find_Nth_Prime_Optimized_Numpy_Lambda.pyx](Find_Nth_Prime_Optimized_Numpy_Lambda.pyx)
169
-
* Uses manual static typing for variables for slightly better performance.
170
-
171
-
#### CPython Optimized Cython with Numpy Arrays and LRU Caching
172
-
* Defined in [Find_Nth_Prime_Optimized_Numpy_LRU.pyx](Find_Nth_Prime_Optimized_Numpy_LRU.pyx)
173
-
* Uses manual static typing for variables for slightly better performance.
174
-
175
-
### Anaconda Variants
176
-
There are no differences in the code between these implementations and the CPython ones.
177
-
The only difference is that the exact same CPython tests were run with the
178
-
Anaconda Python runtime rather than the default CPython runtime.
179
-
* <b>Anaconda</b>: [CPython](#cpython)
180
-
* <b>Anaconda with Lambdas</b>: [CPython w/ Lambdas](#cpython-with-lambdas),
181
-
* <b>Anaconda with LRU Caching</b>: [CPython w/ LRU Caching](#cpython-with-lru-caching)
182
-
* <b>Anaconda with Numpy Arrays</b>: [CPython w/ Numpy Arrays](#cpython-with-numpy-arrays)
183
-
* <b>Anaconda with Numpy Arrays and Lambdas</b>: [CPython w/ Numpy Arrays & Lambdas](#cpython-with-numpy-arrays-and-lambdas)
0 commit comments