POP77001 Computer Programming for Social Scientists
system.time() function to analyse function performance.microbenchmark package and identically named function to time function calls.microbenchmark, otherwise times of individual runs are returns.apply() function.colMeans() function.system.time() benchmark and microbenchmark package.set.seed(1234)
# Here we create a data frame of 1000 observations of 50 variables
# where each variable is a random draw from a normal distribution with mean
# drawn from a uniform distribution between 0 and 10 and standard deviation 1
df <- data.frame(mapply(
function(x) cbind(rnorm(n = 1000, mean = x, sd = 1)),
runif(n = 50, min = 0, max = 10)
))time module.timeit module provides a better alternative as it does it automatically an more.microbenchmark in R in that it averages over many runs.%timeit.Kernel, Change kernel and pick Python from the drop-down menu.pandas DataFrame.mean() from statistics module).# Setting seed using 'numpy' is slightly more involved than with 'random' module (or R)
# We first need to create a random number generator object, that we can than use
# to generate random draws from distributions that are consistent across re-runs
rng = np.random.default_rng(1234)
# Here we are, essentially, replicating the process of data frame creation as in R above
# each variable is a random draw from a normal distribution with mean
# drawn from a uniform distribution between 0 and 10 and standard deviation 1
df2 = pd.DataFrame(np.concatenate([
rng.normal(loc = x, scale = 1, size = (1000, 1))
for x
in rng.uniform(low = 0, high = 10, size = 50)
], axis = 1))