Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 548eb9d

Browse filesBrowse files
authored
Merge pull request #12 from codeasarjun/memory-management
memory profiling/management has been added
2 parents 3872ad1 + 7bb5c8d commit 548eb9d
Copy full SHA for 548eb9d

File tree

Expand file treeCollapse file tree

8 files changed

+120
-0
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+120
-0
lines changed

‎19-configuration-management/.env

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DATABASE_HOST=localhost
2+
DATABASE_PORT=5432

‎19-configuration-management/README.md

Copy file name to clipboardExpand all lines: 19-configuration-management/README.md
Whitespace-only changes.
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[Database]
2+
host = 127.0.0.1
3+
port = 3306
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import configparser
2+
3+
# create a ConfigParser object
4+
config = configparser.ConfigParser()
5+
6+
# read configuration from a file
7+
config.read('config.ini')
8+
9+
# access a value
10+
database_host = config.get('Database', 'host')
11+
database_port = config.getint('Database', 'port')
12+
13+
print(f"Database Host: {database_host}")
14+
print(f"Database Port: {database_port}")
15+
16+
# write to a configuration file
17+
config['Database']['host'] = 'localhost'
18+
config['Database']['port'] = '5432'
19+
20+
with open('config.ini', 'w') as configfile:
21+
config.write(configfile)
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from dotenv import load_dotenv
2+
import os
3+
4+
# load environment variables from .env file
5+
load_dotenv()
6+
7+
# access environment variables
8+
database_host = os.getenv('DATABASE_HOST')
9+
database_port = os.getenv('DATABASE_PORT')
10+
11+
print(f"Database Host: {database_host}")
12+
print(f"Database Port: {database_port}")

‎20-memory-performance/README.md

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
## Overview
3+
4+
This repo includes scripts for profiling memory usage and performance in Python.
5+
6+
## What is Memory Profiling?
7+
8+
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.
9+
10+
### Why Use Memory Profiling?
11+
12+
1. **Optimize Memory Usage**: Memory profiling helps identify memory leaks and inefficient memory usage, which can lead to improved performance and reduced memory consumption.
13+
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.
14+
3. **Resource Management**: For applications running on systems with limited resources, efficient memory usage is critical to maintaining stability and performance.
15+
16+
17+
## What is Performance Profiling?
18+
19+
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.
20+
21+
### Why Use Performance Profiling?
22+
23+
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.
24+
2. **Improve Efficiency**: By understanding execution times, you can make informed decisions about code improvements to enhance overall performance.
25+
3. **Benchmarking**: Profiling provides benchmarks that help you measure the impact of optimizations and compare different approaches.
26+
27+
28+
### Files
29+
30+
- `memory_profiler_example.py`: Demonstrates how to use the `memory_profiler` library to track memory usage of a Python function.
31+
- `performance_profiling.py`: Uses the `cProfile` module to profile the execution time of a Python function.
32+
33+
34+
### Setup
35+
36+
To use these profiling scripts, make sure you have the necessary libraries installed:
37+
38+
```bash
39+
pip install memory_profiler
40+
```
41+
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from memory_profiler import profile
2+
import time
3+
4+
@profile
5+
def compute_sum(n):
6+
total = 0
7+
for i in range(n):
8+
total += i
9+
time.sleep(0.01) # some delay
10+
return total
11+
12+
if __name__ == "__main__":
13+
result = compute_sum(1000)
14+
print(f"Sum is {result}")
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import cProfile
2+
import pstats
3+
import io
4+
5+
def compute_sum(n):
6+
total = 0
7+
for i in range(n):
8+
total += i
9+
return total
10+
11+
def main():
12+
result = compute_sum(100000)
13+
print(f"Sum is {result}")
14+
15+
if __name__ == "__main__":
16+
pr = cProfile.Profile()
17+
pr.enable()
18+
19+
main()
20+
21+
pr.disable()
22+
s = io.StringIO()
23+
sortby = 'cumulative'
24+
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
25+
ps.print_stats()
26+
27+
print(s.getvalue())

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.