Skip to main content
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 ...
Saullo G. P. Castro's user avatar
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 ...
RootTwo's user avatar
  • 10.7k
5 votes
Accepted

Fast complex absolute argmax in Cython

In a generated code (slightly edited for readability) ...
vnp's user avatar
  • 58.7k
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 ...
Peilonrayz's user avatar
  • 44.6k
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 ...
mle's user avatar
  • 171
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. ...
Graipher's user avatar
  • 41.7k
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 <...
J_H's user avatar
  • 42.3k
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 ...
J_H's user avatar
  • 42.3k
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 ...
Reinderien's user avatar
  • 71.1k
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 ...
Luca's user avatar
  • 201
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. ...
FreezePhoenix's user avatar
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 ...
Mingrui Jiang's user avatar
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 ...
DavidW's user avatar
  • 141
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 \...
Graipher's user avatar
  • 41.7k
2 votes

Fast complex absolute argmax in Cython

Thanks to vnp's answer; here's np.argmax(np.abs(x)) vs Numba vs Cython: ...
OverLordGoldDragon's user avatar
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 ...
InusualZ's user avatar
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 ...
Manuel's user avatar
  • 1,030
2 votes
Accepted

Cython Fibonacci Sequence

Using one less variable seems faster: ...
Marc's user avatar
  • 5,734
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 ...
Golden Rockefeller's user avatar
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 ...
QuasiStellar's user avatar
  • 2,327
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__. ...
Reinderien's user avatar
  • 71.1k
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 ...
Toby Speight's user avatar
  • 88.4k
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 ...
Reinderien's user avatar
  • 71.1k
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): ...
M Juckes's user avatar
  • 314
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 ...
CodeSurgeon's user avatar
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 ...
dfhwze's user avatar
  • 14.2k
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 ...
Reinderien's user avatar
  • 71.1k
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 <...
Eric Duminil's user avatar
  • 3,991
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 ...
nicoco's user avatar
  • 389
1 vote

3D Connected Component in Cython

You can try adding annotate=True to your cythonize call, like so: ...
rlee827's user avatar
  • 323

Only top scored, non community-wiki answers of a minimum length are eligible