为什么时间(time

编程入门 行业动态 更新时间:2024-10-21 18:54:29
本文介绍了为什么时间(time_t的*)函数都返回并设置由-REF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直很好奇,为什么在时间(time_t的*)函数都返回一个 time_t的,和时间设置为指针传递的?

I've always been curious, why does the time(time_t *) function both return a time_t, and set the time to the passed in pointer?

返回时间的示例:

time_t myTime = time(NULL); printf("The time is now %s", ctime(&myTime));

的值设置为指针的示例:

Example of setting the value to the pointer:

time_t myTime; time(&myTime); printf("The time is now %s", ctime(&myTime));

我原本以为会有被写入内存而不是返回的性能增益,但如果它做两件事,不只是让它慢?

I originally thought there would be a performance gain by writing to the memory instead of returning, but if it has to do both, doesn't that just make it slower?

推荐答案

有在它的当前定义的方式没有真正的好处。

There's no real benefit in the way it's currently defined.

我怀疑,当时间()最初定义的功能,它采用了无法从一个函数返回一个类型。非常早期的C实现没有长整型并没有能够从函数返回的结构。上与16位整数,重新present一个时间将是作为一个结构或作为阵列的唯一方式的系统;值得一秒钟16位是不到一天的时间。

I suspect that when the time() function was first defined, it used a type that could not be returned from a function. Very early C implementations didn't have long int and were not able to return structures from functions. On a system with 16-bit ints, the only way to represent a time would be as a structure or as an array; 16 bits worth of seconds is less than a day.

的时间这么早实现()可能已经使用这样的事情(炒):

So early implementations of time() might have been used something like this (speculation):

time_t now; time(&now); /* sets now.time_high, now.time_low */

或者

int now[2]; time_t(now); /* sets now[0], now[1] */

在后C实现增加更长的整数和值返回结构的能力,返回从时间 time_t的价值的能力()加入功能,但旧的功能保持,以避免破坏现有的code。

When later C implementations added longer integers and the ability to return structures by value, the ability to return a time_t value from the time() function was added, but the old functionality was kept to avoid breaking existing code.

我认为,如果时间()今天正在界定了,它会看起来更像是这样的:

I think that if time() were being defined today, it would look more like this:

time_t time(void);

我一直没能确认的时间旧的实现()函数的工作这种方式(尝试谷歌搜索时间!),但它给出有意义语言的历史。

I haven't been able to confirm that old implementations of the time() function worked this way (try Googling "time"!), but it makes sense given the history of the language.

如果你传递一个空指针到时间()函数,它返回当前时间也没有将其存储在一个变量;这就避免了一些性能损失的:

If you pass a null pointer to the time() function, it returns the current time without also storing it in a variable; this avoids some of the performance penalty:

time_t now = time(NULL);

更多推荐

为什么时间(time

本文发布于:2023-11-25 15:35:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!