JS/PyS Code Timer

 

This is a simple code timer that measures (in milliseconds) how long it takes code running in the browser to count from 0 to 1,000,000. The timer begins when you click one of the "Go!" buttons. The top button executes a JavaScript function. The middle button executes an equivalent function written in Python, running within the PyScript framework (which allows Python to be run directly in the browser). The bottom button executes both (though not simultaneously--keep reading).

For added fun, open the console and run each function and both together. The console will print the start and completed time for each function. Regardless of the order in which the onClick events appear in the html or the order of the scripts in the body of the html, the JavaScript function always runs first.


JavaScript Function

 


Python Function

 


Run Both



import time def py_run(*args, **kwargs): x = 0 start = time.perf_counter() console.log(f"Python start: {start * 1000}") while x < 1000000: x += 1 stop = time.perf_counter() timed = f"{(stop - start) * 1000}" console.log(f"Python executed: {stop * 1000}") display = f"{timed} milliseconds" Element('py_result').write(display)