- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate time of operation in Arduino
Often, you need to measure the time your microcontroller takes to perform a particular task. You can use the millis() function of Arduino to measure the time. This function returns the number of milliseconds passed since your board started running the current program. Therefore, to calculate the time taken by an operation, you can call millis() before and after your operation, and take the difference of the two values.
An example implementation is given below −
Example
void setup() { // put your setup code here, to run once: Serial.begin(9600); long int t1 = millis(); task_whose_time_is_to_be_measured(); long int t2 = millis(); Serial.print("Time taken by the task: "); Serial.print(t2-t1); Serial.println(" milliseconds"); } void loop() { // put your main code here, to run repeatedly: }
As you can see, this will give the execution time of the task in milliseconds. But what if you want the execution time to be in microseconds? You guessed it! You can use micros() instead of millis().
- Related Articles
- Arduino Time Library Introduction
- Compute the elapsed time of an operation in Java
- Computer elapsed time of an operation in milliseconds in Java
- Real Time Clock (RTC) with Arduino
- Calculate total time duration (add time) in MySQL?
- Calculate the execution time of a method in C#
- What is the Time Reversal Operation on Signals?
- What is the Time Shifting Operation on Signals?
- How to calculate local time in Node.js?
- How to calculate Execution Time of a Code Snippet in C++?
- How to calculate elapsed/execution time in Java?
- Java Program to Calculate the Execution Time of Methods
- Haskell Program to Calculate the Execution Time of Methods
- Java Program to calculate the time of sorting an array
- How to calculate total time between a list of entries?

Advertisements