时间输出,以毫秒为单位 [英] time output in milliseconds

查看:126
本文介绍了时间输出,以毫秒为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个随机数生成器程序并使用time()来播种它。

我发现time()输出是以秒为单位。你知道另一个功能

输出数千秒吗? Thanx。

解决方案



" KevinL" <柯******* @ ureach.com>在消息中写道

news:83 ************************** @ posting.google.c om ...

我正在编写一个随机数生成器程序并使用time()来播种它。
我发现time()输出是以秒为单位的。


也许是你的实施。但对于所有人来说,它并不一定是




------------------ -------------------------------------------------- ----

ISO / IEC 9899:1999(E)


7.23.2.4时间函数


概要


1 #include< time.h>

time_t time(time_t * timer);


描述


2时间函数确定当前日历时间。该值的

编码未指定。


返回


3时间函数返回实现''最佳近似值

到当前日历时间。如果日历时间不可用,则返回值(time_t)( - 1)

。如果timer不是null

指针,则返回值也会分配给它b / b
指向的对象。

----- -------------------------------------------------- -----------------

你知道另一个功能
输出数千秒吗? Thanx。




标准C库没有提供。您的实施可能是
或可能不是。检查你的文档。


你可能还想调查''clock()''函数和

''CLOCKS_PER_SEC''宏(也声明了)通过< time.h>),这将是
通常比''time()'更加细化。


正在使用''时间()你的RNG种子只有一秒的分辨率真的不足

吗?


-Mike

" Mike Wahler" < MK ****** @ mkwahler.net>写道:

您可能还想调查''clock()''函数和
''CLOCKS_PER_SEC''宏(也由< time.h>声明) ;),这通常会让你比''time()'更精细。




请注意,使用clock()播种RNG除非

你让用户在此之前刻录一些时钟滴答,否则不是一个好主意。由于clock()'s
返回值仅取决于所使用的处理器时间,如果你总是在询问之前执行相同的代码,你的种子将是可预测的。 />
时钟()为种子。


Richard


2004年6月30日星期三,KevinL写道:

我正在编写一个随机数生成器程序并使用time()来播种它。我发现time()输出以秒为单位。你知道
另一个在几千秒内输出的功能吗? Thanx。




首先,你发现你的C标准时间()

函数的实现以秒为单位输出。所有

实现都不能保证这是真的。


其次,没有功能可以保证输出数千秒。

有一个clock()函数。它将返回1 / CLOCKS_PER_SEC。

此外,此函数将返回实现的最佳

近似于使用的处理器时间。如果您的机器上的时钟仅为
,那么在一定程度上你可能会发现这个时间已经消失了几美元。


第三,clock()函数是自预定义的

时间段以来''ticks''的数量。那个时间段可以是一个固定的日期(就像时间()

那样)或者它可能是程序执行的开始。如果没有用户输入
并且没有外部事件可以改变应用程序的运行时间,那么clock()函数可以返回相同的值。更糟糕的是,如果有
不支持clock(),它将始终返回(clock_t)-1。


最后,你从未说明为什么需要粒度更好的东西比time()函数更优惠
。我不能给你一个保证的功能

在几千秒内返回一个值而不知道为什么你需要

我不能建议另一种选择。


-

发送电子邮件至:darrell at cs dot toronto dot edu

不要发送电子邮件至 vi ************ @ whitehouse.gov


I''m writing a random number generator program and using time() to seed it.
I found out that time() output is in seconds. Do you know of another function
that outputs in thousands of seconds ? Thanx.

解决方案


"KevinL" <Ke*******@ureach.com> wrote in message
news:83**************************@posting.google.c om...

I''m writing a random number generator program and using time() to seed it.
I found out that time() output is in seconds.
Perhaps it is for your implementation. But it''s not necessarily
the case for all.

------------------------------------------------------------------------
ISO/IEC 9899:1999 (E)

7.23.2.4 The time function

Synopsis

1 #include <time.h>
time_t time(time_t *timer);

Description

2 The time function determines the current calendar time. The
encoding of the value is unspecified.

Returns

3 The time function returns the implementation''s best approximation
to the current calendar time. The value (time_t)(-1) is returned
if the calendar time is not available. If timer is not a null
pointer, the return value is also assigned to the object it
points to.
------------------------------------------------------------------------
Do you know of another function
that outputs in thousands of seconds ? Thanx.



The standard C library does not provide one. Your implementation may
or may not. Check your documentation.

You might also want to investigate the ''clock()'' function and the
''CLOCKS_PER_SEC'' macro (also declared by <time.h>), which will
typically get you more granularity than ''time()''.

Is using ''time()'' with a one-second resolution really insufficient
for your RNG seeding?

-Mike


"Mike Wahler" <mk******@mkwahler.net> wrote:

You might also want to investigate the ''clock()'' function and the
''CLOCKS_PER_SEC'' macro (also declared by <time.h>), which will
typically get you more granularity than ''time()''.



Mind you, using clock() for seeding the RNG is not a good idea unless
you let the user burn some clock ticks before that. Since clock()''s
return value depends only on the processor time used, your seed will be
quite predictable if you always execute the same code before asking
clock() for a seed.

Richard


On Wed, 30 Jun 2004, KevinL wrote:

I''m writing a random number generator program and using time() to seed
it. I found out that time() output is in seconds. Do you know of
another function that outputs in thousands of seconds ? Thanx.



First, you found out that your implementation of the C standard time()
function outputs in seconds. There is no guarantee this is true for all
implementations.

Second, there is no function guaranteed to output thousands of seconds.
There is a clock() function. It will return 1/CLOCKS_PER_SEC.
Additionally, this function will return the implementation''s best
approximation to the processor time used. If the clock on your machine is
only accurate to a certain degree you might find this is off by a few
milliseconds.

Third, the clock() function is the number of ''ticks'' since a predefined
time period. That time period could be a fixed date (just like time()
does) or it could be the start of program execution. If you have no user
input and no external events that change the run time of your application,
the clock() function could return the same value. Even worse, if there is
no support for clock() it will always return (clock_t)-1.

Finally, you never stated WHY you need something with better granularity
than the time() function. I cannot give you a function that is guaranteed
to return a value in thousands of seconds and without knowing why you need
that I cannot suggest an alternative.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don''t send e-mail to vi************@whitehouse.gov


这篇关于时间输出,以毫秒为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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