ANONYMOUS wrote:
> I heard Chris say in one of the lectures/workshops that one of the ways you could advance time was to increment the clock by 1usec at a time and check what's happening at that point. However, this would mean for things like longsleep, there would be 5 million operations of checking the state of just one process, which is very slow. And if there were 10 longsleeps, then every process would need to be checked 5 million times each, which would be even slower. Have I misunderstood what he meant by incrementing 1usec at a time? Or is incrementing by 1usec just *one* of the ways you can do it, but perhaps not the best?
Your thinking and the stated numbers are correct, but the statement '...which is very slow' is not true.
How long will it take to perform 5 million (integer) comparisons on a laptop capable of 2 billion instructions per second?
Your laptop, itself, can answer the question with a simple program (an experiment). Actually, each iteration through the loop also requires a test, so the timing is really an overestimate.
There are other, less simple, approaches which advance a clock, other than by 1usec each time, by determining (guaranteeing) what will happen next, but it may take more effort to determine that.
But keep in mind that the speed of your project is not being assessed, that you're being assessed on your understanding of the problem, not a super-efficient implementation.
#include <stdio.h>
#include <inttypes.h>
#include <sys/time.h>
// RETURNS THE CURRENT TIME, AS A NUMBER OF MILLISECONDS, IN A 64-bit INTEGER
int64_t milliseconds(void)
{
struct timeval now;
gettimeofday( &now, NULL ); // timezone not required, so we pass NULL
return ((int64_t)now.tv_sec * 1000) + now.tv_usec/1000;
}
int main(int argc, char *argv[])
{
int x = 3;
int64_t start = milliseconds();
for(int i=0 ; i<5000000 ; ++i) {
if(i == x) {
; // do nothing
}
}
printf("%llimsec\n", milliseconds() - start);
return 0;
}