One Simple Trick to Reduce Your Memory Usage in Python

Table of Contents

Intro
List Comprehension
Generator Comprehension

Intro

Python is ultimate “getting things done” language, where you can soo easily write code and not worry too much about performance and memory. However once y…


This content originally appeared on DEV Community and was authored by Connor

Table of Contents

  1. Intro
  2. List Comprehension
  3. Generator Comprehension

Intro

Python is ultimate "getting things done" language, where you can soo easily write code and not worry too much about performance and memory. However once your program becomes large, large memory usage can significantly slow down your program. One easy way to reduce memory usage and speed up your programs is to switch your list comprehensions into generator comprehensions.

Lets explore this with a simple example program to sum up a range of numbers.

List Comprehension

Code:

import sys
my_large_list = [i for i in range(100000)]
print(sum(my_large_list))
print(f"My list is {sys.getsizeof(my_large_list)} bytes")

Output:

4999950000
My list is 824456 bytes

Generator Comprehension

My code is often filled with a lot of list comprehensions, where instead we could use generators instead. Generators operate like lists, except they are evaluated "lazily", so the values are grabbed when needed.

All we need to do is use curly braces on all of our list comprehensions.

Code:

import sys
my_large_generator_list = (i for i in range(100000))
print(sum(my_large_list))
print(f"My generator is {sys.getsizeof(my_large_generator_list)} bytes")

Output:

4999950000
My list is 112 bytes

As we can see both give the same result, however the generator only uses a fraction of the memory (112 bytes instead of 824456). When you have hundreds of lists floating in your code, switching them to generators is an easy way to save on memory and increase your program's speed :).


This content originally appeared on DEV Community and was authored by Connor


Print Share Comment Cite Upload Translate Updates
APA

Connor | Sciencx (2021-05-20T08:18:07+00:00) One Simple Trick to Reduce Your Memory Usage in Python. Retrieved from https://www.scien.cx/2021/05/20/one-simple-trick-to-reduce-your-memory-usage-in-python/

MLA
" » One Simple Trick to Reduce Your Memory Usage in Python." Connor | Sciencx - Thursday May 20, 2021, https://www.scien.cx/2021/05/20/one-simple-trick-to-reduce-your-memory-usage-in-python/
HARVARD
Connor | Sciencx Thursday May 20, 2021 » One Simple Trick to Reduce Your Memory Usage in Python., viewed ,<https://www.scien.cx/2021/05/20/one-simple-trick-to-reduce-your-memory-usage-in-python/>
VANCOUVER
Connor | Sciencx - » One Simple Trick to Reduce Your Memory Usage in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/20/one-simple-trick-to-reduce-your-memory-usage-in-python/
CHICAGO
" » One Simple Trick to Reduce Your Memory Usage in Python." Connor | Sciencx - Accessed . https://www.scien.cx/2021/05/20/one-simple-trick-to-reduce-your-memory-usage-in-python/
IEEE
" » One Simple Trick to Reduce Your Memory Usage in Python." Connor | Sciencx [Online]. Available: https://www.scien.cx/2021/05/20/one-simple-trick-to-reduce-your-memory-usage-in-python/. [Accessed: ]
rf:citation
» One Simple Trick to Reduce Your Memory Usage in Python | Connor | Sciencx | https://www.scien.cx/2021/05/20/one-simple-trick-to-reduce-your-memory-usage-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.