0
0
PythonProgramBeginner · 2 min read

Python Program to Create a Stopwatch

You can create a stopwatch in Python using the time module by recording the start time with time.time(), then calculating elapsed time by subtracting the start time from the current time.
📋

Examples

InputStart stopwatch, wait 2 seconds, stop stopwatch
OutputElapsed time: 2.00 seconds
InputStart stopwatch, wait 0 seconds, stop stopwatch
OutputElapsed time: 0.00 seconds
InputStart stopwatch, wait 5.5 seconds, stop stopwatch
OutputElapsed time: 5.50 seconds
🧠

How to Think About It

To create a stopwatch, first record the current time when starting. When stopping, get the current time again and subtract the start time to find how many seconds passed. Display this elapsed time to the user.
📐

Algorithm

1
Record the current time as start time when the stopwatch starts
2
Wait or perform actions while stopwatch is running
3
Record the current time as end time when stopping
4
Calculate elapsed time by subtracting start time from end time
5
Display the elapsed time in seconds
💻

Code

python
import time

print('Press Enter to start the stopwatch')
input()
start_time = time.time()
print('Stopwatch started... Press Enter to stop')
input()
end_time = time.time()
elapsed_time = end_time - start_time
print(f'Elapsed time: {elapsed_time:.2f} seconds')
Output
Press Enter to start the stopwatch Stopwatch started... Press Enter to stop Elapsed time: 2.00 seconds
🔍

Dry Run

Let's trace starting the stopwatch, waiting 2 seconds, then stopping it.

1

User presses Enter to start

start_time = 1700000000.00 (example timestamp)

2

User waits 2 seconds

Time passes...

3

User presses Enter to stop

end_time = 1700000002.00 (example timestamp)

4

Calculate elapsed time

elapsed_time = 1700000002.00 - 1700000000.00 = 2.00 seconds

5

Display elapsed time

Print 'Elapsed time: 2.00 seconds'

StepActionValue
1Start time recorded1700000000.00
2Wait2 seconds
3End time recorded1700000002.00
4Elapsed time calculated2.00 seconds
5Output elapsed timeElapsed time: 2.00 seconds
💡

Why This Works

Step 1: Record start time

Using time.time() captures the current time in seconds since a fixed point, so we know when the stopwatch started.

Step 2: Record end time

When stopping, calling time.time() again gives the current time to compare with the start.

Step 3: Calculate elapsed time

Subtracting start time from end time gives the total seconds passed, which is the stopwatch duration.

Step 4: Display result

Printing the elapsed time with formatting shows the user how long the stopwatch ran.

🔄

Alternative Approaches

Using time.perf_counter()
python
import time

input('Press Enter to start')
start = time.perf_counter()
input('Press Enter to stop')
end = time.perf_counter()
print(f'Elapsed time: {end - start:.2f} seconds')
time.perf_counter() is more precise for measuring short durations but less intuitive than time.time()
Using a class to manage stopwatch
python
import time

class Stopwatch:
    def __init__(self):
        self.start_time = None
    def start(self):
        self.start_time = time.time()
    def stop(self):
        return time.time() - self.start_time

sw = Stopwatch()
input('Press Enter to start')
sw.start()
input('Press Enter to stop')
elapsed = sw.stop()
print(f'Elapsed time: {elapsed:.2f} seconds')
Using a class organizes code better and allows reuse or extension

Complexity: O(1) time, O(1) space

Time Complexity

The stopwatch program runs in constant time because it only records timestamps and calculates their difference once.

Space Complexity

It uses a fixed amount of memory to store start and end times, so space complexity is constant.

Which Approach is Fastest?

Using time.perf_counter() is slightly faster and more precise than time.time(), but both are efficient for this use.

ApproachTimeSpaceBest For
time.time()O(1)O(1)Simple stopwatch with wall-clock time
time.perf_counter()O(1)O(1)High precision timing
Class-based stopwatchO(1)O(1)Reusable and extendable stopwatch code
💡
Use time.perf_counter() for more accurate timing in stopwatches.
⚠️
Beginners often forget to subtract start time from end time, printing the wrong elapsed time.