[Question]
  [
Program the shortest code that will calculate the average BPM (Beats per Minute) using 8 data points of time passed. This is my first post, and I haven't seen a question similar to this. Since I am a fan of rhythm games, it would be nice to have a small handy tool to calculate BPM using your own keyboard and your own sense of rhythm (if you have one...)
# Challenge
* All inputs must be singular. (i.e. Only pressing "enter" as "input")
* Your program must take at least 9 inputs. (To create 8 data points of time passed.)
* Your program should output the BPM of the keypresses rounded at 2 decimal points. (i.e. 178.35)
* **Timing starts at first input. Not start of program**
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest answer wins.
# Example Input and Output
Provide at least one example input and output. Make sure they match your own description of what the input should look like.
Input:
```
> [Enter-Key]
# User waits 1 second
...  7 more inputs
> [Enter-Key] 
```
Output:
```
> 60
```
**Current winner is [KarlKastor](https://codegolf.stackexchange.com/a/135955/72656) at 22 Bytes using Pyth**
**Even though the winner was using Pyth, the Matlab answer was a notable answer.**
  
[Answer]
# MATLAB/Octave, ~~58 56~~ 55 bytes
Thanks @LuisMendo for -1 byte!
```
input('');tic;for k=1:7;input('');end;fix(48e3/toc)/100
```
You have to press enter 9 times. (Also works in Octave.)
Here you see it in action, left MATLAB, right Octave:
![](https://i.stack.imgur.com/L51Ng.gif)![](https://i.stack.imgur.com/NbTfA.gif)
[Answer]
# JavaScript (ES6), ~~77~~ 74 bytes
```
(i=8,n=Date.now,t)=>onclick=_=>(t=t||n(),i--||alert((48e6/(n()-t)|0)/100))
```
Creates an `onclick` event on the global `window` object. Send beats by clicking anywhere in the window.
## Test Snippet
```
let f=
(i=8,n=Date.now,t)=>onclick=_=>(t=t||n(),i--||alert((48e6/(n()-t)|0)/100))
f()
document.write("Running... click anywhere here")
```
```
*{user-select:none}
```
[Answer]
# Python 3, 93 92 81 bytes
Saved 11 bytes thanks to [Felipe](https://codegolf.stackexchange.com/users/66418/felipe-nardi-batista).
```
import time
a,*l,b=[input()or time.time()for i in' '*9]
print(round(480/(b-a),2))
```
[Answer]
# Bash + common Linux utilities, 58
```
script -tt -c'sed -n 9q'
sed '1c2k0
s/ 2/+/;$a480r/p' t|dc
```
[Answer]
## **Javascript, 100, 84 thanks to [Powelles](https://codegolf.stackexchange.com/users/52433/powelles), 82 bytes thanks to [Justin Mariner](https://codegolf.stackexchange.com/users/69583/justin-mariner)**
```
z=>{(a=alert)();i=0;s=(n=Date.now)();while(i++<8)a();a((48e4/(n()-s)).toFixed(2))}
```
```
(z=>{(a=alert)();i=0;s=(n=Date.now)();while(i++<8)a();a((48e4/(n()-s)).toFixed(2))})();
```
[Answer]
# Java 1.5+, ~~345~~ ~~339~~ ~~361~~ 337 bytes
-34 bytes thanks to Michael for pointing out I forgot to fix my imports
```
import java.awt.event.*;class B{public static void main(String[]a){new java.awt.Frame(){{addKeyListener(new KeyAdapter(){long x=System.currentTimeMillis();int b=0;public void keyPressed(KeyEvent e){if(e.getKeyChar()==' '&&b++==9){System.out.println(Math.round(6000000.0*b/(System.currentTimeMillis()-x))/100.0);}}});setVisible(1>0);}};}}
```
Listens to the user as they press the space bar. Then, when the user has pressed it 9 times, prints back to the user the current BPM:
[![enter image description here](https://i.stack.imgur.com/UIXMX.png)](https://i.stack.imgur.com/UIXMX.png)
*Image has debug messages not present in golfed code.*
---
# Ungolfed:
```
import java.awt.event.*;
class B {
    public static void main(String[] a) {
        new java.awt.Frame() {
            {
                addKeyListener(new KeyAdapter() {
                    long x = System.currentTimeMillis();
                    int b = 0;
                    public void keyPressed(KeyEvent e) {
                        if (e.getKeyChar() == ' ' && b++ == 9) {
                            System.out
                                    .println(Math.round(6000000.0 * b
                                            / (System.currentTimeMillis() - x)) / 100.0);
                        }
                    }
                });
                setVisible(1 > 0);
            }
        };
    }
}
```
---
Kinda fun to try and get a highscore...
```
KEY PRESS0 AT 250ms.
KEY PRESS1 AT 343ms.
KEY PRESS2 AT 468ms.
KEY PRESS3 AT 563ms.
KEY PRESS4 AT 672ms.
KEY PRESS5 AT 781ms.
KEY PRESS6 AT 880ms.
KEY PRESS7 AT 989ms.
485
```
[Answer]
# C# (.NET Core), ~~193~~ ~~206~~ ~~189~~ ~~186~~ ~~155~~ ~~143~~ 137 bytes
*-47 bytes thanks to [TheLethalCoder](https://codegolf.stackexchange.com/users/38550/thelethalcoder)*  
*-4 bytes thanks to [Nazar554](https://codegolf.stackexchange.com/users/29636/nazar554)*  
*-16 bytes thanks to [Luc](https://codegolf.stackexchange.com/users/15738/luc)*  
*-2 bytes thanks to [Kamil Drakari](https://codegolf.stackexchange.com/users/71434/kamil-drakari)*
```
_=>{var x=new long[9];for(int i=0;i<9;){Console.ReadKey();x[i++]=DateTime.Now.Ticks;}return Math.Round(48e8/(x[8]-x[0]),2);}
```
Also added to byte count: 
```
using System;
```
Whole program:
```
namespace System
{
    class A
    {
        static void Main()
        {
            Func<int, double> f = _ =>
            {
                var x = new long[9];
                for (int i = 0; i < 9; )
                {
                    Console.ReadKey();
                    x[i++] = DateTime.Now.Ticks;
                }
                return Math.Round(48e8 / (x[8] - x[0]), 2);
            };
            Console.WriteLine(f(0));
        }
    }
}
```
[Answer]
## C++, 150 bytes
```
#include<iostream>
#include<ctime>
#define G getchar()
void f(){G;auto s=clock();G;G;G;G;G;G;G;G;std::cout<<round(6000/(double(clock()-s)/8000))/100;}
```
[Answer]
# Python + curses, 122 bytes
```
import curses as C,time as T
s=C.initscr()
C.cbreak()
t=0
exec's.getch();t=t or T.time()'*9
print'%.2f'%(540/(T.time()-t))
```
Requires the `curses` module to be loaded.
-9 bytes thanks to Felipe Nardi Batista
[Answer]
## vba, 57
```
msgbox"":x=timer:for i=1to 8:msgbox"":next:?480/(timer-x)
```
press enter, or click on the OK in the message box.
[Answer]
# [Python 3](https://docs.python.org/3/), 74 bytes
```
from timeit import*;print('%.2f'%(480/timeit('input()',input(),number=8)))
```
[Try it online!](https://tio.run/##K6gsycjPM/7/P60oP1ehJDM3NbNEITO3IL@oRMu6oCgzr0RDXVXPKE1dVcPEwkAfokBDPTOvoLREQ1NdB8rQySvNTUotsrXQ1NT8/58LBgA "Python 3 – Try It Online")
This will give you silly numbers in TIO since it runs all the inputs at once but it does work. `timeit()` returns the execution time of the statement `'input()'` in seconds excluding the setup parameter `input()`.
[TIO with a 1s delay](https://tio.run/##NYo7CoAwEAV7T5FGsivivxCClxEiLphkiWvh6SP@XjUzPD5lDb5PiRyHKErI2WyJwT1Eot5eGI7kBXRedYvOYRib@j2AJs@HAJrbq32zlqFFXX659IebbZxGREwp@3cB "Python 3 – Try It Online") per input for validation.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 22 bytes
```
 wJ.d0 mw8.Rc480-.d0J2
```
(yes there's a leading space)
Input is 'enter' presses.
If additional output is allowed I can remove the spaces and get a score of 20 bytes.
[![example execution](https://i.stack.imgur.com/QUrao.png)](https://i.stack.imgur.com/QUrao.png)
## explanation
```
 wJ.d0 mw8.Rc480-.d0J2
 w                      # take the first input
                        # (space) throw the result away
  J.d0                  # store the current system time in J
       mw8              # take 8 inputs and throw the result away
                 -.d0J  # difference of current time and J
             c480       # divide 480 by this
           .R         2 # round to 2 decimal places 
```
[Answer]
# Java 8, ~~180~~ 135 bytes
*-45 bytes thanks to @SocraticPhoenix suggesting to use `System.in` directly.*
```
x->{System.in.read();long t=System.nanoTime(),i=0;for(;i++<8;System.in.read());System.out.printf("%.2f",4.8e11/(System.nanoTime()-t));}
```
An anonymous lambda function with an unused argument that must be assigned to a functional interface method that throws an `Exception` (example below). Reads from console; beats are sent by pressing `enter`.
## Ungolfed w/ Surrounding Test Code
```
public class A {
    interface F{void f(Object x) throws Exception;}
    public static void main(String[]a) throws Exception {
        F f =
        x->{
            System.in.read();
            long t=System.nanoTime(),i=0;
            for(;i++<8;System.in.read());
            System.out.printf("%.2f",4.8e11/(System.nanoTime()-t));
        }
        ;
        f.f(null);
    }
}
```
[Answer]
# C#, 117 bytes
There's already a [C# (.NET Core) answer](https://codegolf.stackexchange.com/a/135496/15738) that this one builds on. Added interpolated string (that .NET Core seems to lack) for output and shaved some bytes by using long array instead of DateTime.
```
_=>{var d=new long[9];for(var i=0;i<9;){Console.ReadKey();d[i++]=DateTime.Now.Ticks;}return$"{48e8/(d[8]-d[0]):n2}";}
```
## Humane version
```
class Program
{
    static void Main()
    {
        Func<int, string> f = _ =>
        {
            var d = new long[9];
            for (var i = 0; i < 9;)
            {
                Console.ReadKey();   // Switch these two to "automate" key presses.
                //Thread.Sleep(100); 
                d[i++] = DateTime.Now.Ticks;
            }
            return $"{48e8 / (d[8] - d[0]):n2}";
        };
        var result = f(1);
        Console.WriteLine();
        Console.WriteLine(result);
        Console.ReadKey(true);
    }
}
```
[Answer]
# R, ~~79~~ 84 bytes
```
scan();s=Sys.time;x=s();replicate(8,scan());cat(round(60/as.numeric((s()-x)/8),d=2))
```
Only works when using enter, since that will end scan immediately. Explicitly uses `print` for its `digits` argument, handling the rounding.
```
> scan();s=Sys.time;x=s();replicate(8,scan());cat(round(60/as.numeric((s()-x)/8),d=2))
1: 
Read 0 items
numeric(0)
1: 
Read 0 items
1: 
Read 0 items
1: 
Read 0 items
1: 
Read 0 items
1: 
Read 0 items
1: 
Read 0 items
1: 
Read 0 items
1: 
Read 0 items
[[1]]
numeric(0)
[[2]]
numeric(0)
[[3]]
numeric(0)
[[4]]
numeric(0)
[[5]]
numeric(0)
[[6]]
numeric(0)
[[7]]
numeric(0)
[[8]]
numeric(0)
[1] 439.47
```
[Answer]
# Ruby, 58 bytes
```
gets;t=Time.now;8.times{gets};p (480/(Time.now-t)).round 2
```
]