How to calculate the time taken by each step in a for loop in R

This recipe helps you calculate the time taken by each step in a for loop in R

Recipe Objective

Time elapsed to execute a function is a crucial element in a data scientist's job specifically when you have to deal with large datasets. Since there are different approaches to do the same task, time required to these tasks plays an important role when we deal with large dataset. This recipe demonstrates us to calculate the time taken by each step in a for loop.

We will use Sys.time() function to carry out this task

Learn About the Application of ARCH and GARCH models in Real-World

Example:

We will calculate the squares of numbers from 1 to 4 using for loop and calculate the time required by each step

for (i in 1:4){ # start time stamp start = Sys.time() print(i^2) # end time stamp end =Sys.time() #Time required for each step elapsed_time = end - start print(elapsed_time) }

[1] 1
Time difference of 0 secs
[1] 4
Time difference of 0 secs
[1] 9
Time difference of 0 secs
[1] 16
Time difference of 0 secs

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.