Python Runtime Comparer

Kivy desktop app that times Python programs across growing inputs and plots runtime curves, with sorting algorithms included.

What it is

Python Runtime Comparer is a Kivy desktop app that times a Python program on a series of inputs and plots the result. It ships with five sorting algorithms and a generated input file, so you can compare how each one scales. I built it for studying algorithm complexity.

The problem

Big-O notation says how an algorithm scales, but not what it costs at a given size. Constant factors can reverse the order of two algorithms on small inputs. The only way to see this is to measure. I wanted a tool that runs a program at increasing input sizes and draws the curve.

How it works

The app has three Kivy screens in main.py, with the layout in styling.kv.

  1. MainScreen takes the paths of an input file and a runner file.
  2. FileScreen is the file chooser that fills those paths.
  3. GraphScreen plots the timings.

The input file holds one test case per line. For each line, the app does this:

  1. Write the line to input.txt.
  2. Run the runner with os.system("python <runner>").
  3. Read the elapsed time that the runner wrote to output.txt.

The app saves all times to plot.txt, then draws them with matplotlib. It also fits a quadratic curve to the points. It builds the least-squares normal equations for a + b*i + c*i^2 and solves them with SymPy. The fitted curve is drawn over the measured one.

The ArraySorting/ folder contains the example runners and inputs:

FileContent
bubbleSort.py, insertionSort.py, selectionSort.pyHand-written sorts.
mergeSort.py, quickSort.pyNumPy sort with kind='mergesort' or kind='quicksort'.
inputGenerator.pyWrites 10 lines of random integers, with line n holding 1000 * n numbers.
slowInputs.txt, allInputs.txtGenerated inputs (about 214 KB and 21 MB).

Design decisions

Each run is a separate process. The runner is a standalone script that reads input.txt and writes output.txt. Every test case starts from a clean interpreter, and one runner cannot affect another.

The runner measures itself. Each example runner takes time.time() just before and after the sort. Interpreter startup and file parsing stay outside the measurement. The trade-off is that the author of each runner must follow this contract.

A fitted curve over the raw timings. Real timings are noisy. The quadratic fit shows the trend, so a reader can compare it with the O(n log n) or O(n^2) shape they expect.

Kivy and matplotlib in one language. The whole tool, from file selection to graphing, is Python. The trade-off is that the app runs the runner with python, so it can only time Python programs.

Run it locally

  1. Clone the repository: git clone https://github.com/WhyAsh5114/Python-Runtime-Comparer.git
  2. Install the packages that main.py imports: Kivy, matplotlib, SymPy and the Kivy Garden matplotlib widget. For the sort examples, also install NumPy.
  3. Run the app from the repository root: python main.py
  4. Choose an input file, for example ArraySorting/slowInputs.txt.
  5. Choose a runner file, for example ArraySorting/mergeSort.py.
  6. Select the start button to see the graph.

The repository has no requirements file. The app writes input.txt, output.txt and plot.txt into the working directory.

Reference

ToolUse
PythonApp and example runners.
KivyScreens, file chooser and layout in styling.kv.
matplotlibTiming plot, embedded in Kivy through the Garden widget.
SymPySolves the equations for the quadratic fit.
NumPyArray handling and sort in the example runners.

The repository uses the MIT license.

Notes from the author

This was one of the first tools I built to solve something I cared about. Because I planned to use the results for studying, I pushed to make the measurements reliable.

Outcome

  • The repository was created on 6 September 2022. Most of the work landed between September and December 2022.
  • The last commit is dated 9 December 2023.
  • Two other contributors, 9vishwashah and sundaram123krishnan, have commits, including one merged pull request in April 2023.

Liked Python Runtime Comparer? Here's my background.

Full-stack TypeScript, shipped to real users.