Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone.

I have question to ask.

How can I get the current time and the cpu time in an application in microsecs?

I found a simple code to run but could'nt succed for cpu time.

C++
clock_t start, end;
 double elapsed;
 
start = clock();
... /* Do the work. */
end = clock();
elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;


So, how to get the current time and cpu time?
My best regards...
Posted
Updated 22-Sep-11 23:37pm
v3
Comments
Sergey Chepurin 23-Sep-11 5:41am    
Just corrected the question to make it easier for the others to search for the answer on CPU time.

How about DateTime.Now?
http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx[^]

By the way... the division by CLOCKS_PER_SEC is giving you the number of seconds because is divides it by the number of clock ticks your cpu does every second.

Good luck!
 
Share this answer
 
v2
Hi,maybe you can try function time() to get the system time.
A simple example is shown as follows:
C++
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    time_t tmpTime;
    time(&tmpTime);
    cout<<"The time is "<<ctime(&tmpTime)<<endl;
    return 0;
}


Good luck!
 
Share this answer
 
v2
If you will manage to mix managed and unmanaged code for your C++/CLI application, then this Get CPU Usage with GetSystemTimes seems to do what you need. Note that you have to compile the code provided in "Comments and Discussions" section (see The way it's supposed to be... post).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900