1#include <iostream>
2#include <chrono>
3
4// This is how to measure the time it takes functions to finish
5
6long add(int a, int b) {
7 return a + b;
8}
9
10int main() {
11 auto start = std::chrono::steady_clock::now();
12 std::cout << "9 + 10 = " << add(9, 10) << '\n';
13 auto end = std::chrono::steady_clock::now();
14 std::chrono::duration<double> elapsed_seconds = end - start;
15 std::cout << "elapsed time to compute 9 + 10: " << elapsed_seconds.count() << "s\n";
16 return 0;
17}
1// CPP program to create a timer
2#include <iomanip>
3#include <iostream>
4#include <stdlib.h>
5#include <unistd.h>
6using namespace std;
7
8// hours, minutes, seconds of timer
9int hours = 0;
10int minutes = 0;
11int seconds = 0;
12
13// function to display the timer
14void displayClock()
15{
16 // system call to clear the screen
17 system("clear");
18
19 cout << setfill(' ') << setw(55) << " TIMER \n";
20 cout << setfill(' ') << setw(55) << " --------------------------\n";
21 cout << setfill(' ') << setw(29);
22 cout << "| " << setfill('0') << setw(2) << hours << " hrs | ";
23 cout << setfill('0') << setw(2) << minutes << " min | ";
24 cout << setfill('0') << setw(2) << seconds << " sec |" << endl;
25 cout << setfill(' ') << setw(55) << " --------------------------\n";
26}
27
28void timer()
29{
30 // infinte loop because timer will keep
31 // counting. To kill the process press
32 // Ctrl+D. If it does not work ask
33 // ubuntu for other ways.
34 while (true) {
35
36 // display the timer
37 displayClock();
38
39 // sleep system call to sleep
40 // for 1 second
41 sleep(1);
42
43 // increment seconds
44 seconds++;
45
46 // if seconds reaches 60
47 if (seconds == 60) {
48
49 // increment minutes
50 minutes++;
51
52 // if minutes reaches 60
53 if (minutes == 60) {
54
55 // increment hours
56 hours++;
57 minutes = 0;
58 }
59 seconds = 0;
60 }
61 }
62}
63
64// Driver Code
65int main()
66{
67 // start timer from 00:00:00
68 timer();
69 return 0;
70}
71