1
\$\begingroup\$

This question already has an answer here:

A simple code golf challenge similar to an already existing, but more complex one:

  1. Wait for user to press enter.
  2. Wait for user to press enter again.
  3. Print the elapsed time between the 1. and 2.
  4. Wait an equal amount of time
  5. Print the actual elapsed time of 4.

Order of 3. and 4. is not important. Notice that the second printout entails actual timekeeping during the machine's delay; just outputting the desired delay is not enough.

The time outputs should be in seconds, with at least 10 ms resolution.

\$\endgroup\$

marked as duplicate by Alex A. code-golf Dec 21 '15 at 20:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ @DigitalTrauma Fixed. Thanks. \$\endgroup\$ – Adám Dec 21 '15 at 19:19
  • 1
    \$\begingroup\$ "Simpler" does not imply "not a duplicate." \$\endgroup\$ – Alex A. Dec 21 '15 at 20:56
2
\$\begingroup\$

MATL, 14 bytes

This answer uses the current version of the language (3.1.0), which is earlier than the challenge.

jY`jZ`tDY`Y.Z`

Example

I tried to wait for about 1 second:

>> matl jY`jZ`tDY`Y.Z`
> 
> 
1.011063770698121
1.013172086910349

Explanation

j       % input string (will be empty)
Y`      % start a stopwatch timer      
j       % input string (will be empty) 
Z`      % read the stopwatch timer and push the value onto the stack    
tD      % duplicate, convert to string and display
Y`      % start a stopwatch timer (this doesn't produce any output)      
Y.      % pause the specified amount of time (which is at the top of the stack)
Z`      % read the stopwatch timer (will be implicitly displayed)  
\$\endgroup\$
1
\$\begingroup\$

R, 87 bytes

code

f=readline;s=Sys.time;f();q=s();f();e=(m=s())+(z=(m-q));while(s()<e)1;cat(z,"\n",s()-m)

example

> f=readline;s=Sys.time;f();q=s();f();e=(m=s())+(z=(m-q));while(s()<e)1;cat(z,"\n",s()-m)
> 1.343448     # 1 second wait plus 0.3s mental lag
> 1.358646
\$\endgroup\$
  • \$\begingroup\$ If I take the difference of two Sys.time R output's Time difference of x seconds, is this a valid printout instead of using cat()? \$\endgroup\$ – Mutador Dec 21 '15 at 19:34
1
\$\begingroup\$

Matlab: 44 bytes ##

input('');tic;input('');t=toc
pause(t);toc-t

Output

t =
    1.8318
ans =
    1.8338

Explanation: input('') wait for enter, tic starts timer, t=toc display

\$\endgroup\$
0
\$\begingroup\$

Bash + GNU utilities, 63

d=0
f(){ read;d=`date +%s.%N-$d|bc -l`;}
f
f
bc<<<$d/1
sleep $d
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 71 bytes

(b=InputString)[];a=(c=Print@*First)@(d=AbsoluteTiming)@b[];c@d@Pause@a

Needs to be run in v10.3 or higher. Good thing that symbols are first-class objects...

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.