9 votes

My blit function for my own graphics library

it seems to be optimized quite well by the compiler, as it uses a lot of vectorized instructions Well, kind of. GCC didn't use vectorization at all, and Clang used some of it but in a very strange ...
user avatar
  • 6,359
8 votes
Accepted

Scaling and incrementing non-zero elements of a NumPy matrix

This is a typical use case for numpy.where: cPrime = np.where(C, C * 30 + 1, C) This is about twice as fast as ...
user avatar
  • 49.1k
8 votes
Accepted

Vectorized and Multi Threaded Image Convolution

One problem that immediately jumps out at me is the loop-carried dependency through addps, which has a latency of either 3 or 4 (depending on the processor) while ...
user avatar
  • 6,359
8 votes
Accepted

Implementation of linear regression in Python

Just reviewing normalizeFeatures. Instead of a comment explaining what the function does, write a docstring. (Docstrings are available from the interactive ...
user avatar
  • 49.1k
8 votes

AVX Vectorized Multi-threaded Mandelbrot Renderer

There are some & 0xff operations that are not necessary: (aMask & (~iMask & 0xff)), because the bits reset by ...
user avatar
  • 6,359
7 votes
Accepted

Improving Matlab function within big simulation

Vectorization If you look at all the calls to mean, you see that what you are calculating in each loop is a "Cumulative moving average", but in the oposite ...
user avatar
7 votes
Accepted

Vectorize Matlab sum

Vectorized approach: res = sum(abs(x(:).'.^(2:numel(x)+1))) Read more about vectorization techniques here. Thus, your function would look like this: ...
user avatar
  • 886
7 votes
Accepted

Rolling regressions in R

For each group in your data table, your code computes the coefficient b1 from a linear regression y = b0 + b1*x + epsilon, and ...
user avatar
  • 1,037
7 votes

Biometric matching score calculator

Regarding the vectorized code: permute can be considered a "Zero-cost operation". Logical operations are performed fastest using ...
user avatar
7 votes

My blit function for my own graphics library

There are a few things that can be lifted out of loops. The three screen related variables, screen_height, screen_width, and <...
user avatar
6 votes
Accepted

Optimization of multiple arrays for filtering

There may exist a way to make this particular loop run faster. In fact, I'm sure there probably is... however, it's kind of a futile effort. There's a lot of time also being wasted elsewhere where ...
user avatar
  • 25.2k
6 votes
Accepted

Vector comparison inside for-loop

The following function works without for loops or any function of the *apply family. Furthermore, it does not require additional ...
user avatar
6 votes

Improving Matlab function within big simulation

A few notes: I'm confused about the design of your function. If you are only processing column vectors, why not just process a row vector instead? That's what I would expect when calling this ...
user avatar
  • 21.6k
6 votes
Accepted

Generate random unit vectors around circle

To answer your question, you need to add a new dimension to the ndarray: vecs /= mags[..., np.newaxis] However uniformly ...
user avatar
  • 9,523
6 votes
Accepted

Remove duplicates in list of lists where an item in each list is ignored when determining duplicity

Maybe there's an elegant way for me to instead not delete the e column and still remove duplicates efficiently without considering the e values? Your set-based ...
user avatar
  • 2,738
6 votes
Accepted

Compute a numerical derivative

You can use np.roll to compute the centered differences as a vectorised operation rather than in a for loop: ...
user avatar
5 votes

Generate random unit vectors around circle

If you just want to do this for the unit circle, you can use numpy.random.uniform to choose the angle \$\theta\$ defining the unit vector. If you want to deal with ...
user avatar
  • 16.1k
5 votes
Accepted

Matlab Computer Vision

There's a lot going on in this script, but I've tried to cover all of the important parts. Style and conventions: I think your coding style is very good. You're using nice a descriptive variable ...
user avatar
5 votes
Accepted

Evaluating a series of Legendre polynomials

Compute legendre(n) once per loop iteration instead of twice. Use np.hypot instead of ...
user avatar
  • 49.1k
5 votes

Generic pixel class to seamlessly alpha-blend and convert between different pixel structure layouts

The code seems to get more and more questionable as we read downward. Starting at the bottom: ...
user avatar
4 votes

Efficient element-wise function computation in Python

You can avoid the nested loops using numpy.meshgrid to build a table of entries in x and y, ...
user avatar
  • 49.1k
4 votes
Accepted

Vectorized numpy version of arange with multiple start stop

This code looks good to me: the docstring is clear and the implementation is simple and efficient. So I have only a few minor points. The code doesn't fit in 80 columns, meaning that we have to ...
user avatar
  • 49.1k
4 votes

Extract the largest value for each day from a matrix

A few notes: (I implemented this in Octave, so there may be differences between my code and a proper Matlab implementation, but there shouldn't be). First we care about only unique dates, right? So ...
user avatar
  • 21.6k
4 votes
Accepted

Horizontally concatenating the entries of two matrices, and padding with NaNs

I see two major way to simplify and speed this up. First, rather than copying a to c at every step of the loop, you can define <...
user avatar
  • 2,354
4 votes
Accepted

Converting Array of Floats to UINT8 (`char`) or UINT16 (`unsigned short`) Using SSE4

why not load 4 packed __m128 and then store 32 Pixels at once I think that's 16 pixels, but it's a good plan. The pack instructions were used inefficiently (in the linked question that was not really ...
user avatar
  • 6,359
4 votes

Compute a numerical derivative

You can vectorize the calculation ...
user avatar
4 votes
Accepted

Find the minimum value that data could have had before it was rounded

You can firstly change by difference != 0 and then use na.locf to replace NAs by last ...
user avatar
  • 366
4 votes
Accepted

Determining whether a list of pathways and its genes are all included in another list

If you were to add a cat("hello") at the top of your all_in function, you would find that your function is called 25 times, once ...
user avatar
  • 3,445
4 votes
Accepted

Vectorizing matrix operation instead of for loop in circular matrices

This is easy using numpy.roll, for example: zx = np.roll(x, 1) * (np.roll(x, 2) + np.roll(x, -1)) - x
user avatar
  • 49.1k
4 votes
Accepted

PyTorch Vectorized Implementation for Thresholding and Computing Jaccard Index

I think a different approach is needed to achieve a better performance. The current approach recomputes the Jaccard similarity from scratch for each possible threshold value. However, going from one ...
user avatar
  • 2,281

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