Call a function every nanosecond?
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.
精彩评论