Questions tagged [cython]

The Cython language is a superset of the Python language, used to quickly generate Python C extensions. You should also tag with Python when using this tag.

Filter by
Sorted by
Tagged with
0 votes
0 answers
22 views

cubic spline routine

I tried to implement the cubic spline algorithm in cython. I want to ask how I can make the code more efficient in terms of speed. Any suggestions or references ...
John's user avatar
  • 1
5 votes
3 answers
200 views

Fast complex absolute argmax in Cython

I'm thinking I implemented it optimally, but somehow it's much slower than what should be much slower, np.argmax(np.abs(x)). Where am I off? Code rationale & ...
OverLordGoldDragon's user avatar
6 votes
1 answer
127 views

basic Hartee-Fock program to compute the total energies and some properties of a molecule

I am trying to write a basic Hartee-Fock program that computes the total energies and some properties of a molecule given as an input. The code uses Cython to statically compile the computationally ...
Hemanth Haridas's user avatar
2 votes
0 answers
104 views

Cubic spline in cython

For several projects I need a way to use cubic splines inside cythonized functions and since the scipy functions cannot be used within ...
John's user avatar
  • 279
2 votes
1 answer
160 views

type hinting/documenting/extension of a Cython lib

I've updated some of the type hinting/documentation in a lib called pygrib. The source documentation can be found here. The goal is to extend the ...
Jason Leaver's user avatar
6 votes
1 answer
178 views

Improving performance of radix sort

I am trying to optimize the following (now C) radix sort code for use in my game engine library. The basis for this code was inspired by this youtube video on a C++ implementation called ...
CodeSurgeon's user avatar
3 votes
1 answer
535 views

Fantasy Football (Soccer) Simulation

What my program does My program does simulation for fantasy football. Its inputs are teams and teams results in a particular match and its outputs are how many points each team earned in each match ...
Konstantin Kostanzhoglo's user avatar
5 votes
0 answers
222 views

Pi Calculator Using BBP (Bailey–Borwein–Plouffe) - Written in Cython With MPFR

I wrote a Pi Calculator using the BBP Algorithm in Cython. My specific goals are make the code more legible and more performant. This is a purely academic script in order to teach me how to implement ...
Alexis Evelyn's user avatar
2 votes
2 answers
302 views

Cython Fibonacci Sequence

For my current job, I've been told that soon I'm going to be porting our Python code to Cython, with the intent of performance upgrades. In preparation of that, I've been learning as much about Cython ...
Linny's user avatar
  • 9,977
4 votes
0 answers
901 views

Slow copying of memmap array to numpy array

I have multiple binary (structured) file, each of 2GB, which I am currently reading in pair, using memmap to cross-correlate the same. I want to minimise the time required by this IO process, in the ...
Pavan Uttarkar's user avatar
4 votes
2 answers
4k views

Cython with variable-length arrays

Dynamically growing arrays are a type of array. They are very useful when you don't know the exact size of the array at design time. First you need to define an initial number of elements. (Wikipedia) ...
user4933's user avatar
  • 149
2 votes
0 answers
101 views

Optimizing the Cython code for solving system of ODEs

I am trying to optimize this code for solving a system of ODE. It seems Cython does not speed up the code compared to code using ...
Abolfazl's user avatar
  • 185
1 vote
0 answers
31 views

Cooperator/competitor agent-based simulation

My agent-based simulation spends about 60% of its runtime in one function, as the profiler shows. That function looks as follows. ...
Anaphory's user avatar
  • 201
5 votes
2 answers
115 views

Fast execution of function which resembles matrix multiplication

I am trying to make the following function as fast as possible on large matrices. The matrices can have dimensions in the range of 10K-100K. The matrix values are always between 0 and 1. The function ...
user87687889's user avatar
6 votes
1 answer
409 views

Optimizing a raycasting/voxelspace algorithm in Pygame

In the past three days, I've been developing a simple raycasting algorithm. I've made the renderer work as I'd like to. However, it is extremely slow (5 frames per second) because of two things (as ...
Russell356's user avatar
2 votes
1 answer
439 views

High Speed Python Web Scraper Optimised

I needed a lot of data for a tensorflow project so I made a web scraper to get all of the text and links off of websites then to repeat the process at all of those links. I left it on overnight and ...
GameDungeon's user avatar
4 votes
1 answer
524 views

Cython functions for generating NumPy date ranges

