Macro Optimization Guide

The guide I wish I had when starting out

Optimization is also important

What You'll Learn

  • Reducing CPU & RAM usage
  • Safe settings to start with
  • Troubleshooting performance issues

Reducing CPU & RAM usage

CPU and RAM usage can be a concern when running macros, especially on lower-end computers. When creating macros, you must keep in mind how your code may impact the performance of the computer running it. No matter how powerful your computer is, there are always ways to optimize your macros to use less CPU and RAM.

The most important part of optimization is to avoid unnecessary loops and calculations since those can easily cause high CPU and RAM usage. If you have a macro that just has to have a loop, make sure not to overload it with too many calculations or actions that could easily be done once at the beginning of the macro. For example, in my macros I have this code at the start of every macro:

The image above shows that I used 2 variables to store the width and height of the screen, then I compared the found width and height to the variables associated with the screen size. This way I can easily assign variables based on the screen resolution without having to create multiple macros for each resolution. This is also a great way of optimizing your code since you check the screen resolution only once at the start of the macro, instead of checking it every time you need to use it, which would cause unnecessary CPU usage.

Another way to optimize your macros is to prevent unnecessary checks and actions. Try imagining a macro that checks if a pixel on the screen has changed color every 10 milliseconds; now imagine multiple of those checks in the macro... Since Python isn't optimized for speed, this would cause a lot of CPU usage, and the delay wouldn't be ideal for a macro. An easy fix for this would be to use the time library and add a delay to the checks using time.sleep(time); this way you can reduce the CPU usage by a lot since the script won't be checking the pixel every 10 milliseconds but rather every 100 milliseconds or more, depending on how often you need to check the pixel. So by adding a small delay of 0.1 seconds, your macro is already much more optimized than without the delay.

Safe settings to start with

The word "safe" purely depends on what you consider safe. For some it's a strong password protecting their account, while for others it's a rectangular steel safe, standing 42 inches tall, 24 inches wide, and 20 inches deep, weighing about 450 pounds, with a matte dark graphite gray powder-coated finish resistant to scratches and moisture. Its door is 4 inches thick, reinforced with solid steel plating, and filled with a concrete composite fireproof barrier. BUT in our case it's preventing yourself from locking yourself out of your own PC or having to turn off your PC just to stop a macro.

The best practice is to always add a way to forcefully stop your code in case you need to pause it or you've done the test you needed to do and now you want to fix the issue or improve it in the code. While I myself had such issues once or twice, I could see how a beginner could also easily make them. For example, moving your mouse to specific coordinates and clicking there multiple times per second in a "while True" loop and forgetting to add a break statement at any point would cause the user to have to think outside the box and find a solution or just restart their PC using the power button.

Adding simple code that would allow you to exit your macro at any time is an absolute must-have when creating macros. You can add this simple code below to add an ON/OFF switch with forced exiting added to it too:

But adding it alone into your code wouldn't do much; you need to add it into a separate thread to make it work at any time and never be blocked by other processes. This way, even if you get stuck in an infinite loop, you will still be able to easily exit the macro. All my macros use the exact same way of pausing and force exiting, so you could look into it yourself and see how it's done in an actual example.

Troubleshooting performance issues

If you have some performance issues, my guess is as good as yours when it comes to complex and long macros, but the main performance eaters are redundant loops that perform tasks that use up a lot of resources or, even better, redundant loops that do a simple check hundreds of times per second. These 2 are the most common and the easiest performance issues to fix. Threads, on the other hand, are slightly more advanced but can also cause a lot of issues when handled incorrectly, like memory leaks, which could gobble up your RAM like no other app. Could you imagine Roblox using 2GB of RAM, your browser using 1GB of RAM, and a macro using up 13GB of RAM? That's a memory leak, and you better fix it now before you continue making any other improvements in your macros.

The simplest solution is often AI. It's a great tool for learning and improving when used correctly. I personally use AI to fix memory leaks or improve code I may have overlooked. There's no shame in using AI to help you code, whether you're a beginner or advanced, since it was created to support us, not replace us. If you describe your issue in detail, AI can quickly identify what might be causing a memory leak, sometimes faster than you can open Task Manager and watch your RAM vanish into thin air. Then you can ask it to apply the changes to the code so you can copy-paste it, or ask it what should be changed/replaced, and you yourself will apply the changes and learn how to fix such issues in the future or, even better, not create such issues in the first place.