开发者

Call a function every nanosecond?

开发者 https://www.devze.com 2023-03-24 13:12 出处:网络
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 li

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>


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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消