I have rewritten my python code in cython in order to speed up it quite a bit. However after checking the code in jupyter it seems that part of it is still compiled as python code, therefore its not ...
Alex T's user avatar
  • 133
4 votes
0 answers
293 views

Circular doubly linked list implementation in Cython

This is a follow-up from my previous question. In summary, I am trying to implement a BVH module with Cython and need a way to keep track of the volumes during the construction of the hierarchy. I ...
Jeacom's user avatar
  • 125
4 votes
1 answer
139 views

Doubly linked list in Cython

I am implementing a BVH (Bounding volume hierarchy) in Cython and thought of using a doubly linked list to hold reference of the volumes that still need to be split and push new volumes at one end ...
Jeacom's user avatar
  • 125
7 votes
0 answers
306 views

Numerical integration in cython

I have a set of nested functions that I need to call multiple times. I know scipy.quad is pretty fast, but I will need to call the integrator recursively and want ...
Pedro Relich's user avatar
4 votes
2 answers
111 views

Python command line tool for compiling py files to exec files on Mac

When I used to use a PC I was familiar with py2exe, the mac equivalent I saw most people talking about was py2app — which compiles python to .app files, not .exec files. I was familiar with compiling ...
Shui's user avatar
  • 173
7 votes
2 answers
869 views

Computing the gradient of a function

I have some Python code which uses NumPy which computes gradient of a function and this is a big bottleneck in my application. So, my initial attempt was to try to use Cython to improve the ...
Luca's user avatar
  • 201
4 votes
3 answers
1k views

3D Connected Component in Cython

This is my implementation of 3D connected component algorithm, which I use for a 255x512x512 binary matrix. Although it is written in Cython, it still takes quite some time both for 6-neighbors and 26-...
Kaan Yilmaz's user avatar
3 votes
1 answer
558 views

Speed up a simple edge connecting algorithm

