int main () { time_t start,end; //double length; int number, input; int count=10; /* initialize random seed: */ srand ( time(NULL) ); /* Generate a random number: */ number = rand() % 100 + 1; //start the timer time(&start); do { //take input from the user about guessing the number printf (" Guess the number (1 to 100): "); scanf ("%d",&input); //various conditions to check weather the guessed number from user is smaller or higher if ((number)< input && input < (number+10)) printf ("The number is smaller\n"); else if ((number-10)< input && input< (number)) printf ("The number is higher\n"); else if (input > number) printf("The number is much smaller\n"); else if (input < number) printf("The number is much higher\n"); else printf("\n"); //reducing the count everytime user guesses a number count=count-1; //cond=(number!=input); //printf("%d Count \n",cond); } while (number!=input && count!=0); //end the time when the loop ends time(&end); printf("You finished the game at "); printf("%s\n",ctime(&end) ); //length = difftime(end,start); //checking if the user won or lost depending on the conditions if (count!=0) { //calling the function to calculate the time taken by the user printf("You Won and finished in %f",timedifference(end,start)); //printf("You Won and finished in %d",length); printf(" seconds and took %d",(10-count)); printf(" attempts\n"); } else { //calling the function to calculate the time taken by the user printf ("You LOST and took %f",timedifference(end,start)); //printf ("You LOST and took %d",length); printf(" secs\n"); } return 0; }
float StopWatch::elapsedsec(void) { if(!started) return -1; timespec tmp; clock_gettime(CLOCK_MONOTONIC, &tmp); tmp = timedifference(starttime,tmp); return tmp.tv_sec + (float)tmp.tv_nsec/1000000000; }
/** * C-routine that monitors time in a thread, updates a timeserver structure<br> * bugs: none found<br> * @param ptr timeserver* casted in as a void*<br> */ void* monitortime(void* ptr){ timeserver* timer = (timeserver*)ptr; cout << "[STARTUP]\tTime monitoring" << endl; timer->inittime(); timer->time.previous = clock(); while(timer->running){ sleep(20); timer->time.now = clock(); if(timer->monitoring && timedifference(timer->time.previous, timer->time.now) >= 1000.0){ timer->updatetime(); timer->time.previous = timer->time.now = clock(); } } pthread_exit((void*) 0); return((void*) 1); }
// Main to test this algorithm int main() { // Setup timevals struct timeval startTime, endTime; // Run a variety of functions __initialize(); // Initialize the page fault simulator printf("=-=-=-=-= MEMORY MAXER\n\n"); gettimeofday(&startTime, NULL); // Measure time performance memoryMaxer(); gettimeofday(&endTime, NULL); // Measure time performace printf("=-=-=-=-= Memory Maxer Statistics:\n"); printf(" Time taken for execution with random algorithm for picking pages = %ld msec\n\n", timedifference(endTime, startTime)); printf("=-=-=-=-= PERSONAL FUNCTION 1\n\n"); gettimeofday(&startTime, NULL); // Measure time performance memoryTester(); gettimeofday(&endTime, NULL); // Measure time performace printf("=-=-=-=-= Personal Function 1 Statistics:\n"); printf(" Time taken for execution with random algorithm for picking pages = %ld msec\n\n", timedifference(endTime, startTime)); return 0; }