每纳秒调用一个函数?

发布于 2024-11-27 05:17:14 字数 490 浏览 2 评论 0原文

不确定考虑到计算机硬件或电子的物理限制这是否可能,但是任何编程语言中是否存在一种实用的方法来每纳秒调用一个函数?存在什么限制?

例如,在 javascript 中尝试这不会按预期进行:

<html>
<head>
<script type="text/javascript">

var numb = 1;

function addNum(){

numb=numb+1;
document.getElementById('thing').innerHTML = numb;

}
</script>

</head>

// try to do addNum every nanosecond
<body onload='setInterval("addNum()", 0.000001)'>

<div id="thing"></div>

</body>
</html>

Not sure if this is possible in light of physical limits on computer hardware or electrons but does there exist a practical way in any programming language to call a function every nanosecond? What limits exist?

For example in javascript trying this does not go over as expected:

<html>
<head>
<script type="text/javascript">

var numb = 1;

function addNum(){

numb=numb+1;
document.getElementById('thing').innerHTML = numb;

}
</script>

</head>

// try to do addNum every nanosecond
<body onload='setInterval("addNum()", 0.000001)'>

<div id="thing"></div>

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅过去的我 2024-12-04 05:17:15

每纳秒调用一个函数意味着在 3GHz CPU 上每次调用最多可以使用 3 条指令(假设是单周期指令)。即使只是函数调用的开销,这也远远不够。

Calling a function every nanosecond would mean you can use a maximum of 3 instructions per call on a 3GHz CPU, assuming single-cycle instructions. That's not nearly enough for even just the overhead of a function call.

|煩躁 2024-12-04 05:17:15

Javascript 时间切片量子并没有那么小,哎呀,除非它是硬件实现的,否则您可能无法获得这种粒度,因为我知道没有操作系统会在如此小的粒度上进行时间切片,因为所涉及的开销将使其无用,并且您的代码可能不会由于设置内部计时器和涉及的上下文切换的开销,经常能够执行该操作。

Javascript timeslicing quantums are not that small, heck you probably can't get that granularity unless it's hardware implemented because no operating system that I know will timeslice on such a small granularity because the overhead involved would make it useless and your code will probably not be able to execute that often because of the overhead of setting up the internal timers and the context switching involved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文