| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1189 人关注过本帖
标题:为什么我得到的时间差是个0?
只看楼主 加入收藏
弯弯的小河
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-3-3
收藏
 问题点数:0 回复次数:7 
为什么我得到的时间差是个0?

#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <windows.h>
#include <windowsx.h>
#include <fstream.h>
#include <iostream.h>
int main(void)
{int i,j;
int wrh,v,s=1500;

clock_t start,end;
start=clock();
cout<<start<<"\n";

for(i=0;i<50000;i++)
{j+1;j-1;}
end=clock();
cout<<end<<"\n";
getch();
}

搜索更多相关主题的帖子: include 时间差 windows clock int 
2006-03-28 10:15
柳儿
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:1830
专家分:30
注 册:2004-9-23
收藏
得分:0 
MSDN上的例子,好好看一下。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;

/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}

成功会使人骄傲。如果你骄傲自大,你就会停止学习。不学习,人就停止了进步
2006-03-28 10:28
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

你用的50000值太小了,计算滴答数又不正确.

clock函数告诉调用进程用了多少处理器时间,
秒时间是时钟返回值除以该常量值的近似值
.换句话说,clock返回值为已经消逝的处理器时钟滴答数,
一个时钟滴答数近似等于1/CLOCKSPERSEC
秒.

#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <windows.h>
#include <windowsx.h>
#include <fstream.h>
#include <iostream.h>
int main(void)
{int i,j;
int wrh,v,s=1500;

clock_t start,end;
double duration;
start=clock();
cout<<start<<"\n";

for(i=0;i<50000000;i++)
{j+1;j-1;}
end=clock();
duration=(double)(end-start)/CLOCKS_PER_SEC;
cout<<duration<<"\n";
getch();
}


=×&D o I p R e E n C g T l X&×=
2006-03-28 11:02
弯弯的小河
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-3-3
收藏
得分:0 
真的是呀,谢谢
2006-03-28 11:18
xby20022002
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-3-17
收藏
得分:0 
以下是引用柳儿在2006-3-28 10:28:00的发言:
MSDN上的例子,好好看一下。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;

/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}

请教一下,最后的那个sleep函数有什么用?函数中的while语句是判断什么,有什么用?


2006-03-28 15:18
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
以下是引用xby20022002在2006-3-28 15:18:00的发言:

请教一下,最后的那个sleep函数有什么用?函数中的while语句是判断什么,有什么用?

[此贴子已经被作者于2006-3-28 15:54:21编辑过]


=×&D o I p R e E n C g T l X&×=
2006-03-28 15:51
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
一个时钟滴答数为1/CLOCK_PER_SEC,
CLOCK_PER_SEC个滴答数(CLOCK_PER_SEC*1/CLOCK_PER_SEC)为一秒,
3*CLOCK_PER_SEC为参数意思就是3秒.
goal=wait+clock();意思就是当前的时钟滴答数加上3秒,
while(goal>clock());不断增加处理器时钟滴答数,当时钟滴答数goal==clock()时
完成(也就经过3秒).

=×&D o I p R e E n C g T l X&×=
2006-03-28 15:55
zhubenben
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-3-28
收藏
得分:0 
hao
2006-03-28 19:47
快速回复:为什么我得到的时间差是个0?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016193 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved