Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How should I find the execution time of a program in c++?

+2 votes
asked Mar 13 by Nirali Patel (750 points)

2 Answers

+1 vote
answered Mar 13 by Fatima Suleiman Atta (320 points)
The function clock() returns the number of clock ticks since the program started executing. If you divide it by the constant CLOCKS_PER_SEC you will get how long the program has been running, in seconds.
0 votes
answered Mar 13 by Peter Minarik (79,780 points)
edited Mar 15 by Peter Minarik
C++ way:

#include <chrono>
#include <iostream>
#include <thread>

int main()
    using clock = std::chrono::steady_clock;
    clock::time_point start = clock::now();
    clock::time_point end = clock::now();
    clock::duration execution_time = end - start;
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(execution_time).count() << std::endl;
commented Mar 13 by Nirali Patel (750 points)
I tried using that way as you advised, but I am getting errors. I don't know how to fix. I have never done execution time in my code. can you please help me? Thank you.
Here is the code:
commented Mar 13 by xDELLx (10,500 points)
#include <chrono>

Add this
commented Mar 13 by Nirali Patel (750 points)
okay, I added that library, but still I don't get the execution time of my program
commented Mar 15 by Peter Minarik (79,780 points)
I've updated my original answer to include printing the duration converted to milliseconds. I hope this helps.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.