请问C语言中clock()函数该怎么用?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问C语言中clock()函数该怎么用?相关的知识,希望对你有一定的参考价值。

前几天写了一个用clock做种子的随机数程序

#include<stdio.h>
#include<time.h>

int random(int a)

int b;
srand((unsigned)clock());
b=rand()%a;
return b;


int main (void)

printf("%d",random(10000));
return 0;


测试后发现打印值均为38,后来调了一遍发现clock的返回值都是0

根据网上的资料,clock应该返回从开机到现在经过的时钟周期数,但clock的返回值一直是0

请问这个问题该怎么解决?
我要用clock做种子,别说time,time的精度达不到,也别说gettickcount之类的windows API,我要移植到Linux上的,所以必须用C语言函数

我也觉的很奇怪啊,之前用这个东西是没问题的,dev-cpp 标准的gcc,甚至包括VS2008我都用了,换了三个编译器,莫非要我重装系统?
******************************************************

以下更新于7.30 11:59
______________________________________________________________________________________________
我现在对clock已经晕了,各有各的说法,有没有一个至少能返回毫秒级的函数
另外我希望函数是C语言自带的标准库函数,不要只能在linux下跑,因为最后这个东西要跨平台的
gettimeofday() 还有clock_gettime() 这两个微秒纳秒级的函数是标准的c语言函数还是linux下专有的?

clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。

它的具体功能是返回处理器调用某个进程或函数所花费的时间。函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,其中clock_t是用来保存时间的数据类型。

在time.h文件中,我们可以找到对它的定义:

#ifndef _CLOCK_T_DEFINED  

typedef long clock_t;  

#define _CLOCK_T_DEFINED  

#endif  

clock_t其实就是long,即长整形。该函数返回值是硬件滴答数,要换算成秒或者毫秒,需要除以CLK_TCK或者 CLK_TCK CLOCKS_PER_SEC。比如,在VC++6.0下,这两个量的值都是1000,这表示硬件滴答1000下是1秒,因此要计算一个进程的时间,用clock()除以1000即可。

clock的返回值一直是0的原因:

1、编译器优化,for循环实际根本没执行,直接跳过去了,所以时间为0。

2、clock计算的是程序占用cpu的时间,如果你的程序执行的动作很少,那么clock算出的时间也很少。

3、建议使用time gettimeofday函数来计时。

扩展资料:

C语言中clock()函数的程序例1:(TC下运行通过)

#include <stdio.h>

#include <time.h>

int main(void)

clock_t start, end;

start = clock();

delay(2000);

end = clock();

printf("The time was: %f\\n", (double)(end - start) / CLK_TCK);

return 0;

说明:CLK_TCK 定义在TC中的time.h中:#define CLK_TCK18.2。

在VC6.0中也有关于CLK_TCK的宏定义,不过其值不再是18.2,而是1000。

实际上在VC6.0中CLK_TCK已完全等同CLOCKS_PER_SEC。

参考资料来源:百度百科-clock()

参考技术A C语言函数clock()  功 能:
返回处理器调用某个进程或函数所花费的时间。

  用 法: clock_t clock(void);

  说明:clock_t其实就是long,即长整形。该函数返回值是硬件滴答数,要换算成秒或者毫秒,需要除以CLK_TCK或者 CLK_TCK
CLOCKS_PER_SEC。比如,在VC++6.0下,这两个量的值都是1000,这表示硬件滴答1000下是1秒,因此要计算一个进程的时间,用clock()除以1000即可。具体见第一个例子。

  程序例1:(TC下运行通过)

  #include <stdio.h>

  #include <time.h>

  int main(void)

  

  clock_t start, end;

  start = clock();

  delay(2000);

  end = clock();

  printf("The time was: %f\\n", (double)(end - start) / CLK_TCK);

  return 0;

  

  CLK_TCK 定义在TC中的time.h中:#define CLK_TCK
18.2。在VC6.0中也有关于CLK_TCK的宏定义,不过其值不再是18.2,而是1000。实际上在VC6.0中CLK_TCK已完全等同于CLOCKS_PER_SEC。

  在VC中delay用Sleep()来代替,其头文件是windows.h。

  程序例2:(VC6.0下运行通过)

  #include <stdio.h>

  #include <time.h>

  #include<windows.h>

  int main(void)

  

  clock_t start, end;

  start = clock();

  Sleep(2000);

  end = clock();

  printf("The time was: %d\\n", (end - start) /
CLK_TCK);//注意是%d,不再是%f

  return 0;

  

  程序例3:(VC6.0下运行通过)

  #include "stdio.h"

  #include "stdlib.h"

  #include "time.h"

  int main( void )

  

  long i = 10000000L;

  clock_t start, finish;

  double duration;

  

  printf( "Time to do %ld empty loops is ", i );

  start = clock();

  while( i-- ) ;

  finish = clock();

  duration = (double)(finish - start) / CLK_TCK;
