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
- Host: GitHub
- URL: https://github.com/tqt97/dsa_benchmark_with_php
- Owner: tqt97
- Created: 2025-02-16T09:56:00.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-02-16T10:33:59.000Z (8 months ago)
- Last Synced: 2025-02-16T11:20:08.164Z (8 months ago)
- Topics: algorithms, benchmark, complexity, data-structures, php
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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
[](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! π