qq0276029252 回答的不对
题目要求的是微秒 不是毫秒
#include<windowsh>
#include<iostream>
class timer
{
public:
timer();
void start(void);
void end(void);
DWORD getTime(void) const;
private:
LARGE_INTEGER m_i64CPUFreq;
LARGE_INTEGER m_i64Begin;
LARGE_INTEGER m_i64End;
void reset(void);
};
int main()
{
timer t;
tstart();
{
Sleep(1000);
}
tend();
std::cout<<tgetTime();
system("pause");
}
timer::timer()
{
QueryPerformanceFrequency(&m_i64CPUFreq);
}
void timer::start(void)
{
reset();
QueryPerformanceCounter(&m_i64Begin);
}
void timer::end(void)
{
QueryPerformanceCounter(&m_i64End);
}
DWORD timer::getTime(void) const
{
DWORD dwTotalMicrosecond = 0;
LARGE_INTEGER i64TickCount;
i64TickCountQuadPart = m_i64EndQuadPart - m_i64BeginQuadPart;
dwTotalMicrosecond = (DWORD) (i64TickCountQuadPart 1000000 / m_i64CPUFreqQuadPart);
return dwTotalMicrosecond;
}
void timer::reset(void)
{
m_i64Begin = m_i64End;
}
进入发那科数控系统,点击页面上的“参数”按钮。
在参数设置页面中,找到“程序保留时间”选项并点击。
根据实际需要,选择需要保存程序的天数。可以通过左右箭头或手动输入进行调整。一般建议根据企业的管理需求来决定程序保留时间的长短。
1、使用clock函数获得程序开始和结束的时间,相减就能得到程序运行的时间。
clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:
clock_t clock(void) ;
简单而言,就是该程序从启动到函数调用占用CPU的时间。这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型。
例程:
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
clock_t start,finish;
start=clock();
cout << "HW " << endl;
finish=clock();
cout << finish-start << "/" << CLOCKS_PER_SEC << " (s) "<< endl;
return 0;
}
3、转>
第一种方法利用SystemDateTimeNow
[csharp]
view
plaincopy
static
void
SubTest()
{
DateTime
beforDT
=
SystemDateTimeNow;
//耗时巨大的代码
DateTime
afterDT
=
SystemDateTimeNow;
TimeSpan
ts
=
afterDTSubtract(beforDT);
ConsoleWriteLine("DateTime总共花费{0}ms",
tsTotalMilliseconds);
}
第二种用Stopwatch类(SystemDiagnostics)
[csharp]
view
plaincopy
static
void
SubTest()
{
Stopwatch
sw
=
new
Stopwatch();
swStart();
//耗时巨大的代码
swStop();
TimeSpan
ts2
=
swElapsed;
ConsoleWriteLine("Stopwatch总共花费{0}ms",
ts2TotalMilliseconds);
}
第三种用API实现:
[csharp]
view
plaincopy
[SystemRuntimeInteropServicesDllImport("Kernel32dll")]
static
extern
bool
QueryPerformanceCounter(ref
long
count);
[SystemRuntimeInteropServicesDllImport("Kernel32dll")]
static
extern
bool
QueryPerformanceFrequency(ref
long
count);
static
void
SubTest()
{
long
count
=
0;
long
count1
=
0;
long
freq
=
0;
double
result
=
0;
QueryPerformanceFrequency(ref
freq);
QueryPerformanceCounter(ref
count);
//耗时巨大的代码
QueryPerformanceCounter(ref
count1);
count
=
count1
-
count;
result
=
(double)(count)
/
(double)freq;
ConsoleWriteLine("QueryPerformanceCounter耗时:
{0}
秒",
result);
}
#include "timeh"
#include "stdioh"
main()
{
double start, finish;
start = clock();//取开始时间
printf("Hello, World!\n");
finish = clock();//取结束时间
printf( "%f seconds\n",(finish - start) / CLOCKS_PER_SEC);//以秒为单位显示之
}
上面的代码理论上是可以显示printf("Hello, World!\n");语句的运行时间的,但我猜实际的显示结果是0,因为printf("Hello, World!\n");这个语句的运行时间是可以忽略不计的,加一个次数较多的循环才能看到效果
以上就是关于计算程序的时间(以微妙计算的)全部的内容,包括:计算程序的时间(以微妙计算的)、发那科怎么设置程序保存时间、如何统计C++程序运行时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)