1

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>
e82ric
  • 21
  • 3
  • http://stackoverflow.com/questions/6002808/is-there-any-way-to-get-current-time-in-nanoseconds-using-javascript – Lenar Hoyt Jul 30 '11 at 03:26
  • I'm pretty sure a cpu cycle takes almost a nanosecond to even go through it's cycle. – Matt Jul 30 '11 at 03:29
  • @Mitch Wheat because my great-great-grandma needs this answer ASAP! – e82ric Jul 30 '11 at 03:44
  • @e82ric: is that supposed to be humour? – Mitch Wheat Jul 30 '11 at 03:45
  • @Mitch Wheat Yep! you're a very sharp dude – e82ric Jul 30 '11 at 03:46
  • @e82ric: I think I've split my sides. Please, no more wit of this calibre. I fear I might completely explode laughing. – Mitch Wheat Jul 30 '11 at 03:48
  • So how about answering the question: "Why do you think you need to Call a function every nanosecond"? – Mitch Wheat Jul 30 '11 at 03:50
  • @Mitch Wheat -- Short answer being that there are situations in which checking for changes in patterns that happen in that timescale would be very useful. I wondered if common computers have the capability to do things like that yet. Still a beginner in CS btw – e82ric Jul 30 '11 at 03:59
  • What would you be observing that would be changing at a rate of 1GHz? – JimN Jul 30 '11 at 04:06
  • The applications would be numerous! One idea I have is of analyzing video data at that timescale to create super-slow motion video of fast moving objects (lightning, insect flight, explosions, etc).. – e82ric Jul 30 '11 at 04:14
  • This assumes that you have a hardware device capable of processing data at this rate, and I/O capable of receiving data at this rate, and memory reads/writes capable of operating at this rate. – JimN Jul 30 '11 at 04:23

2 Answers2

5

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.

hammar
  • 138,522
  • 17
  • 304
  • 385
2

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.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88