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

  • SEARCH
  • Home
  • 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 614671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:08:12+00:00 2026-05-13T18:08:12+00:00

…regarding execution time and / or memory. If this is not true, prove it

  • 0

…regarding execution time and / or memory.

If this is not true, prove it with a code snippet. Note that speedup by vectorization does not count. The speedup must come from apply (tapply, sapply, …) itself.

  • 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-05-13T18:08:13+00:00Added an answer on May 13, 2026 at 6:08 pm

    The apply functions in R don’t provide improved performance over other looping functions (e.g. for). One exception to this is lapply which can be a little faster because it does more work in C code than in R (see this question for an example of this).

    But in general, the rule is that you should use an apply function for clarity, not for performance.

    I would add to this that apply functions have no side effects, which is an important distinction when it comes to functional programming with R. This can be overridden by using assign or <<-, but that can be very dangerous. Side effects also make a program harder to understand since a variable’s state depends on the history.

    Edit:

    Just to emphasize this with a trivial example that recursively calculates the Fibonacci sequence; this could be run multiple times to get an accurate measure, but the point is that none of the methods have significantly different performance:

    fibo <- function(n) {
      if ( n < 2 ) n
      else fibo(n-1) + fibo(n-2)
    }
    system.time(for(i in 0:26) fibo(i))
    # user  system elapsed 
    # 7.48    0.00    7.52 
    system.time(sapply(0:26, fibo))
    # user  system elapsed 
    # 7.50    0.00    7.54 
    system.time(lapply(0:26, fibo))
    # user  system elapsed 
    # 7.48    0.04    7.54 
    library(plyr)
    system.time(ldply(0:26, fibo))
    # user  system elapsed 
    # 7.52    0.00    7.58 
    

    Edit 2:

    Regarding the usage of parallel packages for R (e.g. rpvm, rmpi, snow), these do generally provide apply family functions (even the foreach package is essentially equivalent, despite the name). Here’s a simple example of the sapply function in snow:

    library(snow)
    cl <- makeSOCKcluster(c("localhost","localhost"))
    parSapply(cl, 1:20, get("+"), 3)
    

    This example uses a socket cluster, for which no additional software needs to be installed; otherwise you will need something like PVM or MPI (see Tierney’s clustering page). snow has the following apply functions:

    parLapply(cl, x, fun, ...)
    parSapply(cl, X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
    parApply(cl, X, MARGIN, FUN, ...)
    parRapply(cl, x, fun, ...)
    parCapply(cl, x, fun, ...)
    

    It makes sense that apply functions should be used for parallel execution since they have no side effects. When you change a variable value within a for loop, it is globally set. On the other hand, all apply functions can safely be used in parallel because changes are local to the function call (unless you try to use assign or <<-, in which case you can introduce side effects). Needless to say, it’s critical to be careful about local vs. global variables, especially when dealing with parallel execution.

    Edit:

    Here’s a trivial example to demonstrate the difference between for and *apply so far as side effects are concerned:

    df <- 1:10
    # *apply example
    lapply(2:3, function(i) df <- df * i)
    df
    # [1]  1  2  3  4  5  6  7  8  9 10
    # for loop example
    for(i in 2:3) df <- df * i
    df
    # [1]  6 12 18 24 30 36 42 48 54 60
    

    Note how the df in the parent environment is altered by for but not *apply.

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

Sidebar

Ask A Question

Stats

  • Questions 393k
  • Answers 393k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It should just work absolutely fine - but it depends… May 15, 2026 at 2:04 am
  • Editorial Team
    Editorial Team added an answer I would try to build a token filter that: Extracts… May 15, 2026 at 2:04 am
  • Editorial Team
    Editorial Team added an answer new SqlConnection(connectionString) creates a new SqlConnection instance against the supplied… May 15, 2026 at 2:04 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.