in Education by
I need to get the results of the following function getScore <- function(history, similarities) { nh<-ifelse(similarities<0, 6-history,history) x <- nh*abs(similarities) contados <- !is.na(history) x2 <- sum(x, na.rm=TRUE)/sum(abs(similarities[contados]),na.rm=TRUE) x2 } For example for the following vectors: notes <- c(1:5, NA) history <- sample(notes, 1000000, replace=T) similarities <- runif(1000000, -1,1) That changes inside a loop. This takes: ptm <- proc.time() for (i in (1:10)) getScore(history, similarities) proc.time() - ptm user system elapsed 3.71 1.11 4.67 Initially I suspect that the problem is the for loop, but profiling result points to ifelse(). Rprof("foo.out") for (i in (1:10)) getScore(history, similarities) Rprof(NULL) summaryRprof("foo.out") $by.self self.time self.pct total.time total.pct "ifelse" 2.96 65.78 3.48 77.33 "-" 0.24 5.33 0.24 5.33 "getScore" 0.22 4.89 4.50 100.00 "<" 0.22 4.89 0.22 4.89 "*" 0.22 4.89 0.22 4.89 "abs" 0.22 4.89 0.22 4.89 "sum" 0.22 4.89 0.22 4.89 "is.na" 0.12 2.67 0.12 2.67 "!" 0.08 1.78 0.08 1.78 $by.total total.time total.pct self.time self.pct "getScore" 4.50 100.00 0.22 4.89 "ifelse" 3.48 77.33 2.96 65.78 "-" 0.24 5.33 0.24 5.33 "<" 0.22 4.89 0.22 4.89 "*" 0.22 4.89 0.22 4.89 "abs" 0.22 4.89 0.22 4.89 "sum" 0.22 4.89 0.22 4.89 "is.na" 0.12 2.67 0.12 2.67 "!" 0.08 1.78 0.08 1.78 $sample.interval [1] 0.02 $sampling.time [1] 4.5 ifelse() is my performance bottleneck. Unless there is a way in R to speed up ifelse(), there is unlikely to be great performance boost. However, ifelse() is already the vectorized approach. It seems to me that the only chance left is to use C/C++. But is there a way to avoid using compiled code? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use logical multiplication for this task to achieve the same effect: s <- similarities < 0 nh <- s*(6-history) + (!s)*history Benchmark on i7-3740QM: f1 <- function(history, similarities) { s <- similarities < 0 s*(6-history) + (!s)*history} f2 <- function(history, similarities) ifelse(similarities<0, 6-history,history) f3 <- function(history, similarities) { nh <- history ind <- similarities<0 nh[ind] <- 6 - nh[ind] nh } microbenchmark(f1(history, similarities), f2(history, similarities), f3(history, similarities)) ## Unit: milliseconds ## expr min lq mean median uq max neval cld ## f1(history, similarities) 22.830260 24.6167695 28.31384860 24.89869950000000 25.651655 81.043713 100 a ## f2(history, similarities) 364.514460 412.7117810 408.37156626 415.10114899999996 417.345748 437.977256 100 c ## f3(history, similarities) 84.220279 86.2894795 92.64614571 87.18016549999999 89.616522 149.243051 100 b On E5-2680 v2: ## Unit: milliseconds ## expr min lq mean median uq max neval cld ## f1(history, similarities) 20.03963 20.10954 21.41055 20.68597 21.25920 50.95278 100 a ## f2(history, similarities) 314.54913 315.96621 324.91486 319.50290 325.93168 378.26016 100 c ## f3(history, similarities) 73.81413 73.92162 76.10418 74.79893 75.84634 105.98770 100 b On T5600 (Core2 Duo Mobile): ## Unit: milliseconds expr min lq mean median uq max neval cld ## f1(history, similarities) 147.2953 152.9307 171.0870 155.5632 167.0998 344.7524 100 b ## f2(history, similarities) 408.5728 493.3886 517.0573 501.6993 525.8573 797.9624 100 c ## f3(history, similarities) 102.9621 110.6003 131.1826 112.9961 125.3906 303.1170 100 a Aha! My approach is slower on the Core 2 architecture.

Related questions

0 votes
    Sending an E-mail is similar to- (A) writing a letter (B) drawing a picture (C) talking on the phone (D) ... package (E) None of these Select the correct answer from above options...
asked Dec 2, 2021 in Education by JackTerrance
0 votes
    What kind of etiquettes are proof reading? (A) reading (B) E mail (C) writing (D) editing Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    What kind of etiquettes are proof reading? (A) reading (B) E mail (C) writing (D) editing Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A filter which passes without attenuation all frequencies up to the cut-off frequency fc and attenuates all other ... EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    5. Identify the conjunctions from the following Under, And, In, AL, Or, Up. On (a) And, Or (b) At ... answer is marked as brainlist Select the correct answer from above options...
asked Dec 20, 2021 in Education by JackTerrance
0 votes
    Physical components that make up your computer are known as- (A) Operating Systems (B) Software (C) Hardware (D) ... (E) None of these Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
0 votes
    Complete the list by writing the contribution made by the following leaders. For example, Lai Bahadur Shastri: Tashkent ... Bihari Vajpayee :.... Please answer the above question....
asked Aug 18, 2022 in Education by JackTerrance
0 votes
    I am trying to write a basic program that will sum up the filesizes of all files in the current ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I'm certain I'm missing something obvious, but the gist of the problem is I'm receiving a PNG ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I am loading a (5gb compressed file) into memory (aws), creating a dataframe(in spark) and trying ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    After preparing data from a dataset, I want to save the prepared data using h5py. The data is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Which of these exception is thrown in cases when the file specified for writing is not found? (a) IOException (b ... & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
...