An open API service indexing awesome lists of open source software.

https://github.com/tqt97/dsa_benchmark_with_php

Lean Data Structure Algorithms using PHP
https://github.com/tqt97/dsa_benchmark_with_php

algorithms benchmark complexity data-structures php

Last synced: 7 months ago
JSON representation

Lean Data Structure Algorithms using PHP

Awesome Lists containing this project

README

          

# πŸ“š Lean Data Structure Algorithms using PHP

----

## πŸš€ Overview
This project is designed to **learn and benchmark algorithms using PHP**. It focuses on fundamental **data structures, algorithms, and complexity analysis**. The benchmarking system allows performance comparisons of different implementations.

## πŸ”¨ Project Status
🚧 This project is currently under development. Features and benchmarks may change as improvements are made. Contributions and feedback are welcome!

## πŸ‘¨β€πŸ’» Author
[![GitHub](https://img.shields.io/badge/GitHub-TQT97-blue?logo=github&style=for-the-badge)](https://github.com/tqt97)

πŸ“§ Contact: quoctuanit2018@gmail.com

## πŸ›  Features
- βœ… **Benchmarking Engine**: Compare multiple algorithm implementations.
- πŸ“Š **Execution Time Analysis**: Evaluate the efficiency of each function.
- πŸ’Ύ **Memory Usage Tracking**: Identify the most optimized approach.
- 🎨 **Formatted Output**: Clean and structured result presentation.
- πŸ”¬ **Unit Testing**: Ensure the accuracy of benchmark results.

## πŸ“œ List of Algorithms
> πŸ“Œ Click the link below to view all implemented algorithms:

[πŸ“– **View All Algorithms**](algorithms.md) *(updating...)*

### πŸ”’ Categories:
| Algorithm Type | Examples |
|----------------|----------|
| 🏁 **Sorting** | Bubble Sort, Quick Sort, Merge Sort |
| πŸ” **Searching** | Linear Search, Binary Search |
| πŸ”— **Graph Algorithms** | BFS, DFS, Dijkstra’s Algorithm |
| 🧠 **Dynamic Programming** | Fibonacci, Knapsack Problem |
| πŸ’‘ **Greedy Algorithms** | Huffman Coding, Kruskal’s Algorithm |

πŸ“‚ **Full List:** See [`algorithms.md`](algorithms.md)*(updating...)*

---

## πŸ“– What are Data Structure and Algorithms?

### **Data Structure**
A **Data Structure (DS)** is a way of organizing and storing data efficiently to perform operations like searching, sorting, and modifying data effectively. Common types of data structures include:

- **Arrays** πŸ“Š – Fixed-size lists of elements stored in contiguous memory.
- **Linked Lists** πŸ”— – Dynamic lists where elements (nodes) are linked together.
- **Stacks & Queues** πŸ“₯πŸ“€ – LIFO (Last In, First Out) and FIFO (First In, First Out) structures.
- **Trees** 🌳 – Hierarchical structures used in databases, search operations (e.g., Binary Search Trees).
- **Graphs** πŸ•Έ – A set of nodes connected by edges, useful for networking and social connections.
- **Hash Tables** πŸ”‘ – Key-value storage used for quick lookups (e.g., associative arrays, maps).

### **Algorithms**
An **Algorithm** is a step-by-step procedure to solve a problem efficiently. Algorithms work alongside data structures to process data effectively. Some common algorithms include:

- **Sorting Algorithms** πŸ“Œ (Bubble Sort, Quick Sort, Merge Sort)
- **Searching Algorithms** πŸ” (Binary Search, Linear Search)
- **Graph Algorithms** πŸ”— (Dijkstra's Algorithm, BFS, DFS)
- **Divide and Conquer** πŸͺ“ (Merge Sort, Quick Sort)
- **Dynamic Programming** 🎭 (Fibonacci, Knapsack Problem)
- **Greedy Algorithms** πŸ’° (Huffman Coding, Prim’s Algorithm)

---

## πŸ›  **Time Complexity & Space Complexity**
### **Time Complexity** ⏳
Time complexity measures how the runtime of an algorithm grows as the input size increases. It is represented using **Big-O Notation**:

| Complexity | Notation | Example Algorithms |
|-------------|----------|--------------------|
| Constant | O(1) | Hash Table Lookup |
| Logarithmic | O(log n) | Binary Search |
| Linear | O(n) | Linear Search |
| Quadratic | O(nΒ²) | Bubble Sort |
| Exponential | O(2ⁿ) | Recursive Fibonacci|

### **Space Complexity** πŸ’Ύ
Space complexity measures the amount of memory required by an algorithm as the input size grows. It includes:

1. **Auxiliary Space** – Extra space required beyond input data.
2. **Input Space** – Memory occupied by input data.

| Complexity | Notation | Example |
|-------------|----------|---------|
| O(1) | Constant | Swapping two variables |
| O(n) | Linear | Storing results in an array |
| O(nΒ²) | Quadratic | Creating an adjacency matrix for graphs |

πŸ’‘ **Example: Space Complexity in Recursion**
```php
function factorial($n) {
if ($n == 0) return 1;
return $n * factorial($n - 1);
}
```
- This function has **O(n) space complexity** due to recursive stack calls.

---

## πŸ“‚ Project Structure
```
πŸ“¦ project/
│── πŸ“‚ benchmarks/ # πŸ’¨ Algorithm benchmarks
β”‚ β”œβ”€β”€ πŸ“‚ array/ # πŸ”’ Contains array algorithms
β”‚ β”œβ”€β”€ πŸ“‚ graph/ # πŸ•Έ Contains graph algorithms
β”‚ β”œβ”€β”€ πŸ“‚ linked_list/ # πŸ”— Contains linked list algorithms
β”‚ β”œβ”€β”€ πŸ“‚ queue/ # πŸ“€ Contains queue algorithms
β”‚ β”œβ”€β”€ πŸ“‚ string/ # πŸ”‘ Contains string algorithms
β”‚ β”œβ”€β”€ πŸ“‚ stack/ # πŸ“₯ Contains stack algorithms
β”‚ β”œβ”€β”€ πŸ“‚ tree/ # 🌳 Contains tree algorithms
β”‚
│── πŸ“‚ results/ # πŸ“Š Benchmark logs (auto-generated)
β”‚ β”œβ”€β”€ πŸ“‚ array/ # πŸ“ Results of array benchmarks
β”‚ β”œβ”€β”€ πŸ“‚ string/ # πŸ“ Results of string benchmarks
β”‚
│── πŸ“‚ logs/ # πŸ“ Store runtime logs
β”‚
│── πŸ“‚ src/ # πŸ”§ Core logic (business logic)
β”‚ β”œβ”€β”€ πŸ“‚ bootstrap/ # πŸš€ Bootstrap & initialization
β”‚ β”‚ β”œβ”€β”€ πŸ“„ autoload.php # πŸ”„ Load all functions and config
β”‚ │── πŸ“‚ commands/ # πŸ–₯️ CLI command handlers
β”‚ β”‚ β”œβ”€β”€ πŸ“„ benchmark.php # ⚑ Handle benchmarking
β”‚ β”‚ β”œβ”€β”€ πŸ“„ clean.php # 🧹 Handle cleaning results
β”‚ β”‚ β”œβ”€β”€ πŸ“„ list.php # πŸ“œ Generate list of algorithms
β”‚ β”œβ”€β”€ πŸ“‚ functions/ # πŸ—οΈ Core utility functions
β”‚ β”‚ β”œβ”€β”€ πŸ“„ benchmark.php # ⏳ Benchmarking logic
β”‚ β”‚ β”œβ”€β”€ πŸ“„ deleteFile.php # πŸ—‘οΈ File deletion utilities
β”‚ β”‚ β”œβ”€β”€ πŸ“„ env.php # 🌎 Load environment variables
β”‚ β”‚ β”œβ”€β”€ πŸ“„ format.php # 🎨 Format output, print, and timing
β”‚ β”‚ β”œβ”€β”€ πŸ“„ help.php # ℹ️ Show CLI help text
β”‚ β”‚ β”œβ”€β”€ πŸ“„ logger.php # πŸ“ Write output to file
β”‚ β”‚ β”œβ”€β”€ πŸ“„ utils.php # πŸ› οΈ General helper functions
β”‚
│── πŸ“‚ tests/ # βœ… Unit tests
β”‚
│── πŸ“„ .env.example # πŸ”§ Example env file
│── πŸ“„ .gitignore # 🚫 Ignore unnecessary files
│── πŸ“„ config.php # βš™οΈ Configuration settings
│── πŸ“„ index.php # πŸš€ Main entry point
│── πŸ“– README.md # πŸ“š Documentation
```

### πŸ“Œ Key Files & Directories
- [**benchmarks/**](./benchmarks) - Contains different algorithm benchmarks.
- [**src/**](./src) - Core logic including benchmark execution and formatting.
- [**results/**](./results) - Stores benchmarking logs.
- [**tests/**](./tests) - Unit tests to validate accuracy.
- [**index.php**](./index.php) - Main script to execute benchmarks.

## πŸš€ Getting Started

#### 1️⃣ Clone the Repository
```bash
git clone https://github.com/tqt97/benchmark-project.git
cd benchmark-project
```

#### 2️⃣ πŸš€ Usage

>πŸ“’ **Guide:** How to implement benchmark. [how_to_implement_benchmark.md](./how_to_implement_benchmark.md)

##### **Run a benchmark**
```sh
php index.php benchmark benchmarks/array/1_sum.php
```
- 🏁 This will execute the benchmark script `benchmarks/array/1_sum.php`.
- If no action (`benchmark`, `list`, `clean`) is specified, it defaults to **benchmark**.

##### **Generate algorithm list**
```sh
php index.php list
```
- πŸ“œ This will create a **`algorithms.md`** file listing all available benchmark scripts with clickable links.

##### **Clean the results directory**
```sh
php index.php clean
```
- 🧹 This will delete all files inside the **results** directory after confirmation.

##### **Auto-detect benchmark mode**
```sh
php index.php benchmarks/array/1_sum.php
```
- πŸ€– Since no action (`benchmark`, `list`, `clean`) is provided, the system assumes **benchmark mode** automatically.

---

## 🀝 Contributing

We appreciate contributions to improve this project! If you’d like to contribute, follow these steps:

### 1️⃣ Fork the repository
Click the **Fork** button on GitHub to create your own copy of the project.

### 2️⃣ Clone the project
```sh
git clone https://github.com/your-username/benchmark-project.git
cd benchmark-project
```

### 3️⃣ Create a new branch
```sh
git checkout -b feature/new-feature
```

### 4️⃣ Make changes and commit
- Improve benchmarks, optimize algorithms, or add new features.
- Follow the existing code structure for consistency.
- **Add a new algorithm** inside the `benchmarks/` directory under the appropriate data structure folder.
```
benchmarks/
β”œβ”€β”€ array/
β”‚ β”œβ”€β”€ 1.sum.php # Example of array algorithm
β”œβ”€β”€ stack/
β”‚ β”œβ”€β”€ stack_push.php # Example of stack algorithm
```
```sh
git commit -m "✨ Added new sorting algorithm"
```

### 5️⃣ Push and create a Pull Request
```sh
git push origin feature/new-feature
```
- Open a **Pull Request (PR)** from your fork to the main repository.
- Add a description of your changes.

πŸ“’ **Note:** All new algorithms must be added inside the `benchmarks/` directory!

---

## πŸ“œ Contribution Guidelines
βœ” Keep code **clean** and **well-documented**.
βœ” Follow **PSR-12** PHP coding standards.
βœ” Run tests before submitting PRs.

> πŸ’‘ **Have a suggestion or found a bug?** Open an **Issue** on GitHub! πŸš€

Enjoy coding! πŸš€