PDA

View Full Version : Code maintainability vs. speed



Kyohack
01-20-2013, 12:28 AM
Lately, I've been looking through the code of some old programs across our servers, to check for any potential optimizations that can be implemented. My coding style has changed significantly over the past year, especially after reading Microsoft's Manual of Style and Zend's Coding Style (http://framework.zend.com/manual/1.12/en/coding-standard.coding-style.html). One of the most difficult decisions I face when revising code, is whether code maintainability should be sacrificed for speed. For example, consider the following snippet of code:


function appendFooter($response)
{
$footer = base64_encode($response);
$footer = str_replace('/', '_', $footer);
$footer = str_replace('+', '-', $footer);
$salt = 'HZEdGCzcGGLvguqUEKQN';
$footer = sha1($salt . $footer . $salt);
return $response . $footer;
}


In its current state, the snippet of code is quite easy for the programmer to read and maintain but it does not perform as fast as it potentially could. When this function is run 10 million times within a loop, it takes around 38.1 seconds to compute. Take a look at the optimized version of the same code:


function appendFooter($response)
{
return $response . sha1('HZEdGCzcGGLvguqUEKQN' . str_replace('+', '-', str_replace('/', '_', base64_encode($response))) . 'HZEdGCzcGGLvguqUEKQN');
}


The logic of the function now only occupies a single line. When run 10 million times within a loop, the code now only takes 31.6 seconds to compute. Yes, that's around 7 seconds of computing time saved by the server. Just why is it faster? Well, here's what changed:

Less variables are declared. Instead of declaring $footer and $salt, they are used directly. A tiny amount of RAM is saved by the server (likely around one KB or so), from avoiding the usage of variables. That is a trivial amount of RAM for any server, but consider the server's RAM latency. Each time a variable is declared and uses RAM, the server is essentially spending some time waiting for that data to be stored in RAM. When the code is looped 10 million times, this RAM latency begins to add up. As an example, my dev system has a theoretical RAM bandwidth of 10 GB per second. Do the math, and that's 10 million KB. So assuming the usage of variables in the first snippet of code writes 1 KB of RAM and loops 10 million times, then the entire 10 GB of bandwidth is used up for 1 second.
Excessive redeclaration of variables is avoided. Every time a variable is redeclared, it uses additional RAM. In the first snippet of code, the $footer variable is redeclared 4 times and therefore requires additional time to compute.
Unnecessary declaration of variables is avoided. It can be debatable whether some variables are necessary at all. Consider the $salt variable, which is only used twice and is located on the very next line. When variables are used such few times or are easily located closely to one another, some experienced programmers may decide to directly use their values instead. Some inexperienced programmers may mistakenly declare variables solely for the purpose of allowing the code to be more easily understood. Instead, comments should be used for this purpose since they are ignored by the PHP compiler and don't impact code performance.


But obviously, the second snippet is more difficult to read and maintain since code statements are nested on a single line, as opposed to being on separate lines like in the first example. So here I am, faced with the decision of how much readability and maintainability should be sacrificed for speed. Both factors are critical. Code needs to be easily readable and understandable so that changes can be made quickly when maintenance inevitably needs to be performed. On the flip side, the servers that run our code receive quite a significant amount of traffic. Therefore, it is very important that every possible optimization is made, in order to reduce the amount resources used by the server, and also to speed up the overall response time.

ffworld
01-24-2015, 02:39 AM
A tiny amount of RAM is saved by the server (likely around one KB or so), from avoiding the usage of variables.



-------------------------------------
fifa 15 coins (http://www.fifachampion.com/) cheap fifa coins (http://www.fifachampion.com/)