Performance comparison between Python, Rust, C++, and JavaScript through Discord bots.
This project was born from a concrete optimization need. I had initially developed a Discord bot in Python (Linkify) for its development simplicity. Although the code was well-organized and functional, the performance was not optimal.
Wanting to maximize performance before production deployment, I wanted to explore the impact of programming language choice. I therefore implemented the same bot in different languages:
- Rust and C++ for their native performance
- JavaScript with the official Discord.js library for comparison
The benchmark performs the same intensive mathematical calculation to measure the pure performance of each language, independently of the Discord API:
Tested operation: Loop of 1,000,000 iterations with for each iteration i:
x = i Γ 1.0
result += sin(x Γ Ο) + cos(x Γ· e) + (βx Γ β2)
Where:
Ο β 3.14159(pi approximation)e β 2.71828(Euler's number approximation)β2 β 1.414(square root of 2 approximation)
This operation combines trigonometric calculations (sine, cosine) and algebraic operations (square root, multiplications, divisions) to intensively stress the processor and reveal performance differences between languages.
The execution time differences are significant and mainly come from the programming language used. Although each Discord library may have its own optimizations, the performance impact remains negligible compared to the gaps between languages during intensive calculations.
This analysis demonstrates the value of rewriting critical bots in Rust or C++ to:
- Reduce response times
- Save server resources
- Improve error handling (especially with Rust vs Python)
Note: If you check my GitHub, the Linkify bot might be available in Python version, Rust version, or both depending on the progress of the rewrite project.
- Python - Debug mode (discord.py)
- Rust - Release mode (Serenity)
- C++ - Release/debug mode (DPP 10.1)
- JavaScript - Node.js (discord.js)
- Python 3.x
- Rust (cargo)
- Node.js
- Visual Studio (for C++)
Create a .env file at the root based on .env.example
For the C++ bot, you must install DPP 10.1 dependencies manually. Follow this installation video: https://www.youtube.com/watch?v=JGqaQ9nH5sk
The dependencies folder is not included in git to avoid large files.
# Windows
scripts/launch_all.batcd bots/python
python bot.pycd bots/rust
cargo run --releasecd bots/js
npm install
node bot.jsCompile with Visual Studio then run MyBot.exe
In Discord, type ?benchmark to launch the performance test.
Currently, dependency management for each language can be complex and requires installing multiple development environments. A planned improvement consists of encapsulating each bot in a Docker container to:
- Simplify installation - A single
docker-compose upto launch all bots - Isolate environments - Each language in its own container
- Standardize deployment - Same behavior on all systems
- Facilitate maintenance - Centralized version and dependency management
This approach would transform the current multi-step installation into a single command, regardless of operating system.
Addition of a launch_all.sh script for macOS and Linux systems, equivalent to the Windows launch_all.bat script, to enable bot launching on all Unix-like systems.
bots/
βββ python/ # Python Bot
βββ rust/ # Rust Bot
βββ cpp/ # C++ Bot
βββ js/ # JavaScript Bot
scripts/
βββ launch_all.bat # Windows Script
