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
4
votes
2answers
2k views
6
votes
1answer
92 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 ...
16
votes
2answers
609 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: ...
2
votes
1answer
96 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 ...
4
votes
1answer
128 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 ...
5
votes
1answer
422 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 ...
3
votes
0answers
63 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 ...
4
votes
1answer
43 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 ...
6
votes
0answers
74 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 ...
7
votes
1answer
2k 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 ...
4
votes
2answers
62 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 ...
3
votes
1answer
214 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 ...
4
votes
3answers
788 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-...
7
votes
2answers
615 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 ...
2
votes
0answers
598 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 ...
3
votes
1answer
330 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 (...
1
vote
1answer
43 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 ...
1
vote
0answers
57 views

Calculating the number of matches for a tuple, a pair of nodes and a mapping

For a current node_pair, this program computes the number of variables matches it has. The matches for a node pair combined with a key are stored in a weight_dictionary with keys for each node pair. ...
6
votes
1answer
242 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 ...
3
votes
2answers
92 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 ...
5
votes
2answers
468 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: ...
4
votes
0answers
475 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 ...
17
votes
2answers
81k views

Fastest way to iterate over Numpy array

I wrote a function to calculate the gamma coefficient of a clustering. The bottleneck is the comparison of values from dist_withing to ...
3
votes
0answers
134 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 ...
12
votes
3answers
713 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...
6
votes
2answers
1k 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 ...
8
votes
2answers
2k 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 ...
6
votes
0answers
739 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 ...
3
votes
2answers
15k 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. ...
3
votes
0answers
140 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 ...
3
votes
1answer
528 views

Where is the bottleneck in my Cython code?

The profile tells me it took ~15s to run, but without telling me more. ...
3
votes
1answer
446 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 ...
4
votes
1answer
133 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 ...
3
votes
2answers
368 views

Calculating the push or pull force of a molecule at a vertex

The basic idea of the code is to calculate the push or pull force at a vertex, given the number of "push causing molecules" and "pull causing molecules" at a polygon vertex. The code thus mainly ...
5
votes
1answer
890 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 ...
12
votes
1answer
603 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 ...
4
votes
1answer
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 ...
3
votes
1answer
3k 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 ...
4
votes
1answer
991 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....
4
votes
1answer
243 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 ...
3
votes
0answers
361 views

Cythonized version of FDCT (fast discrete cosine transform) function, ported from Java

This is my Cython code for an FDCT function (invoked here): ...
3
votes
1answer
74 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 ...
7
votes
3answers
5k views

Understanding the differences: A direct comparison between Cython and C++ [closed]

Introduction: The following two code samples are a direct comparison of performance between Cython and C++. I pieced them together with the help of others-- I barely understand how they work. Their ...
2
votes
2answers
3k views

Fastest / most efficient random bit indexing in Python

Using Python/Cython, I am trying to construct an efficient way to index boolean values. I have been trying to use the bitarray and/or bitstring packages to construct a fast, non memory intensive way ...
6
votes
1answer
3k views

Optimize Cython code with np.ndarray contained

Can someone help me further optimize the following Cython code snippets? Specifically, a and b are ...
3
votes
1answer
4k views

Speedup cython dictionary counter

I wrote a simple cython script to optimize the collections.Counter of a dictionary counter and the python zip implementation (the main input is a list of tuples). Is there a way to speed it up? ...
4
votes
2answers
1k views

Speeding up a Cython program

I wrote the following piece of Python as a part of a larger system. Profiling reveals that a large amount of time is spent in DocumentFeature.from_string. So far I'...
1
vote
2answers
1k views

Is my Encryption Module Secure? [closed]

I have a simple encryption module that is used to "encrypt" a text file because it contains some passwords. I only need to encrypt it because I need those passwords in my program (they are used to ...