如何在C ++.Net中获取CPU时间和当前时间? [英] How to get the CPU time and current time in C++.Net?

查看:146
本文介绍了如何在C ++.Net中获取CPU时间和当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有问题要问.

如何以微秒为单位获取应用程序中的当前时间和cpu时间?

我发现可以运行一个简单的代码,但是在cpu时间内无法成功.

 clock_t开始,结束;
 已超过 double ;
 
开始= clock();
... /*  完成工作. */
结束=时钟();
elapsed =((( double )(结束-开始))/CLOCKS_PER_SEC; 



那么,如何获取当前时间和cpu时间呢?
最好的问候...

解决方案

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

顺便说一下...用CLOCKS_PER_SEC除以秒数是因为用它除以CPU每秒钟执行的时钟滴答数.

祝您好运!


也许您可​​以尝试使用time()函数获取系统时间.
一个简单的示例如下所示:

 #include   ><   iostream  > 
 #include   <   ctime  > 
 使用 命名空间 std;

 int  main()
{
    time_t tmpTime;
    时间(& tmpTime);
    cout<< " <<< ctime(& tmpTime)<<< ; endl;
    返回  0 ;
} 



祝您好运!


如果您要为C ++/CLI应用程序混合使用托管代码和非托管代码,请使用它应该是的方式... 发布).


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.

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...

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!


Hi,maybe you can try function time() to get the system time.
A simple example is shown as follows:

#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!


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).


这篇关于如何在C ++.Net中获取CPU时间和当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