vectorization

Why is vectorization faster

蓝咒 提交于 2019-12-28 16:26:47
问题 I've been learning R for a while now, and have come across a lot of advice to programming types like myself to vectorize operations. Being a programmer, I'm interested as to why / how it's faster. An example: n = 10^7 # populate with random nos v=runif(n) system.time({vv<-v*v; m<-mean(vv)}); m system.time({for(i in 1:length(v)) { vv[i]<-v[i]*v[i] }; m<-mean(vv)}); m This gave user system elapsed 0.04 0.01 0.07 [1] 0.3332091 user system elapsed 36.68 0.02 36.69 [1] 0.3332091 The most obvious

Why is vectorization faster

拈花ヽ惹草 提交于 2019-12-28 16:26:43
问题 I've been learning R for a while now, and have come across a lot of advice to programming types like myself to vectorize operations. Being a programmer, I'm interested as to why / how it's faster. An example: n = 10^7 # populate with random nos v=runif(n) system.time({vv<-v*v; m<-mean(vv)}); m system.time({for(i in 1:length(v)) { vv[i]<-v[i]*v[i] }; m<-mean(vv)}); m This gave user system elapsed 0.04 0.01 0.07 [1] 0.3332091 user system elapsed 36.68 0.02 36.69 [1] 0.3332091 The most obvious

Vectorizing nested loops in matlab using bsxfun and with GPU

南笙酒味 提交于 2019-12-28 16:26:01
问题 For loops seem to be extremely slow, so I was wondering if the nested loops in the code shown next could be vectorized using bsxfun and maybe GPU could be introduced too. Code %// Paramaters i = 1; j = 3; n1 = 1500; n2 = 1500; %// Pre-allocate for output LInc(n1+n2,n1+n2)=0; %// Nested Loops - I for x = 1:n1 for y = 1:n1 num = ((n2 ^ 2) * (L1(i, i) + L2(j, j) + 1)) - (n2 * n * (L1(x,i) + L1(y,i))); LInc(x, y) = L1(x, y) + (num/denom); LInc(y, x) = LInc(x, y); end end %// Nested Loops - II for

How to speed up or vectorize a for loop?

吃可爱长大的小学妹 提交于 2019-12-28 16:02:29
问题 I would like to increase the speed of my for loop via vectorization or using Data.table or something else. I have to run the code on 1,000,000 rows and my code is really slow. The code is fairly self-explanatory. I have included an explanation below just in case. I have included the input and the output of the function. Hopefully you will help me make the function faster. My goal is to bin the vector "Volume", where each bin is equal to 100 shares. The vector "Volume" contains the number of

Is there a vectorized parallel max() and min()?

你说的曾经没有我的故事 提交于 2019-12-28 03:56:06
问题 I have a data.frame with columns "a" and "b". I want to add columns called "high" and "low" that contain the highest and the lowest among columns a and b. Is there a way of doing this without looping over the lines in the dataframe? edit: this is for OHLC data, and so the high and low column should contain the highest and lowest element between a and b on the same line, and not among the whole columns. sorry if this is badly worded. 回答1: Sounds like you're looking for pmax and pmin ("parallel

Why doesn't outer work the way I think it should (in R)?

有些话、适合烂在心里 提交于 2019-12-28 02:06:43
问题 Prompted by @hadley's article on functionals referenced in an answer today, I decided to revisit a persistent puzzle about how the outer function works (or doesn't). Why does this fail: outer(0:5, 0:6, sum) # while outer(0:5, 0:6, "+") succeeds This shows how I think outer should handle a function like sum : Outer <- function(x,y,fun) { mat <- matrix(NA, length(x), length(y)) for (i in seq_along(x)) { for (j in seq_along(y)) {mat[i,j] <- fun(x[i],y[j])} } mat} > Outer(0:5, 0:6, `+`) [,1] [,2]

Fastest way to compute absolute value using SSE

孤街醉人 提交于 2019-12-27 14:00:41
问题 I am aware of 3 methods, but as far as I know, only the first 2 are generally used: Mask off the sign bit using andps or andnotps . Pros: One fast instruction if the mask is already in a register, which makes it perfect for doing this many times in a loop. Cons: The mask may not be in a register or worse, not even in a cache, causing a very long memory fetch. Subtract the value from zero to negate, and then get the max of the original and negated. Pros: Fixed cost because nothing is needed to

Vectorized NumPy linspace for multiple start and stop values

会有一股神秘感。 提交于 2019-12-27 12:06:10
问题 I need to create a 2D array where each row may start and end with a different number. Assume that first and last element of each row is given and all other elements are just interpolated according to length of the rows In a simple case let's say I want to create a 3X3 array with same start at 0 but different end given by W below: array([[ 0., 1., 2.], [ 0., 2., 4.], [ 0., 3., 6.]]) Is there a better way to do this than the following: D=np.ones((3,3))*np.arange(0,3) D=D/D[:,-1] W=np.array([2,4

How to convert a nested loop into parfor loop

℡╲_俬逩灬. 提交于 2019-12-25 18:12:46
问题 This is my from my MATLAB script. function [ Im ] = findBorders( I ) Im = false(size(I)); I = padarray(I, [1, 1], 1); [h w] = size(Im); bkgFound = false; for row = 1 : h for col = 1 : w if I(row + 1, col + 1) bkgFound = false; for i = 0:2 for j = 0:2 if ~I(row + i, col + j) Im(row, col) = 1; bkgFound = true; break; end; end; if bkgFound break; end; end; end; end; end; end So, I need to convert it to parfor loop, to run into GPU. I need help. I read some articles, but have no idea about how to

Matlab: Filling up matrix rows using moving intervals from a column vector without a for loop

若如初见. 提交于 2019-12-25 16:58:17
问题 I built a function for outliers detection and it worked quite well, but given the huge amount of data I'm working on I needed to remove the "for loop", so here we have the vectorized version (or at least what I think is a vectorized version of my code). Calling the function the following parameters are intialized by the user, I am working with the following: alpha=3 gamma=0.5 k=5 The series "price" exist in the workspace, is linked when calling the function. I think I almost did it but I am