找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 896|回复: 7

为什么我得到的时间差是个0?

[复制链接]
发表于 2009-11-2 05:03:57 | 显示全部楼层 |阅读模式 IP:江苏扬州
#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();
}
发表于 2009-11-2 05:04:00 | 显示全部楼层 IP:江苏扬州
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() )
;
}
回复

使用道具 举报

发表于 2009-11-2 05:04:03 | 显示全部楼层 IP:江苏扬州
你用的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();
}
回复

使用道具 举报

发表于 2009-11-2 05:04:06 | 显示全部楼层 IP:江苏扬州
真的是呀,谢谢
回复

使用道具 举报

发表于 2009-11-2 05:04:09 | 显示全部楼层 IP:江苏扬州
以下是引用柳儿在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语句是判断什么,有什么用?
回复

使用道具 举报

发表于 2009-11-2 05:04:15 | 显示全部楼层 IP:江苏扬州
以下是引用xby20022002在2006-3-28 15:18:00的发言:


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

使用道具 举报

发表于 2009-11-2 05:04:22 | 显示全部楼层 IP:江苏扬州
一个时钟滴答数为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秒).
回复

使用道具 举报

发表于 2009-11-2 05:04:29 | 显示全部楼层 IP:江苏扬州
hao
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 03:30 , Processed in 0.122272 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表