Skip to content

lucascompython/speed-comparison

Repository files navigation

Speed comparison of programming languages

This project compares the speed and memory of different programming/scripting languages.

It calculates prime and composite numbers with a very basic algorithm and an more advanced one, from any given interval to do this comparisom.
All languages use the same algorithms.


Preview


Methods

The code below is a Python code snippet of the not optimal but not bad algorithm used for counting prime and composite numbers to a given range, that is used for the different languages.

for num in range(rounds):
    ctr = 0

    for i in range(2, num // 2):
        if num % i == 0:
            ctr += 1
            composites += 1
            break

    if ctr == 0 and num != 1:
        primes += 1

The code below is an implemation of the Sieve of Eratosthenes in Python

primes = [True for _ in range(rounds + 1)]

for i in range(2, round(sqrt(rounds)) + 1):
    if primes[i]:
        for j in range(i ** 2, rounds, i):
            primes[j] = False

total = 0
for k in range(2, rounds):
    if primes[k]:
        total += 1
composites = rounds - total

Disclaimer

I'm no expert in all these languages, so take my results with a grain of salt.
The speed of the language does not determine its quality.

You are also more than welcome to contribute and help me.
See Known Bugs.

The results shown in preview may vary A LOT because im running this tests on a potato :(

Languages used in this comparison

In total there are available 14 languages in this comparison which are:

Features

  • Graphs
  • Tables
  • Advanced but simple command line integration with multiple options for comparing (E.g: choosing with languages to compare and changing the values of the iterations).
  • All languages will read from a txt file.
  • The program will get and display the memory usage.
  • It also has two modes, one for running native and other for running inside a docker container.
  • Can choose to run in sequence or in parallel.
  • Saves results in various formats.

Run it yourself

To measure the execution time, in each language is implemented a timer. To measure the compilation / interpretation time and peak memory usage, before each measurement the GNU time command is invoked. The plots are made with MatPlotLib. Everything can be ran inside a Docker container.

Requirements

  • Docker
  • Makefile support

Instalation and Execution

git clone https://github.com/lucascompython/speed-comparison.git
cd speed-comparison
make run-native #to run natively must have all requirements, to run inside docker use make build && make run-docker 

FAQ

Does it work on Windows?

Yes, I think...

Are the compile/interpret times included in the comparison?

Yes, it's measured by the GNU time command!

Why do you also count reading a file and printing the ouput?

Because I think this is a more realistic scenario to compare speeds.

TODO (Mostly by Order)

  • Add TypeScript with Deno
  • Add JavaScript with Node
  • Add Java
  • Finish adding an optimized version for each language using compiler optimizations and using the Sieve of Eratosthenes
  • Add an option to run the comparisons in parallel
  • Add an option to run natively
  • Add C#
  • Add Lua
  • Add Php
  • Add Ruby
  • Add Golang
  • Add Rust
  • Add Terminal graphs
  • Add Powershell
  • Add Swift
  • Add Dart
  • Add the ability to run the comparison multiple times and then show the mean, best and worst time.
  • Add Bash
  • Add Zig
  • Add Julia
  • Add V
  • Add other methods of comparing E.g: Leibniz formula for π to compare the floating point operations
  • Add full Docker Support
  • Add Kotlin
  • Add F#
  • Add Haskell
  • Add Scala
  • Add Nix
  • Add R
  • Add R plots instead of MatPlotLib
  • Add Elixir
  • Add Raku (Perl6)
  • Add Nim
  • And many others...

Known Bugs (Because im too lazy to fix them...)

  • For the first time running the comparison you might need to to run it twice because deno shows some extra information at the first compilation of a file.
  • Docker doesn't support graphical graphs for now.
Super top Secret 🤫
  1. Most languages are good, use whatever you want.
  2. Execpt HTML
  3. Fuck HTML