system time in millisecond c++

May 17, 2011 at 3:16pm
how can I get system time in milliseconds? thankss..
May 17, 2011 at 3:16pm
On Windows, you can use GetTickCount() from Windows.h.

http://msdn.microsoft.com/en-us/library/ms724408%28v=vs.85%29.aspx
May 17, 2011 at 3:21pm
No, it must run under linux compiler too...
Last edited on May 17, 2011 at 3:24pm
May 17, 2011 at 3:40pm
closed account (4GNRko23)
I would think about this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <ctime>
//get the system time (unix)
#include <iostream>
using namespace std;

int main()
{
const long double sysTime = time(0);
const long double sysTimeMS = sysTime*1000;

cout << "System Time in milliseconds is " << sysTimeMS << "." << endl;
}


I haven't tested this, so try it yourself.


~nickvth2009
May 17, 2011 at 5:42pm
@nickvth2009 buddy

it prints 1.30565e+012

and what is that? :\
Last edited on May 17, 2011 at 5:43pm
May 17, 2011 at 6:03pm
Actually I want to explain what I want to do exactly

I have a queue with structs. This struct includes the exactly time of pushing itself into queue

I have something like this to see system time:
1
2
3
4
5
6
7
8
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );

long callTime=timeinfo->tm_hour*3600+timeinfo->tm_min*60+timeinfo->tm_sec;
q.push( Call( callNum, callTime, callLength ));


The problem is when I pop next struct from the queue, I wanna calculate how much long struct waited in queue in milliseconds.

I hope someone can get what I wanna say.. :\
May 17, 2011 at 6:08pm
http://www.cplusplus.com/reference/clibrary/ctime/clock/
That looks like what you want, doesn't it?
May 17, 2011 at 11:30pm
no it doesnt... :\

I need something that give me current time hour:minute:second:millisecond :\
Last edited on May 17, 2011 at 11:31pm
May 17, 2011 at 11:52pm
I need something that give me current time hour:minute:second:millisecond :\


No you don't. You told us what you need. You need a way of knowing how many milliseconds have passed between two events. You have no need for the current time.

If you want something that is platform independent and uses only standard libraries, the best you're going to do is with ctime, which will only give you the number of whole seconds elapsed (which nickvth gave you above - that number, which you could have discovered for yourself by reading the documentation http://www.cplusplus.com/reference/clibrary/ctime/time/ is the number of seconds that have passed since January 1, 1970.

If you must have milliseconds, you're going to have to use platform dependent code. If you use a library that hides it from you (such as Boost.DateTime) you won't have to deal with that platform dependent code yourself.
Last edited on May 17, 2011 at 11:52pm
May 18, 2011 at 5:25am
May 20, 2011 at 9:10am
I had the same problem and found a neat piece of code at http://www.firstobject.com/getmillicount-milliseconds-portable-c++.htm...

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
#include <iostream>
using namespace std;
#include cstdlib
#include <sys/timeb.h>

int getMilliCount(){
	timeb tb;
	ftime(&tb);
	int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
	return nCount;
}

int getMilliSpan(int nTimeStart){
	int nSpan = getMilliCount() - nTimeStart;
	if(nSpan < 0)
		nSpan += 0x100000 * 1000;
	return nSpan;
}

int main(){
	printf("\n\n");
	printf("****************************************\n");
	printf("** Millisecond Timer\n");
	printf("****************************************");
	

	printf("\n\nStarting timer...");
	int start = getMilliCount();

	// CODE YOU WANT TO TIME
	for(int i = 0; i < 1000000; i++){
		int a = 55/16
	}

	int milliSecondsElapsed = getMilliSpan(start);

	printf("\n\nElapsed time = %u milliseconds", milliSecondsElapsed);

	printf("\n\n");
	return 1;
}
May 20, 2011 at 3:10pm
"I wanna calculate how much long struct waited in queue in milliseconds"
I honestly don't know what's wrong with clock(), it does exactly what you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
  unsigned long long Int64 = 0;
  clock_t Start = clock();
  for(unsigned short i = 0; i < 1024; ++i)
  {
    for(unsigned short j = i; j > 0; --j)
    {
      Int64 += j + i;
    }
  }
  cout << "Time Difference: " << clock() - Start << endl;
}
Last edited on May 20, 2011 at 3:11pm
Topic archived. No new replies allowed.