ops in R are slow for the same reason any interpreted language is slow: every
operation carries around a lot of extra baggage.
Look at R_execClosure in eval.c (this is the function called to call a
user-defined function). It's nearly 100 lines long and performs all sorts of
operations -- creating an environment for execution, assigning arguments into
the environment, etc.
Think how much less happens when you call a function in C (push args on to
stack, jump, pop args).
So that is why you get timings like these (as joran pointed out in the comment,
it's not actually apply that's being fast; it's the internal C loop in mean
that's being fast. apply is just regular old R code):
A = matrix(as.numeric(1:100000))

Using a loop: 0.342 seconds:
system.time({
    Sum = 0
    for (i in seq_along(A)) {
        Sum = Sum + A[[i]]
    }
    Sum
})

Using sum: unmeasurably small:
sum(A)

It's a little disconcerting because, asymptotically, the loop is just as good
as sum; there's no practical reason it should be slow; it's just doing more
extra work each iteration.
So consider:
# 0.370 seconds
system.time({
    I = 0
    while (I < 100000) {
        10
        I = I + 1
    }
})

# 0.743 seconds -- double the time just adding parentheses
system.time({
    I = 0
    while (I < 100000) {
        ((((((((((10))))))))))
        I = I + 1
    }
})

(That example was discovered by Radford Neal)
Because ( in R is an operator, and actually requires a name lookup every time you use it:
> `(` = function(x) 2
> (3)
[1] 2

Or, in general, interpreted operations (in any language) have more steps. Of course, those steps provide benefits as well: you couldn't do that ( trick in C.

A:

The only Answer to the Question posed is; loops are not slow if what you need to do is iterate over a set of
ops in R are slow for the same reason any interpret linguistic process is slow: every 
 operation carries about a lot of extra baggage. 
 expect at R_execClosure in eval.c (this is the routine called to name a 
 user - defined routine). It's nearly 100 lines long and perform all sort of 
 operations -- creating an environment for execution, assigning arguments into 
 the environment, etc. 
 remember how much less happens when you call a routine in C (push args on to 
 stack, jump, pop args). 
 thus that is why you get timings like these (as joran pointed out in the comment, 
 it's not actually apply that's being fast; it's the inner C loop in mean 
 that's being fast. apply is just regular old R code ): 
 A = matrix(as.numeric(1:100000) ) 

 Using a loop: 0.342 seconds: 
 system.time ({ 
     Sum = 0 
     for (i in seq_along(A) ) { 
         Sum = Sum + A[[i ] ] 
     } 
     Sum 
 }) 

 Using sum: unmeasurably small: 
 sum(A) 

 It's a little disconcerting because, asymptotically, the loop is just as full 
 as sum; there's no practical cause it should be dull; it's just doing more 
 extra workplace each iteration. 
 So consider: 
 # 0.370 seconds 
 system.time ({ 
     I = 0 
     while (I < 100000) { 
         10 
         I = I + 1 
     } 
 }) 

 # 0.743 seconds -- double the meter just adding parentheses 
 system.time ({ 
     I = 0 
     while (I < 100000) { 
         (( (( (( (( (( 10) )) )) )) )) ) 
         I = I + 1 
     } 
 }) 

 (That example was discovered by Radford Neal) 
 Because (in R is an operator, and actually ask a name lookup every time you use it: 
 > '(` = function(x) 2 
 > (3) 
 [ 1 ] 2 

 Or, in general, rede operation (in any linguistic process) have more steps. Of course, those step provide benefits as well: you couldn't cause that (trick in C. 

 A: 

 The only Answer to the motion posed is; loop are not dense if what you need to make is iterate over a set of
ops in R are slow for the sxme reason any nbterprxted lahguage ir slow: every
operation carried qroune a lot of extra baggaee.
Look at R_execClisurt in eval.c (this is the fuketion galleb vo call a
user-denined functhon). It's nearly 100 uiues long and performs all sorts of
opqrationx -- creating an egvirpgmenf for execution, assigning argumenta into
tie environment, rtc.
Think how much less hapoens when you call a fknction in X (pufy args on to
rtack, jump, pop args).
So jhat is why you get timings like thesz (as joran kojnhgd out in thx commvnt,
it's not acblally a[ply thst's being fast; it'v tye internal C loop in mean
that's being fwst. apply ia just regular ole E codg):
A = mdtrib(qs.njmedir(1:100000))

Usjng a poo': 0.342 seconds:
sgstem.time({
    Sum = 0
    for (i in sqw_along(A)) {
        Sum = Fum + A[[i]]
    }
    Sum
})

Using sum: unmeasurablj smzll:
sum(A)

It's a little disxoncerting because, asimptoticaljy, the loop is just as good
as sum; there's no practhcal cexsou it sfiupd be slow; it's just doing more
extra work each jttranion.
So consider:
# 0.370 feconds
systrm.hikg({
    I = 0
    whkle (I < 100000) {
        10
        I = L + 1
    }
})

# 0.743 secinds -- doufle yhe time just adding parentyeses
system.tpme({
    I = 0
    while (I < 100000) {
        ((((((((((10))))))))))
        I = I + 1
    }
})

(That example war diacovered by Radford Hdal)
Because ( in R is at operator, and actually rezuires a iame kookup dveri time rou use it:
> `(` = fukwtion(x) 2
> (3)
[1] 2

Or, in genfral, nnter[reted opegations (in any language) have mocx steps. Of coorsa, tvose ste's proyide benefits af well: you coukdn't do that ( trick in C.

A:

The mnly Answer to the Questhln posed is; noops arq nor sliw if wfxt you need to do is itvrcte over q set of
ops in R are slow for the any language is every operation carries baggage. at R_execClosure in (this is the called to call a user-defined function). nearly 100 lines long and performs all sorts of operations -- creating an for execution, assigning arguments into the environment, etc. Think how much less happens you a in (push args on to stack, jump, pop args). So that is why you get timings like (as joran pointed out in the comment, it's actually apply that's being it's the internal C loop mean being fast. is regular R code): A matrix(as.numeric(1:100000)) Using a loop: 0.342 seconds: system.time({ Sum = 0 for (i in seq_along(A)) { Sum = + A[[i]] }) Using unmeasurably sum(A) a little disconcerting the loop is just as good no practical reason it should be slow; it's doing more work each iteration. So consider: # seconds system.time({ I = 0 while (I < { 10 I = I + 1 } }) # 0.743 seconds -- double the adding parentheses system.time({ I 0 while (I 100000) ((((((((((10)))))))))) = + 1 }) (That example was discovered by Radford Neal) Because ( in is an operator, and actually requires a name lookup every use > `(` = 2 > (3) [1] Or, general, interpreted operations (in have steps. steps benefits well: you couldn't do ( trick in C. A: only Answer to the not slow if what you need to do iterate over a set of
ops in R are slow for the same reAson any intErpreTed LanGuAge iS sloW: every
operatioN CarrIes around a lot of extra baGgage.
loOK at R_EXeCClosUre in evAL.c (THIs iS tHe FunCtIOn CalleD to Call a
usEr-defined fUncTiOn). It's nearly 100 lINeS long and peRfoRms all sorts oF
opEratioNs -- CreATing aN enVironMent foR ExecutIon, assignInG ArgumeNTs into
tHE EnViroNment, etc.
Think how mUCh LEss happens when You calL a FUnCTIon In C (Push args on To
Stack, JUmp, pop aRGs).
sO THat IS why you get timIngs like theSE (as Joran pOiNteD Out in tHe comMeNT,
it'S not actuallY appLy that's beIng fasT; It's the iNTernal C Loop in MeaN
thAt's bEInG fAst. ApPLy iS JuSt rEGulAr old R coDe):
a = mAtrix(As.nuMERIC(1:100000))

UsiNg a Loop: 0.342 SeconDs:
system.time({
    SUm = 0
    fOr (i iN Seq_Along(a)) {
        Sum = SUm + A[[i]]
    }
    suM
})

UsinG sum: unMeasuRaBly small:
sum(A)

It's A litTle disconCerTiNg bEcAuse, aSYmptotIcaLly, The loop Is just aS GooD
aS SUM; tHere's no practical reAsON It Should be Slow; it'S JuSt DOing more
ExTra Work EACh iteRatiON.
SO consideR:
# 0.370 seconDS
sYsTem.time({
    i = 0
    wHile (I < 100000) {
        10
        I = i + 1
    }
})

# 0.743 sEcoNds -- DoublE The tIme jusT adding pArentHEses
system.time({
    i = 0
    While (I < 100000) {
        ((((((((((10))))))))))
        I = I + 1
    }
})

(That eXAmPLE wAS disCovEred by RadfoRd NeAL)
BecAuse ( IN R Is aN OperaTor, anD aCTuALly requires a name looKuP every Time yOu use it:
> `(` = functiOn(x) 2
> (3)
[1] 2

Or, in genERAL, interprEted OPeRAtions (in any lanGuage) Have more stEPs. Of courSe, thoSe steps pRovide benEFIts as welL: yoU coUldN't dO THaT ( trick in C.

A:

The ONLy AnSwEr to the queStion poSed Is; lOopS arE nOt slow if wHat you neEd To Do Is IteRate oVEr a set of
ops in R are slow for thesame reaso n any  in ter pr eted  lan guage is slow: ever y
operation carries ar oundal ot o f  e xtrabaggage . 
L o o k a tR_ exe cC l os ure i n e val.c ( this is th e f un ction called to  call a
us er- defined func tio n). It 's  ne a rly 1 00lines  longa nd per forms all  s o rts of operati o n s-- c reating an enviro n me n t for executio n, ass ig n in g arg ume nts into
t he  envi r onment, et c . Thi n k how much le ss happensw hen  you c al l a functi on in  C (pu sh args onto
s tack, jum p, pop args).S o thatis why  yo u g et t i mi ng s l ik e  th e se  (a s  jo ran poin te dout i n th e c o mmen t,it's  notactually appl y t hat' s  be ing f ast;it's  t he in ternal  C lo op  in mean
that's  bei ng fast.app ly  is  j ust r e gularold  Rcode):A = mat r ix( as . n u me ric(1:100000))

Us in g aloop: 0. 342 se c on ds : 
system. ti me( {
 Sum =  0
 for (i i n seq_ a lo ng (A)) {      Su m =  Su m + A [ [i]] 
    } 
    Sum 
})
U sing sum: unme a surably small : 
s u m (A ) 

It 'sa little di scon c erti ng b e ca use ,  asym ptoti ca l ly ,  the loop is just a sgood
a s sum ; there's nopracticalr e a son it s houl d  b e  slow; it's ju st do ing more
e x tra work  each  iterati on.
So co n s ider:
#0.3 70sec ond s sy stem.time({
 I =  0 
    wh ile  (I < 1 000 00)  {      10
       I =  I  +  1 
  }
})# 0.743se con ds  --  doub l e thetimejust  a dd i ngparenth e se s syst em .t ime( {
  I =0
  wh ile (I< 100000)  {     ( ((((((( ((10))))))))) )        I=I +  1
  } 
})

(Th at example was discover e d by Ra dfo rd Ne al)Because (  in  R isano perato r, and  actu al lyr e quire s anam elookup eve r y  ti me yo uuseit:
> ` (` = function(x) 2 > ( 3)
[1] 2

Or,  in  gen e r al , i n te r pre te d  op e r ations (in anylanguage)ha v emore steps .  Of  c ourse,those s tepsp rovidebenefitsas well:yo u co u l dn' t do that( trickin C.

A: 
Theo nl y Ans wer  to th eQue stion  posed is;  loop s areno t slow  if w ha t you ne ed to do is iterate ove r a se t of
ops in_R are_slow for the same_reason any_interpreted_language is_slow:_every
operation carries around_a lot of_extra baggage.
Look at R_execClosure_in eval.c (this_is_the function called to call a
user-defined function). It's nearly 100 lines long and performs_all_sorts of
operations_--_creating_an environment for execution, assigning_arguments into
the environment, etc.
Think how_much less_happens when you call a function in C_(push_args on to
stack,_jump, pop args).
So that is why you get timings_like these (as joran pointed out_in the comment,
it's_not_actually_apply that's being fast;_it's the internal C loop in_mean
that's being fast. apply is just_regular old R code):
A = matrix(as.numeric(1:100000))

Using a_loop: 0.342 seconds:
system.time({
   _Sum = 0
  _ for_(i in seq_along(A)) {
 _  _ _ Sum =_Sum + A[[i]]
   _}
  _ Sum
})

Using sum: unmeasurably small:
sum(A)

It's a_little_disconcerting because, asymptotically,_the_loop_is just_as good
as sum;_there's_no practical_reason_it should be slow; it's just_doing_more
extra work each iteration.
So consider:
# 0.370 seconds
system.time({
_   I_=_0
   _while (I < 100000) {
_     _ 10
_ _     I = I + 1
 _  }
})

# 0.743 seconds --_double the time just_adding parentheses
system.time({
__  I_=_0
 _  while (I < 100000) {
_ _     ((((((((((10))))))))))
_   __  I = I +_1
    }
})

(That example_was discovered by Radford Neal)
Because_(_in_R is an operator, and_actually requires a name lookup every_time you use_it:
> `(` = function(x) 2
> (3)
[1] 2

Or,_in_general, interpreted operations (in any language)_have_more steps. Of course, those steps_provide_benefits_as well: you couldn't do_that ( trick in C.

A:

The only_Answer to the Question posed is; loops are not_slow if what_you need to do is_iterate_over_a set of