Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 19-configuration-management/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_HOST=localhost
DATABASE_PORT=5432
Empty file.
3 changes: 3 additions & 0 deletions 19-configuration-management/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Database]
host = 127.0.0.1
port = 3306
21 changes: 21 additions & 0 deletions 19-configuration-management/configparser_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import configparser

# create a ConfigParser object
config = configparser.ConfigParser()

# read configuration from a file
config.read('config.ini')

# access a value
database_host = config.get('Database', 'host')
database_port = config.getint('Database', 'port')

print(f"Database Host: {database_host}")
print(f"Database Port: {database_port}")

# write to a configuration file
config['Database']['host'] = 'localhost'
config['Database']['port'] = '5432'

with open('config.ini', 'w') as configfile:
config.write(configfile)
12 changes: 12 additions & 0 deletions 19-configuration-management/dotenv_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from dotenv import load_dotenv
import os

# load environment variables from .env file
load_dotenv()

# access environment variables
database_host = os.getenv('DATABASE_HOST')
database_port = os.getenv('DATABASE_PORT')

print(f"Database Host: {database_host}")
print(f"Database Port: {database_port}")
41 changes: 41 additions & 0 deletions 20-memory-performance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

## Overview

This repo includes scripts for profiling memory usage and performance in Python.

## What is Memory Profiling?

Memory profiling is the process of analyzing a program to understand its memory consumption patterns. This involves tracking how much memory your application uses and identifying memory-intensive parts of your code.

### Why Use Memory Profiling?

1. **Optimize Memory Usage**: Memory profiling helps identify memory leaks and inefficient memory usage, which can lead to improved performance and reduced memory consumption.
2. **Enhance Performance**: By understanding which parts of your code are using the most memory, you can refactor or optimize those areas to make your application more efficient.
3. **Resource Management**: For applications running on systems with limited resources, efficient memory usage is critical to maintaining stability and performance.


## What is Performance Profiling?

Performance profiling is the process of analyzing a program to determine its execution time and identify bottlenecks. This involves measuring how long different parts of your code take to execute.

### Why Use Performance Profiling?

1. **Identify Bottlenecks**: Performance profiling helps pinpoint parts of your code that are slow or inefficient, allowing you to focus optimization efforts where they are needed most.
2. **Improve Efficiency**: By understanding execution times, you can make informed decisions about code improvements to enhance overall performance.
3. **Benchmarking**: Profiling provides benchmarks that help you measure the impact of optimizations and compare different approaches.


### Files

- `memory_profiler_example.py`: Demonstrates how to use the `memory_profiler` library to track memory usage of a Python function.
- `performance_profiling.py`: Uses the `cProfile` module to profile the execution time of a Python function.


### Setup

To use these profiling scripts, make sure you have the necessary libraries installed:

```bash
pip install memory_profiler
```

14 changes: 14 additions & 0 deletions 20-memory-performance/memory_profiler_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from memory_profiler import profile
import time

@profile
def compute_sum(n):
total = 0
for i in range(n):
total += i
time.sleep(0.01) # some delay
return total

if __name__ == "__main__":
result = compute_sum(1000)
print(f"Sum is {result}")
27 changes: 27 additions & 0 deletions 20-memory-performance/performance_profiling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import cProfile
import pstats
import io

def compute_sum(n):
total = 0
for i in range(n):
total += i
return total

def main():
result = compute_sum(100000)
print(f"Sum is {result}")

if __name__ == "__main__":
pr = cProfile.Profile()
pr.enable()

main()

pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()

print(s.getvalue())