6
votes
Accepted
Implementation of Adaptive Rejection Sampling in Cython
Cython Directives
The use of the following Cython directives will remove plenty of overhead from
many parts of your code and make it faster, although it transfers more
responsability for you to check ...
6
votes
Using lots of regex substitutions to tokenize text
3x to 4x speedup using str.translate()
Based on a quick test (see below), str.translate() is an order of magnitude faster than a regular expression for replacing a ...
5
votes
Accepted
Fast complex absolute argmax in Cython
In a generated code (slightly edited for readability)
...
5
votes
Python command line tool for compiling py files to exec files on Mac
I recommend that you read PEP 8.
It's advised against doing import os, re. Instead have one line dedicated to each import.
You should have an empty line between ...
4
votes
Optimizing the Cython code for solving system of ODEs
I have not checked the outputs of the run commands. I know two things to study your code performance.
Use the cython -a command (which I see in your code)
In ...
4
votes
Cython functions for generating NumPy date ranges
I'm sorry, but this code is really hard to read. I must admit I don't know Cython too well, so I won't be able to comment too much on that part. But anyways, here are a few comments, in random order.
...
4
votes
Check that a string is a palindrome using Cython
already fast
The word[i] != word[N - i - 1] test looks Just Fine,
you won't go much faster than that in any language.
(Though I do wish you would
name
it lowercase <...
3
votes
Fast complex absolute argmax in Cython
The OP code suffers from at least a 6x factor of suckage.
The question would benefit from posting
godbolt
links which examine the differences in the generated code.
Often the trouble boils down to
...
3
votes
Accepted
Fantasy Football (Soccer) Simulation
I don't think that your Scoring class is a great place to store constants. (Even if it was, a regular class with statics would be a better fit than a singleton ...
3
votes
Computing the gradient of a function
Ok, after playing around a bit, it turns out that the main thing that will boost speed is using ctypes. Here is the modified code which offers about 13x speedup. I am leaving it here in case it will ...
2
votes
Computing the gradient of a function
So what you want to do is have this run faster.
To begin with, there are several things to improve on.
Let's start with the second code block.
...
2
votes
3D Connected Component in Cython
The is so useful, thank you for making this.
May I suggest one improvement: adding a small zero padding / zero stripping utility.
Because currently, if a connected component is at the edge, looking ...
2
votes
3D Connected Component in Cython
There's quite a bit of code here so I'm not going to be comprehensive, but here are some observations.
Profiling
The first thing you should do is to try to profile your code and work out where time ...
2
votes
Fast execution of function which resembles matrix multiplication
I think you can rearrange your equation from
\$
AB_{ija} = \frac{1}{2}(A_{ia} + B_{ja})
\$
\$
\begin{align}
C_{ij} = \sum_a & A_{ia}\cdot \log\left(\frac{A_{ia}}{AB_{ija}}\right) + B_{ja}\cdot \...
2
votes
Fast complex absolute argmax in Cython
Thanks to vnp's answer; here's np.argmax(np.abs(x)) vs Numba vs Cython:
...
2
votes
Accepted
High Speed Python Web Scraper Optimised
If you want absolute performance
You could avoid printing anything at all. Since that can be slow in some cases. If you absolutly need to know at least what happened you could try flushing at the end ...
2
votes
Cython Fibonacci Sequence
How about unrolling the loop a bit, doing two steps per iteration so you don't need to "swap" a and b? (I'm not ...
2
votes
Accepted
2
votes
Cython with variable-length arrays
In addition to Reinderien's answer, may I suggest:
Yes, Use Memoryviews to speed up access
In addition to the code you have, I would also type the a_mat and ...
2
votes
Accepted
basic Hartee-Fock program to compute the total energies and some properties of a molecule
Let's talk about structure and readability.
Use snake_case for naming, it's a standard practice in Python. nuclear_energy instead of ...
2
votes
Accepted
type hinting/documenting/extension of a Cython lib
Your Reader method has a grib_file member that is missing from the class until __enter__. ...
1
vote
Accepted
Improving performance of radix sort
Some general review:
We're missing an include of <string.h> for memset().
There are some constants that aren't obviously ...
1
vote
Cython with variable-length arrays
Zeros
This:
a_mat = np.zeros
is not the right call for your purpose. You want np.empty instead, because you don't actually care ...
1
vote
Fast execution of function which resembles matrix multiplication
You can simplify this by rearranging the function as follows (writing z=(a+b)/2):
...
1
vote
Optimizing a raycasting/voxelspace algorithm in Pygame
Below is the code I have come up with. It basically follows the code used for the web demo in the github repo you had linked to. I will add some explanation to this answer on how I had made some basic ...
1
vote
Accepted
Doubly linked list in Cython
Empty Constraint
You do not allow to create an empty list.
cdef AABBList* aabb_list_create(AABB* start):
However, the pop ...
1
vote
Python command line tool for compiling py files to exec files on Mac
This code is obviated by @Peilonrayz, which has posted a good solution. However, in the future, if you need to do something like
...
1
vote
Speed up a simple edge connecting algorithm
Just a collection of thoughts...
Terminology
Since you're talking about graphs, it might help the readers if you talk about nodes (your edges) and edges (your <...
1
vote
Accepted
Cythonic Laplacian solving on a 3D regular grid
I was helped on the #scipy IRC channel by user @ngoldbaum and could substantially speed up my code by:
Avoiding to use any numpy ...
1
vote
3D Connected Component in Cython
You can try adding annotate=True to your cythonize call, like so:
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cython × 59python × 54
performance × 40
numpy × 23
beginner × 6
python-3.x × 6
matrix × 3
pandas × 3
signal-processing × 3
compiler × 3
algorithm × 2
array × 2
linked-list × 2
reinventing-the-wheel × 2
graph × 2
computational-geometry × 2
numba × 2
c++ × 1
c × 1
programming-challenge × 1
strings × 1
parsing × 1
python-2.x × 1
recursion × 1
datetime × 1