Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:36:38+00:00 2026-06-17T07:36:38+00:00

The problem with my R script is that it takes too much time and

  • 0

The problem with my R script is that it takes too much time and the main solution that I consider is to parallelize it. I don’t know where to start.

My code look like this:

n<- nrow (aa) 
output <- matrix (0, n, n)

akl<- function (dii){
        ddi<- as.matrix (dii)
        m<- rowMeans(ddi)
        M<- mean(ddi)
        r<- sweep (ddi, 1, m)
        b<- sweep (r, 2, m)
        return (b + M)  
        }
for (i in 1:n)
{
A<- akl(dist(aa[i,]))

dVarX <- sqrt(mean (A * A))

for (j in i:n)
{
    B<- akl(dist(aa[j,]))
        V <- sqrt (dVarX * (sqrt(mean(B * B))))

        output[i,j] <- (sqrt(mean(A * B))) / V        
}
}   

I would like to parallelize on different cpus. How can I do that?
I saw the SNOW package, is it suitable for my purpose?
Thank you for suggestions,
Gab

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:36:39+00:00Added an answer on June 17, 2026 at 7:36 am

    There are two ways in which your code could be made to run faster that I could think of:

    First: As @Dwin was saying (with a small twist), you could precompute akl (yes, not necesarily dist, but the whole of akl).

    # a random square matrix
    aa <- matrix(runif(100), ncol=10)
    n <- nrow(aa)
    output <- matrix (0, n, n)
    
    akl <- function(dii) {
        ddi <- as.matrix(dii)
        m   <- rowMeans(ddi)
        M   <- mean(m) # mean(ddi) == mean(m)
        r   <- sweep(ddi, 1, m)
        b   <- sweep(r, 2, m)
        return(b + M)
    }
    
    # precompute akl here
    require(plyr)
    akl.list <- llply(1:nrow(aa), function(i) {
        akl(dist(aa[i, ]))
    })
    
    # Now, apply your function, but index the list instead of computing everytime
    for (i in 1:n) {
        A     <- akl.list[[i]]
        dVarX <- sqrt(mean(A * A))
    
        for (j in i:n) {
            B <- akl.list[[j]]
            V <- sqrt (dVarX * (sqrt(mean(B * B))))
            output[i,j] <- (sqrt(mean(A * B))) / V        
        }
    }
    

    This should already get your code to run faster than before (as you compute akl everytime in the inner loop) on larger matrices.

    Second: In addition to that, you can get it faster by parallelising as follows:

    # now, the parallelisation you require can be achieved as follows
    # with the help of `plyr` and `doMC`.
    
    # First step of parallelisation is to compute akl in parallel
    require(plyr)
    require(doMC)
    registerDoMC(10) # 10 Cores/CPUs
        akl.list <- llply(1:nrow(aa), function(i) {
        akl(dist(aa[i, ]))
    }, .parallel = TRUE)
    
    # then, you could write your for-loop using plyr again as follows
    output <- laply(1:n, function(i) {
        A     <- akl.list[[i]]
        dVarX <- sqrt(mean(A * A))
    
        t <- laply(i:n, function(j) {
            B <- akl.list[[j]]
            V <- sqrt(dVarX * (sqrt(mean(B*B))))
            sqrt(mean(A * B))/V
        })
        c(rep(0, n-length(t)), t)
    }, .parallel = TRUE)
    

    Note that I have added .parallel = TRUE only on the outer loop. This is because, you assign 10 processors to the outer loop. Now, if you add it to both outer and inner loops, then the total number of processers will be 10 * 10 = 100. Please take care of this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have working registration script the only problem is that i do not know
in a bash script I am implementing some functions, that take parameters The problem
I tweaked this script that I used from JqueryUi and I have a problem.
i've got some jquery script that changes element image on mouseover and the problem
I got a problem with JavaScript. I want a script that will pop-up on
I experience a weird problem; I have a bash script that runs as wwwrun,
Problem When working with MasterPages, a common irritation I run into is that script
I have problem with php script. The thing is that it shows me, that
Problem <script type=text/javascript src=http://localhost/ci/js/global_functions.js></script> <script type=text/javascript src=http://localhost/ci/js/global.js></script> Why can't global.js find a function that
I am using a preview script via http://cssglobe.com/lab/tooltip/03/ The problem is that when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.