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

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

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

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


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


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

Relevant Projects

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.