Optimizing Code with AI
Tags: development, techinsight, ai, artificialintelligen
Published: 2024-05-03 09:41:48

---
title: Optimizing Code with AI
published: true
date: 2024-01-10 20:24:24 UTC
tags: Development,TechInsight,AI,artificialintelligen
canonical_url: https://mid.as/blog/optimizing-code-with-ai/
---

[![](https://mid.as/blog/wp-content/uploads/2024/01/perl_code.jpg)](https://mid.as/blog/wp-content/uploads/2024/01/perl_code.jpg)

Often in the [release notes](https://mid.as/changelog) for our MIDAS room scheduling software, you may see the entry “Code Optimization”.

## What is “Code Optimization”?

Code optimization is the process of refining our software code to make it execute more efficiently, consume fewer resources, or improve its overall performance. It involves strategically modifying code whilst at the same time ensuring the new code still produces the correct results.

### Key goals of code optimization:

- **Enhanced speed:**  Executing tasks more quickly
- **Reduced resource consumption:**  Using less memory, CPU cycles, or power
- **Improved scalability:**  Handling larger workloads effectively
- **Maintainability:**  Making code easier to understand and modify

Some common code optimization techniques involve:

- **Algorithm optimization:**  Choosing more efficient algorithms
- **Loop optimization:**  Reducing loop iterations or overhead
- **Memory optimization:**  Minimizing memory usage and allocations
- **Input/output optimization:**  Streamlining data reading and writing
- **Caching:**  Storing frequently used data for faster access
- **Compiler optimization:**  Leveraging compiler features for automatic optimization
- **Profiling:**  Identifying performance bottlenecks to focus optimization efforts

## Code Optimization in MIDAS

Over the years we’ve been developing MIDAS, all our code optimization work has been done manually.

This work has involved attempting to simplify and rewrite parts of the code to be more efficient.

## Code Optimizing with AI

In our latest release, [MIDAS v4.35](https://mid.as/changelog/4.35), for the very first time, a small section of code has been optimized with the assistance of [AI (or Artificial Intelligence)](https://mid.as/blog/what-ai-thinks-about-midas/).

We did this as an experiment to see whether AI could potentially be used to aid our development processes in the future.

We chose a small “subroutine” from our software and asked an AI if it could optimize it for us.

A “subroutine” is essentially a small block of code which can be re-used and “called” repeatedly during a program’s execution.

The subroutine within the MIDAS software code that we asked an AI if it could optimize for us was basically a function which converts dates to “epoch” time.

Epoch time is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT).

## How did the AI do?

Our original subroutine was 15 lines of code long. AI was able to optimize this down to just 9 lines of code.

However, the intial code that the AI generated for us did not just work “out of the box”. In fact, it didn’t work at all!

But using this AI generated code as a “template”, our team was able to modify the generated code so that it worked and produced the correct results.

Our team then extensively tested the new subroutine to ensure that it consistently produced the same expected output as the original subroutine.

Once we had a working subroutine that we were confident in, the next step was to “benchmark” the new routine against the old one. After all, there would be no point in using the new routine if there were no performance gains to be had, or indeed if the new code performed worse than the original.

To test this, we ran each subroutine 10,000,000 (10 million times), and analyzed the results:

> **Our Original Subroutine:**  
> 30 wallclock secs (30.28 usr + 0.00 sys = 30.28 CPU) @ 330229.18/s (n=10000000)  
>   
> **AI Optimized Subroutine:**  
> 27 wallclock secs (27.09 usr + 0.00 sys = 27.09 CPU) @ 369085.41/s (n=10000000)

To explain the above results, over 10 million iterations of each subroutine, the new AI-optimized subroutine was more efficient. It ran (executed) quicker, and used less processing (CPU) power to achieve the same results as the original subroutine.

On our test server, the optimized subroutine was able to run 369,085 times in the space of one second, compared with 330,229 times in the space of a second for the original subroutine.

Ok, so the original subroutine was pretty quick and efficient to begin with – but **with the assistance of AI, we were able to improve its efficiency by almost 12%**.

## Where do we go from here?

Firstly, don’t worry, AI is not about to take over the development of our MIDAS software!

MIDAS has been – and will remain – coded, developed, and maintained by humans.

But our experiment – on a very small part of our code – has shown that AI tools may be able to assist our human developers to write ever more efficient code.

The result of such carefully applied optimizations is that our software can in the future potentially run faster, use resources more efficiently, and provide a better overall user experience.

The post [Optimizing Code with AI](https://mid.as/blog/optimizing-code-with-ai/) appeared first on [MIDAS - Room Booking System | Blog](https://mid.as/blog).