for-loop

OpenMP - Why does the number of comparisons decrease?

你。 提交于 2019-12-31 04:07:09
问题 I have the following algorithm: int hostMatch(long *comparisons) { int i = -1; int lastI = textLength-patternLength; *comparisons=0; #pragma omp parallel for schedule(static, 1) num_threads(1) for (int k = 0; k <= lastI; k++) { int j; for (j = 0; j < patternLength; j++) { (*comparisons)++; if (textData[k+j] != patternData[j]) { j = patternLength+1; //break } } if (j == patternLength && k > i) i = k; } return i; } When changing num_threads I get the following results for number of comparisons:

replace loops with apply family functions (or dplyr), using logical functions in R

大城市里の小女人 提交于 2019-12-31 04:02:26
问题 I have created this representative data frame that assigns condition categories using a for loop. df <- data.frame(Date=c("08/29/2011", "08/29/2011", "08/30/2011", "08/30/2011", "08/30/2011", "08/29/2012", "08/29/2012", "01/15/2012", "08/29/2012"), Time=c("09:45", "10:00", "13:00", "13:30", "10:14", "9:09", "11:23", "17:06", "12:20"), Diff = c(0.2,4.3,6.5,15.0, 16.5, 31, 30.2, 21.9, 1.9)) df1<- df %>% mutate(Accuracy=ifelse(Diff<=3, "Excellent", "TBD")) for(i in 1:nrow(df1)){ if(df1$Diff[i]>3

Convert a for loop into a vector (vectorization)

做~自己de王妃 提交于 2019-12-31 03:43:09
问题 For those super experts out there, I was wondering if you see a quick way to convert the following "for" loop into a one-line vector calculation that is more efficient. %Define: %A size (n,1) %B size (n,m) %C size (n,1) B = [2 200; 3 300; 4 400]; C = [1;2;1]; for j=1:n A(j) = B( j, C(j) ); end So to be clear, is there any alternative way to express A, as a function of B and C, without having to write a loop? 回答1: Yes, there is: A = B(sub2ind([n,m], (1:n).', C)); 回答2: It depends on functions A

Disadvantage of for loop [closed]

与世无争的帅哥 提交于 2019-12-31 03:34:06
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . What are the actual disadvantages of for loop? No, I mean seriously. There must be something right. There are while and do while loops, both effective, yet we have a for loop. There must be some disadvantage in for loop due to which while and do while were developed, right?

rsync in shell for loop

喜你入骨 提交于 2019-12-31 03:31:07
问题 I have this shell script #!/bin/sh PATHS=( a b c d ) for PATH in ${PATHS[@]} do rsync -avziP /home/user/$PATH $SERVER:$server_folder -b --backup-dir=$backup_folder/backup_$date --delete --exclude=.* --log-file=$HOME/rsync.log done And I always get this error: rsync: command not found What is driving me crazy is that if I delete the for loop, and just run the rsync command, the script works perfectly 回答1: PATH is a reserved variable! It is the variable specifying where to search tools (like

Iterate again within the for loop

北战南征 提交于 2019-12-31 03:21:48
问题 is there a way to iterate again within the for loop? For example: for x in list: if(condition): #I'd like to grab the next iteration of the list So if I had [1,2,3,4], I'd be iterating over 1 first, then trying to advance the iteration to 2 within the for loop so that when the loop started again, it'd be at 3. Possible? I'm creating a parser that reads an if statement, then wants to read lines up until it hits a line that terminates the if statement. 回答1: You could do something like this: a =

for loop without condition checking

瘦欲@ 提交于 2019-12-31 03:03:20
问题 I was reading through this question, I am not able to grasp the concept used for the 'for loop' Generally, syntax of for loop is for(assign value, check condition, increment){} They have used the for loop but there is no condition checking, how does this work? for(var i = arr1.length; i--;) { if(arr1[i] !== arr2[i]) return false; } 回答1: Actually, it's for ([initialization]; [condition]; [final-expression]) where all three expressions are optional . In this case, i-- is a condition, when it

Spark Python: How to calculate Jaccard Similarity between each line within an RDD?

时光怂恿深爱的人放手 提交于 2019-12-31 02:42:27
问题 I have a table of around 50k distinct rows, and 2 columns. You can think of each row being a movie, and columns being the attributes of that movie - "ID": id of that movie, "Tags":some content tags of the movie, in form of a list of strings for each movie . Data looks something like this: movie_1, ['romantic','comedy','English']; movie_2, ['action','kongfu','Chinese'] My goal is to first calculate the jacquard similarity between each Movie based on their corresponding tags, and once that's

Spark Python: How to calculate Jaccard Similarity between each line within an RDD?

扶醉桌前 提交于 2019-12-31 02:41:46
问题 I have a table of around 50k distinct rows, and 2 columns. You can think of each row being a movie, and columns being the attributes of that movie - "ID": id of that movie, "Tags":some content tags of the movie, in form of a list of strings for each movie . Data looks something like this: movie_1, ['romantic','comedy','English']; movie_2, ['action','kongfu','Chinese'] My goal is to first calculate the jacquard similarity between each Movie based on their corresponding tags, and once that's

Can this python code be more efficient?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 01:56:31
问题 I have written some code to find how many substrings of a string are anagram pairs. The function to find anagram(anagramSolution) is of complexity O(N). The substring function has complexity less than N square. But, this code here is the problem. Can it be more optimized? for i in range(T): x = raw_input() alist = get_all_substrings(x) for k, j in itertools.combinations(alist,2): if(len(k) == len(j)): if(anagramSolution(k,j)): counter +=1 counterlist.append(counter) counter = 0 The alist can