d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso Unix Homework Help > C Program That Detects Time Between 2 Ke
Add Reply New Topic New Poll
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Apr 7 2016 05:38pm
Long story short - I had surgery but homework is still due.

Looking for any help.

Needs to be in C. Cannot use getchar, cannot wait after input, works with any keys.

Thanks in advance. Will pay fg for help.

/edit

Must be simple. Would rather not use curses (We haven't used curses at all in the program).

This post was edited by ROM on Apr 7 2016 05:46pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 7 2016 06:29pm
Thanks for letting us know you


  • had surgery
  • will pay us fg
  • cant use ncurses


But not telling us what we are supposed to be solving.

Great post, would read again.
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Apr 7 2016 06:56pm
Quote (AbDuCt @ Apr 7 2016 08:29pm)
Thanks for letting us know you

  • had surgery
  • will pay us fg
  • cant use ncurses


But not telling us what we are supposed to be solving.

Great post, would read again.


In the topic. Keys got cut short.

"C Program That Detects Time Between 2 Keys"
Member
Posts: 769
Joined: Apr 1 2016
Gold: 0.00
Apr 7 2016 06:58pm
Quote (AbDuCt @ Apr 7 2016 08:29pm)
Thanks for letting us know you

  • had surgery
  • will pay us fg
  • cant use ncurses


But not telling us what we are supposed to be solving.

Great post, would read again.


Yeah, great part is he is paying 140 fg.
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Apr 7 2016 07:01pm
Quote (SoSSoS @ Apr 7 2016 08:58pm)
Yeah, great part is he is paying 140 fg.


140fg for some help seems reasonable. What would you want for your help?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 7 2016 07:31pm
I request 1000fg for this.

Please note that all though the timing function is accurate, it only works on intel based processors, and it also times the run time of the second scanf call. This can screw the results by a few milliseconds.

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;
}


This post was edited by AbDuCt on Apr 7 2016 07:33pm
Member
Posts: 29,346
Joined: Mar 27 2008
Gold: 504.69
Apr 7 2016 07:34pm
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;
}
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 8 2016 07:42am
Code
#include<stdio.h>
#include<time.h>
#include<conio.h>

int main(){
while(true){
time_t now = time(NULL);
printf("start time is : %s\n",ctime(&now));
getch();
double dif = difftime (time(NULL), now);
now = time(NULL);
printf("end time is : %s\n",ctime(&now));
printf ("you took %.f seconds\n", dif );
}
return 0;
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll