How can i see time consumption?

i have two reverse function below, i think they don't consume equal time. although they take minute time, can i see how much time each of the takes?
or how much time any part of any C++ program takes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
//using namespace std;

std::string rev1 (std::string s1) {
	std::string s2;
	unsigned long long int lli = s1.length();
	s2.reserve(lli);
	while(lli>0)
		{ s2 += s1[lli-1]; lli--; }	
	return s2;
}

std::string rev2 (std::string s) {
	char temp;
	unsigned long long int i=0, lli = s.length()-1;
	while(i<lli)
		{ 
			temp = s[i]; 
			s[i] = s[lli]; 
			s[lli] =temp; 
			i++;
			lli--;
		}	
	return s;
}
 
int main() {	

std::string s= "This string will be reversed.";
std::cout << rev1(s) << "\n";
std::cout << rev2(s) << "\n";

}
1
2
3
4
5
6
7
8
9
10
#include <ctime>
...

	clock_t start, stop;
	double totalTime;

	start = clock();
	// Do stuff here
	stop = clock();
	totalTime = (stop - start) / (double)CLOCKS_PER_SEC;

Last edited on
Thank you. may you live long :)
Topic archived. No new replies allowed.