每纳秒调用一个函数?
不确定考虑到计算机硬件或电子的物理限制这是否可能,但是任何编程语言中是否存在一种实用的方法来每纳秒调用一个函数?存在什么限制?
例如,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每纳秒调用一个函数意味着在 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.
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.