Learn R Programming

dipsaus (version 0.3.1)

sumsquared: Fast Calculation of Sum-squared for Large Matrices/Vectors

Description

Calculate sum(x^2), but faster when the number of elements exceeds 1000.

Value

A numerical scalar

Arguments

x

double, integer, or logical vector/matrix

Examples

Run this code

x <- rnorm(10000)
sumsquared(x)

# Compare speed
microbenchmark::microbenchmark(
  cpp = {sumsquared(x)},
  r = {sum(x^2)}
)

Run the code above in your browser using DataLab