Quote (AbDuCt @ Apr 7 2016 09:31pm)
I request 1000fg for this.
Code
$ ./a.out
a
b
Time elapsed was: 3 seconds, 351 milliseconds
Code
#include <stdio.h>
#include <stdint.h>
#include <time.h>
inline uint64_t rdtsc() {
uint32_t lo, hi;
__asm__ __volatile__ (
"xorl %%eax, %%eax\n"
"cpuid\n"
"rdtsc\n"
: "=a" (lo), "=d" (hi)
:
: "%ebx", "%ecx");
return (uint64_t)hi << 32 | lo;
}
int main() {
unsigned long long x;
unsigned long long y;
char blank;
scanf(" %c", &blank);
x = rdtsc();
scanf(" %c", &blank);
y = rdtsc();
printf("Time elapsed was: %lld seconds, %lld milliseconds\n", ((y-x)/CLOCKS_PER_SEC)/1000, ((y-x)/CLOCKS_PER_SEC)%1000);
return 0;
}
Here's 40 for your troubles. That is way over my head. This is my solution. I don't need help anymore i guess.
Cleaning it up a little.
Code
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
//http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed
//http://unixwiz.net/techtips/termios-vmin-vtime.html
char getch() {
char buf = 0; //set up buffer to hold character
struct termios old = {0}; //set up a termios structure
tcgetattr(0, &old); //get attributes and make backup
old.c_lflag &= ~ICANON; //turn off canonical mode
old.c_lflag &= ~ECHO; //turn off echo
old.c_cc[VMIN] = 1; //set max amount of bytes to recieve
old.c_cc[VTIME] = 0; //set time to elapse between input
tcsetattr(0, TCSANOW, &old);
read(0, &buf, 1); //read 1 character to buffer
old.c_lflag |= ICANON; //restore canonical mode
old.c_lflag |= ECHO; //resore echo
tcsetattr(0, TCSADRAIN, &old); //restore termios structure
return buf;
}
int main ()
{
time_t now; //time first button is pressed
time_t then; //time second button is pressed
struct tm *curtime;
printf("\nThis C program that can measure the time between 2 keys that you click.\n\n");
if(getch())
{
time (&now);
curtime = gmtime(& now);
printf ("First Key Press: %s", ctime (&now));
}
if(getch())
{
time (&then);
printf ("Second Key Press: %s", ctime (&then));
}
printf("The difference between the 2 keys you clicked is %s", difftime(then, now));
return 0;
}