I have a list of edges and a similar list of links (linking edges) telling which edges to connect (how to order them). Connected edges form polygons and there can be many disconnected polygons (...
Andreas K.'s user avatar
1 vote
1 answer
59 views

Cythonic Laplacian solving on a 3D regular grid

I have followed to official Cython for NumPy users to make a Laplacian solver on a 3D regular grid with Dirichlet conditions. Right now I'm happy with the results it returns, but I'm wondering if ...
nicoco's user avatar
  • 389
2 votes
0 answers
1k views

Lotto simulator in Python

I wrote a rather inapt lotto simulator in Python, showing how long it would take to win the jackpot. I ran a similar application implemented in C and it is so much faster. I know you can't compare the ...
royskatt's user avatar
  • 203
6 votes
1 answer
293 views

Implementation of Adaptive Rejection Sampling in Cython

I am trying to re-write some Fortran code in Cython which is the implementation of the adaptive rejection sampling method. I'd like to use my Cython version eventually in some Python code. I am ...
Dalek's user avatar
  • 133
3 votes
2 answers
114 views

Cythonized Sutherland-Hogman algorithm

I want to cythonise the python implementation of the Sutherland-Hogman algorithm. This algorithm updates a list of vertices according to pretty simple rules (being inside or outside an edge, etc.) but ...
jeandut's user avatar
  • 133
16 votes
2 answers
862 views

Using lots of regex substitutions to tokenize text

I authored a piece of code that was merged into the nltk codebase. It is full of regex substitutions: ...
alvas's user avatar
  • 705
3 votes
0 answers
822 views

Efficiently writing string comparison functions in Python

Let's say I work for a company that hands out different types of loans. We are getting our loan information from from a big data mart from which I need to calculate some additional things to calculate ...
Tim.Lucas's user avatar
  • 139
3 votes
1 answer
257 views

Cython code for adaptive binning

Here is my Cython code used for adaptive binning. The calcAdaptiveBinnedRateMap function is called from another Python script. The script is compiled using Cython ...
raj's user avatar
  • 31
3 votes
0 answers
144 views

Speed up a Gibbs sampler with Cython

I have implemented a Gibbs sampler to generate textured images. According to the beta parameters (array of shape(4)), we can generate various textures. Here is my ...
floflo29's user avatar
  • 131
5 votes
2 answers
674 views

Weighted Random Choice

I have a Cython function that takes a list of weights/probabilities (double) and returns a random index into the list. For example: ...
Ryan Rosario's user avatar
10 votes
0 answers
1k views

Cyther: The Cross Platform Cython/Python Compiler (Take 2)

I recently posted an earlier version of this code on Code Review, and now with the given suggestions and many other improvements, I am back. I included the description of exactly what Cyther is ...
Nick Pandolfi's user avatar
7 votes
2 answers
3k views

Cyther: The Cross Platform Cython/Python Compiler

The newer version of this question is located here: Cyther: The Cross Platform Cython/Python Compiler (Take 2) I am currently writing a Python library (soon to be published)that automatically compiles ...
Nick Pandolfi's user avatar
13 votes
3 answers
1k views

Fast Python spring network solver

I wanted a very simple spring system written in Python. The system would be defined as a simple network of knots, linked by links...
Fnord's user avatar
  • 383
3 votes
2 answers
22k views

Efficiently index rows of numpy array by exclusion

I need a function that takes a numpy array and a row number as inputs and returns the array (or copy of the array) excluding the given row. I want to do this as efficiently as possible. ...
C_Z_'s user avatar
  • 501
6 votes
2 answers
2k views

Fastest possible Cython for Black-Scholes algorithm

I started with a pure python implementation, and have been trying to get the performance as close to native C as possible using numpy, numexpr, and cython. Here is the the numpy version that I ...
woodenToaster's user avatar
3 votes
0 answers
151 views

Compute the box covering on a graph using CPython

Edit: My initial idea was to use numpy to solve efficiently this problem but after trying without results, someone comment on stackoverflow: "Numpy can speed things up a lot IF your problem is ...
Hernandcb's user avatar
  • 139
13 votes
1 answer
1k views

A big "Game of Life"

Our quest: Create a big simulation for Conway's Game of Life, and record the entire simulation history. Current Approach: Cython is used for an iterate method. The ...
Paul's user avatar
  • 475
4 votes
1 answer
930 views

Chromatic aberration

surface is a pygame.surface object from the Pygame graphics library. It actually runs way faster than I thought it would for a ...
Schilcote's user avatar
  • 213
3 votes
1 answer
11k views

Reading the bytes of a PDF

I'm quite a newbie in Python and I want to speed up this method since it takes very long time especially when the size of the input file in Mbs. Also, I couldn't figure out how to use Cython in the ...
amsr's user avatar
  • 33
9 votes
1 answer
3k views

Create a list of all strings within hamming distance of a reference string with a given alphabet

For a bioinformatic problem, I found myself wanting to create a list of strings within Hamming distance "k" of a reference sequence. I wanted to do so quickly and pythonically. I've implemented it in ...
NoSeatbelts's user avatar
5 votes
1 answer
157 views

Project Euler #10 in Cython

I'm trying to teach myself some Cython. To do so, I use Project Euler #10: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. 2 Find the sum of all the primes below two million. My Cython ...
magu_'s user avatar
  • 948
5 votes
1 answer
1k views

Resource-constrained project scheduling

I'm trying to implement an algorithm for a resource-constrained project scheduling problem. I have several resources, resource constraints and all of this is in ...
Artimi's user avatar
  • 151
5 votes
1 answer
441 views

Finding cycles in a graph that pass through a vertex at most k times

I have a project that relies on finding all cycles in a graph that pass through a vertex at most k times. Naturally, I'm sticking with the case of k=1 for the sake of development right now. I've come ...
Matt Morse's user avatar
4 votes
1 answer
4k views

Fastest computation of N likelihoods on normal distributions

In the context of a Gibbs sampler, I profiled my code and my major bottleneck is the following: I need to compute the likelihood of N points assuming they have been drawn from N normal distributions ...
alberto's user avatar
  • 235
4 votes
1 answer
1k views

BK Tree implementation

I'm working on a system that can search for similar images. This involves being able to search by edit-distance, and as such I've implemented a special data-structure called a BK tree. Some notes here....
Fake Name's user avatar
  • 201
3 votes
1 answer
84 views

Fast counting of interactions between groups given users interactions and groups assignments

Given: An array c that contains the group assignment of every user (c[i]=4 indicates that user i belongs to group 4) A matrix ...
alberto's user avatar
  • 235
4 votes
1 answer
261 views

Calculating the distance squared between all vertex-pairs of a number of 2D polygons

I have implemented my code using Cython. It is the current bottleneck in my computations. There are two non-numpy functions involved: calculate_2D_dist_squared ...
bzm3r's user avatar
  • 399