C++ Counting

Hi There Not sure if anyone has time to help me but I Am looking for a tutorial to wright a program that counts the time between a switch been pressed and then pressed for a second time with a .txt output any help would be much appreciated

Thanks for this it was intresting to read but i am rely new to this and was looking for more of a guid through building the program thanks for your time tho
@miinipaa, in, first link example what is "volatile double" the example had problem in Visual studio2012,
second link example doesn't run even in cpp.sh !
what is "volatile double"
Something that is used here to prevent optimisation and le time actually pass. It has no relation to time() and difftime() functions. Basically all you need to know to use it was written before.

Second example uses std::put_time which still not supported by many compilers. As before, you do not need it as it has no relation to actual time conputing.
they show time in integer, such as if consumedTime is 0.5 , it will show 0.
the following example simply shows 0.000 precision:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//compare which function is faster.
#include <ctime>
#include <iostream>

using std::string;
using std::cout;
using std::endl;
//reverse string function
inline string rev1 (string s1) {
	string s2;
	unsigned long long int lli = s1.length();
	s2.reserve(lli);
	while(lli>0)
		{ s2 += s1[lli-1]; lli--; }	
	return s2; 
}
//another reverse string function
inline string rev2 (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() {
string s= "This string will be reversed, many times.";
	clock_t t1, t2, t3; //calculate time. ::::::::::::::::::::
	double time1, time2;
	t1 = clock();
	for(int i=0; i<1e6; i++) //call 1 million times
		rev1(s);

	t2 = clock();
	for(int i=0; i<1e6; i++)
		rev2(s);

	t3 = clock();
	time1 = (t2 - t1)/ (double)CLOCKS_PER_SEC;	//0.000 precision 
	time2 = (t3 - t2)/ (double)CLOCKS_PER_SEC;	
	cout << "\ntime1 = " << time1 << endl; // fractional output
	cout << "\ntime2 = " << time2 << endl;		

}

time1 = 0

time2 = 0
This shows why volatile is used. Compiler optimised away those operations completely.

the time between a switch been pressed and then pressed for a second time
Please elaborate. What is a "switch" and how do you "press" it?
Last edited on
could you please explain the difference between
"type identifier" and "volatile type identifier"
type identifier is another way to say type name volatile is a type qualifiers or type modifier (There are two of them, second one is const).
So volatile type identifier would be name of the type with volatile qualifier applied.

Note that identifier can refer to many things. Most often it is used to denote variable name. Do not get confused.
Topic archived. No new replies allowed.