As a solo developer, I often find myself seeking ways to optimize my code without the luxury of pair programming. That’s where ChatGPT comes in handy. In this blog post, I’ll share my experience of using ChatGPT to improve the performance of my related posts generator.
Getting Started with ChatGPT
To begin, I feed all the code that needs optimization into ChatGPT and ask if there’s any way for it to be improved. It generally provides me with a list of suggestions, some of which may be duplicates (the same change described in different ways). I’ve tried asking ChatGPT to apply the changes for me but more often than not this breaks things. It applies all the changes and they all conflict and everything goes wrong. So while it’s tempting to rely on ChatGPT entirely, I’ve learned that manually checking then implementing each suggestion is quicker and safer.
Profiling for Performance
In order to identify the areas needing improvement, I perform profiling on my codebase. This allows me to pinpoint the slowest parts of the program. Although ChatGPT can guess what might be causing performance issues, it isn’t aware of the actual bottlenecks within the code. Often optimisations it offers make little improvement to the overall performance, so it’s important to work out where the bottleneck is and focus on that.
Tackling Slow Functions
The specific task at hand was optimizing related posts generation for my websites. On smaller sites, this process was fast, but larger sites experienced significant delays. For example, Binary Moon (over 400 posts) used to take around 20 seconds to build.
After profiling, I discovered that the function responsible for calculating Jacard Similarity 1 between two arrays was causing major slowdowns. My original implementation was concise but inefficient. However, by rewriting the function as a longer function with less array manipulation involved, I was able to reduce processing time from 20 seconds to just 5 seconds - a substantial improvement.
While waiting times during deployment were not critical at the moment, this slowness impacts my approach of iterating quickly and will only get worse as the size of my websites increase.
Collaborating with ChatGPT
It’s important to note that while ChatGPT lacks true understanding of coding concepts and best practices, collaborating with it effectively has helped me make more optimal decisions in terms of code efficiency and performance.
By working through ChatGPT’s suggestions, carefully selecting the ones I deemed valuable, and implementing them manually, I have been able to fine-tune my codebase for better performance.
Even as a solo developer, optimizing code doesn’t have to be a daunting task. Thanks to the assistance of ChatGPT and careful collaboration, I’ve been able to improve the efficiency of my related posts generator. While it may not be a perfect solution, it certainly provides valuable insights that can lead to more optimal code.
Jacard Similarity measures the similarity between two sets (arrays) by looking at the ratio of shared elements to the total unique elements in both sets. It’s a way to figure out how much two sets have in common. ↩