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 8881129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:17:00+00:00 2026-06-14T20:17:00+00:00

If i have a condition, does putting it inside/outside an apply function affect run

  • 0

If i have a condition, does putting it inside/outside an apply function affect run time significantly for example:

names = c("Joe", "Jen", "Bob")

if("Joe" %in% names){
     lapply(1:1000000, function(y){
        #Do something computationally intensive
     })
}
if("Jen" %in% names){
    lapply(1:1000000, function(y){
        #Do something computationally intensive
    })
}

Versus:

lapply(1:1000000, function(y){
    if("Joe" %in% names){
        #Do something computationally intensive
    }
    if("Jen" %in% names){
        #Do something computationally intensive
    }
})

Thanks

  • 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-14T20:17:02+00:00Added an answer on June 14, 2026 at 8:17 pm

    The if in the loop is very expensive.

    Use rbenchmark to see. Writing the first as function ‘a’, the second as ‘b’, gives this:

    > benchmark(a(), b(), replications=1)
      test replications elapsed relative user.self sys.self user.child sys.child
    1  a()            1   0.595     1.00     0.593    0.000          0         0
    2  b()            1   4.141     6.96     4.121    0.001          0         0
    

    Suggested to put both “Joe” and “Jen” into names. The results are about the same.

    > benchmark(a(), b(), replications=1)
      test replications elapsed relative user.self sys.self user.child sys.child
    1  a()            1   0.600    1.000     0.597        0          0         0
    2  b()            1   4.036    6.727     4.016        0          0         0
    

    EDIT: Note that the expressions that you give to benchmark are evaluated inside the timing loop, and thus function parentheses must be supplied.

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

Sidebar

Related Questions

I have code that all over the place(probably 20-30 instances) does this: <widget>.setVisible((condition ==
I have following condition: How do I send email to example@mail.com on submitButton clicklistener
I have a logical condition that does not work. it would compare two characters,
I have a condition such as 'a==1' stored in property $(c) and I wanna
I have if condition and I want to disable or enable my actionLink button.
Say, you have a condition like this; if ($condition_1 && $condition_2) and you have
Im facing following problem I have created mentioned condition, but when I choose y
Possible Duplicate: php false place in condition I have noticed that a lot of
I have the following if condition statement if ( (strlen($data[70])>0) || ( (remove19((trim($data[29])) ==
i have a simple if condition as follows... $site = new-object Microsoft.SharePoint.SPSite($url) $sitelists =

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.