Does complex code interfere with application performance?

Asked

Viewed 727 times

3

I have a somewhat legacy C# web application, which works on my client, but it’s been about 3 years.

At the time, I programmed as I learned in college and did not know the concept of clean code. It is not so big, but taking in complexity, I believe it is still quite complex, comparing to the codes I write today. And I noticed that this application often takes up a lot of space on my server’s ram, ranging between 240mb, and 700mb. Believing that applications of the same size might not occupy more than 128mb.

I wonder if possible complexities in my code, like a loop inside another, if’s inside other if’s, and things like that, can be responsible for the performance of my application?

3 answers

5


Complexity

Contrary to what one might think at first, a good program will usually be less complex than a bad program. Many people think that the best programmer is the one who writes the most difficult and counter-intuitive code.

In fact, the good programmer is the one who solves complex problems as simply as possible. This is because when the developer is more experienced, he learns to solve the problem directly, without unnecessary "loops".

Another maturity factor of a developer is the question of first solving the problem, then implementing the solution. The younger ones usually go out programming without really knowing where they’re going. The code ends up "working", but with gambiarras and completely unnecessary parts.

Reading suggestion: Good programmers write complex code?

Of course some algorithms are inherently complex, but the truth is that in general we never get an optimal solution

There is a whole theory about the complexity of algorithms, but what in general affects even the complexity is the amount of loops and comparisons performed.

Memory vs. Performance

Memory and performance are usually not proportional.

A good program can use more memory to cache and fine optimizations, a more elaborate structure of classes and more. A bad program can do all operations on disk or directly in the database and contain fewer classes, but without separation of responsibilities.

Therefore, I think it is better to analyze these items separately...

Memoriam

Except in the cases I mentioned, where we purposely use more memory, a well-made program will occupy less memory when the developer correctly uses the data structures and language Apis.

In languages they have Garbage Collector it is essential to pay attention to memory Leaks in static attributes, such as maps that never have their items removed.

Another problem is reading too much data in memory without need. Some people read database and entire file in memory to then process the information, and you could do this record by record or line by line.

Performance

Performance can also be greatly affected by how a system is implemented.

This can start with how the database is modeled, how the system reads and writes data, and implemented algorithms.

Reading and writing in a database should be carefully thought out, avoiding select * and make updates mass when possible instead of updating records one by one.

For files it is interesting to use streams for reading and writing, unlike the common practice of always loading everything to a String in memory.

Performance, memory and complexity

Decreasing the complexity of a program can help decrease memory usage and improve performance.

The amount of loops and comparisons can be reduced using appropriate data structures. For example, in many situations we can use a *hash* based map to retrieve key-based elements instead of using indexOf in lists and vectors. In the best case, a map allows retrieval of an item without iterations, in a single step, while in the lists it may be necessary to scan all elements to find an object inside.

You can also save memory by avoiding the use of unnecessary variables and data structures. Or even avoid generating values in memory unnecessarily, using StringBuffers or equivalents instead of concatenation of strings, for example.

Readable code and optimizations

In general, a programmer tries to write simple and readable code, after all maintenance is a very important factor in most systems.

However, in specific cases, it is possible that it "breaks" certain rules by prioritizing some other quality issue: performance, availability, efficient use of memory or some other resource.

An example of this is in "real time" systems, where it is necessary to extract the maximum performance at the cost of having a code very difficult to read.

Or when it is decided to use UDP socket communication with a proprietary protocol instead of a REST web service due to data volume.

Considerations

Anyway, all this was to say that, yes, complex code usually impairs performance and memory usage.

I tried on the above topics to consider some fairly generic points, but which are relevant to the discussion.

  • 1

    Guy excellent explanation, at first I wonder only if the complexity of the code could affect the performance and use of ram server memory, you went further and gave an excellent response. I have been using clean code practice, always reducing the complexity of a year here, but I have this project already 3 years, which was created when I did not know about code complexity. Thank you.

3

for sure! an algorithm of the type:

for (i, i<n, i++){
   for (j, j<n, j++){
      for (k, k<n, k++){
          exec algo;
      }
      exec algo;
   }
   exec algo;
}

will have a complexity n 3

While an algorithm of the same type, only recursive, will have a 3*n complexity

The intention is always to optimize the code so that the machine suffers as little as possible

2

Loop inside the other can be a problem, it depends a lot on what you did and how you did it! Try to drill in a little better. Post small "important" parts of the code for better community analysis.

What @Marcelo Bonifazio said is partially correct, but depends on the situation, recursion does not compete with inline.

  • I understood @Trplz0, however my doubt was a bit wide, as it is an old application and comparing with my recent applications I know that the level of code is very complex, many methods doing many things, it was not simple. Today I have another view and as this application is occupying a somewhat excessive memory on the server, I am considering rewriting part of her code. But before doing so I wondered if this could be one of the main reasons she used so much memory.

  • 1

    An easy way to check RAM consumption is to analyze each variable you create, because they consume RAM. If you hit your eye on the code and notice that you duplicate variables, instance objects without need it will consume an amount of RAM for that request and when you multiply by the multiple requests you have server consumption, another point is to analyze the methods created and try to lessen their impact. This comes from long-term code maintenance. Unit testing is the best tool for analyzing these methods! I will continue on the next comment..

  • 1

    Also try to analyze the return of database data and order of these returns with the user input. I see this a lot, return some data for later use the validation of the user but if the validation fails you alert the user and does not use these data previously loaded, that is, just went to the database, consumed memory with the data for nothing. If you used any framework to develop, you should do a clean test of it, just a hello world and see the consumption. Based on this you will make sure that it is your code that is costing X and the framework costs Y.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.