a = 1:10

c = 0

for (e in a) c = c + e^2

c

sum(a ^ 2)

a = 1:1000000

c = 0

system.time(for (e in a) c = c + e ^ 2)

system.time(sum(a ^ 2))

a = seq(0, 1, length.out = 10)

b = 0

c = 0

for (e in a) {

b = b + exp(e)

}

b

c = sum(sapply(a, exp))

c

sum(sapply(a, function(x) { return(exp(x ^ 2)) }))

sum(sapply(a, function(x) exp(x ^ 2)))