//CLK_TCK在VC6.0中可以用CLOCKS_PER_SEC

  printf( "%f seconds\\n", duration );

  system("pause");

  return 0;

  

参考技术B The clock() function returns an approximation of processor time used by the program.
以上内容摘自 man 3 clock
返回接近程序使用的CPU时间 你拿这个做种子?

如果linux下 建议
GRand* g_rand_new (void);

Creates a new random number generator initialized with a seed taken either from /dev/urandom (if existing) or from the current time (as a fallback).
使用这个函数 直接使用操作系统提供的/dev/urandom 这个设备是根据环境噪声产生的真(记得好像是)随机数
如果没有urandom就使用当前时间 具体这个时间精确到多少不知道

如果不用glib库
就直接去读/dev/urandom设备 这个做种子是最好了的

linux里面一般都用这个/dev/random 和/dev/urandom得到真随机数 这个这个设备的种子是 通过中断事件得到的 网卡 键盘 等设备的中断是异步 产生的随机数应该可以算作真随机数了

不过这个只适应linux而且 程序环境必须是在有交换性 也就是外界必须提供中断事件 而且如果熵池里面的随机数用完了 读取这个设备会造成程序阻塞 直到熵池里有新的随机数

如果你的程序适应这种条件
可以使用glib 有交换环境 linux (一般不是特殊环境下都满足)
建议直接使用这个方便的函数了

glib就是为了方便程序员 跨平台跑程序的 在window下可以用

clock()函数没什么好说的我直接把man里面note节翻译给你看下
Note that the time can wrap around. On a 32-bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes.
每72分钟会产生相同的值 (所以肯定是伪随机数,不过可以达到一般程序要求了)

On several other implementations, the value returned by clock() also includes the times of any children whose status has been collected via wait(2) (or another wait-type call). Linux does not include the times of waited-for children in the value returned by clock(). The times(2) function, which explicitly returns (separate) information about the caller and its children, may be preferable.

The C standard allows for arbitrary values at the start of the program; subtract the value returned from a call to clock() at the start of the program to get maximum portability.

C标准允许在程序开始设置一个任意值减去"clock()"的返回值 作为结果
以方便移植

The value returned is the CPU time used so far as a clock_t; to get the
number of seconds used, divide by CLOCKS_PER_SEC. If the processor
time used is not available or its value cannot be represented, the
function returns the value (clock_t) -1.
函数的返回值 使用的秒数除以CLOCKS_PER_SEC (32位系统这个数是1000000)

如果time不可用 返回(clock_t) -1

linux里面不会收集子进程的CPU使用时间
但某些其他实现收集子进程CPU使用时间
参考技术C clock 计算的是从程序运行到执行clock时的时间间隔。

重复启动程序,时间还是 从程序运行到执行clock时的时间间隔。
这个数字不变,种子不变。你的随机就不随机了。

所以要用:
srand(time(NULL));
time(NULL) 是永远在变的。种子变,随机了。
参考技术D 网上资料错了,clock返回当前进程创建到现在经过的时钟周期数

你的程序那么短,返回0是正常的

我在自己的电脑上试了试,在调用clock()前用了Sleep(1000),结果就不一样了

Sleep()是 Windows API,不要在你的程序里用

而且觉得,随机数钟子只要设一次就够了,不应该每次设,
你这样,在一段时间内(大约1毫秒)都会得到同样的随机数

clock返回的大约是毫秒级(不是处理器周期,你可以查CLOCKS_PER_SEC)本回答被提问者采纳

表用英语怎么说

如果是手表的话就是watch 钟表的话可以是 clock。如果赞同的话 请采纳我的答案哈 谢谢 参考技术A 请问您说的是表格的表:table, graph, diagram, chart
还是钟表的表:clock, alarm, watch, timer?
ps:图里没有表啊……追问

clock

追答

clock, alarm, watch, timer都可以~sundial也算吧~日冕也可以当表…望采纳哈~

追问

请问你叫谁。你很像我们班的一个同学

本回答被提问者采纳
参考技术B 要看问的是什么表。实体表的话手表watch 钟表 clock,表格的话table;list;chart;form追问

谢谢了

参考技术C 你指手表?
那是 watch
如果是钟表或者闹钟
那是clock
参考技术D watch 表/手表追问

watch是坐式表。clock是手表

追答

clock是钟表 一般手表都说watch的

追问

追答

啊- -。

以上是关于请问C语言中clock()函数该怎么用?的主要内容,如果未能解决你的问题,请参考以下文章

请问在C语言里怎么获取当前时间和日期(精确到毫秒)?

C语言中怎样测试函数执行时间

C语言 关于时间函数

c语言中怎么设置计时器?

请问这道C语言该怎么做啊?

请问这个C语言中有参数的函数是怎么传递值的,调用的时候没有参数啊。