Search code examples
programming-languagestimingmilliseconds

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>

Solution

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