Skip to content

πŸš€ Performance benchmark comparing Python, Rust, C++, and JavaScript through identical Discord bots. Measures computational efficiency via intensive math operations.

Notifications You must be signed in to change notification settings

StenguyzCSGO/Discord_Bot_Benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Discord Bot Benchmark

Performance comparison between Python, Rust, C++, and JavaScript through Discord bots.

Motivation

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.

Observed Results

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.

Tested Languages

Installation

Prerequisites

  • Python 3.x
  • Rust (cargo)
  • Node.js
  • Visual Studio (for C++)

Configuration

Create a .env file at the root based on .env.example

C++ Dependencies

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.

Usage

Launch all bots

# Windows
scripts/launch_all.bat

Launch individually

Python

cd bots/python
python bot.py

Rust

cd bots/rust
cargo run --release

JavaScript

cd bots/js
npm install
node bot.js

C++

Compile with Visual Studio then run MyBot.exe

Benchmark Test

In Discord, type ?benchmark to launch the performance test.

Example

Benchmark Results

Future Improvements

Docker Containerization

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 up to 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.

Cross-platform launch script

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.

Structure

bots/
β”œβ”€β”€ python/     # Python Bot
β”œβ”€β”€ rust/       # Rust Bot
β”œβ”€β”€ cpp/        # C++ Bot
└── js/         # JavaScript Bot
scripts/
└── launch_all.bat  # Windows Script

About

πŸš€ Performance benchmark comparing Python, Rust, C++, and JavaScript through identical Discord bots. Measures computational efficiency via intensive math operations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •