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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.