Hi, I have created the following application for streamlit and I think I am losing a lot of performance because of the cache. When I run one of the pages in visual studio, I get this in the console repeatedly
If you go into the application, you can see how it is too long in running time. I have measured the code execution time in python and it is quite low.
If you’re experiencing performance issues with your Streamlit application, particularly related to the caching mechanism, here are a few suggestions to improve the performance:
Optimize your code: Analyze your code to identify any potential bottlenecks or areas that can be optimized. Look for inefficient loops, redundant calculations, or unnecessary data manipulation. By improving the efficiency of your code, you can reduce the overall execution time.
Granular caching: Check if you’re caching the entire page or function when only specific parts need to be cached. Streamlit’s caching mechanisms, such as
@st.cache decorator, allow you to cache specific functions or sections of code. By narrowing down the scope of what you cache, you can avoid unnecessary recalculations and improve performance.
Use memoization techniques: Streamlit’s caching mechanism is based on memoization. However, in some cases, you might benefit from implementing your own memoization technique tailored to your specific use case. This allows you to have more fine-grained control over the caching process and optimize it according to your needs.
Leverage background processes: If your Streamlit app involves long-running tasks or computations, consider offloading those tasks to background processes or worker threads. This can prevent the UI from becoming unresponsive and enhance overall performance. You can use Python libraries like
concurrent.futures to achieve parallel or asynchronous execution.
Profile your code: Use profiling tools, such as Python’s
cProfile module, to identify the exact areas of your code that are taking the most time to execute. This will help you pinpoint performance bottlenecks and focus your optimization efforts where they will have the most impact.
Consider asynchronous programming: If your Streamlit app involves making multiple external API calls or performing I/O operations, consider using asynchronous programming techniques. Libraries like
aiohttp allow you to perform multiple tasks concurrently, improving overall performance by minimizing waiting times.
By following these suggestions, you can identify and address performance bottlenecks in your Streamlit application. Remember to profile your code to target specific areas for optimization and test the performance improvements iteratively.