Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Efficiency of code execution

  é™ˆçš“        2012-07-13 10:59:21       4,331        0    

If you want to optimize your program codes, you need to find their Hotspot, i.e, the codes which are executed most frequently. If you can optimize this portion of codes a bit, you may gain much improvement of your code efficiency. Here I give you three examples about efficiency of code execution.

1. PHP's Getter and Setter (From Reddit)

This example a quite simple, you can skip it if you want.

Consider the code below, we can find it's slower when we use Getter/Setter method to read a member variable rather than read/write member variables directly.

<?php
    //dog_naive.php
 
    class dog {
        public $name = "";
        public function setName($name) {
            $this-&gt;name = $name;
        }
        public function getName() {
            return $this-&gt;name;
        }
    }
 
    $rover = new dog();
    //Using Getter/Setter
    for ($x=0; $x<10; $x++) {
        $t = microtime(true);
        for ($i=0; $i<1000000; $i++) {
            $rover->setName("rover");
            $n = $rover->getName();
        }
        echo microtime(true) - $t;
        echo "\n";
    }
    //Directly read/write
    for ($x=0; $x<10; $x++) {
        $t = microtime(true);
        for($i=0; $i<1000000; $i++) {
            $rover->name = "rover";
            $n = $rover->name;
        }
        echo microtime(true) - $t;
        echo "\n";
    }
?>

This is not weird. Because there is cost in calling functions. When calling functions, we need to push and pop variables to and from the stack and pass values, sometimes even interrupts may happen. Actually, all programming languages have similar behavior. This is also why C++ introduces inline function.

You may think the code below(Magic function) may be better. In fact it's worse.

class dog {
    private $_name = "";
    function __set($property,$value) {
        if($property == 'name') $this->_name = $value;
    }
    function __get($property) {
        if($property == 'name') return $this->_name;
    }
}

Efficiency of dynamic languages is always a problem. If you want to improve PHP's efficiency, you may need Facebook's HipHop to compile PHP code to C++ code.

2. Why does Python code run faster in a function? (From Stackoverflow)

Consider the for loop below, one is in a function, one is in global scope.

If put the for loop in a function, the time taken to execute them is 1.8s

def main():
    for i in xrange(10**8):
        pass
main()

If put the for loop in global scope, the time taken to execute them is 4.5s

for i in xrange(10**8):
    pass

Inside a function, the bytecode is

  2           0 SETUP_LOOP              20 (to 23)
              3 LOAD_GLOBAL              0 (xrange)
              6 LOAD_CONST               3 (100000000)
              9 CALL_FUNCTION            1
             12 GET_ITER           
        >>   13 FOR_ITER                 6 (to 22)
             16 STORE_FAST               0 (i)

  3          19 JUMP_ABSOLUTE           13
        >>   22 POP_BLOCK          
        >>   23 LOAD_CONST               0 (None)
             26 RETURN_VALUE       
At top level, the bytecode is

  1           0 SETUP_LOOP              20 (to 23)
              3 LOAD_NAME                0 (xrange)
              6 LOAD_CONST               3 (100000000)
              9 CALL_FUNCTION            1
             12 GET_ITER           
        >>   13 FOR_ITER                 6 (to 22)
             16 STORE_NAME               1 (i)

  2          19 JUMP_ABSOLUTE           13
        >>   22 POP_BLOCK          
        >>   23 LOAD_CONST               2 (None)
             26 RETURN_VALUE       
The difference is that STORE_FAST is faster (!) than STORE_NAME. This is because in a function, i is a local but at toplevel it is a global. Local variables are stored in an array, while global variables are stored in a dictionary which is slower to search.

To examine bytecode, use the dis module. I was able to disassemble the function directly, but to disassemble the toplevel code I had to use the compile builtin.

The above explanation is from ecatmur.

3. Why is processing a sorted array faster than an unsorted array? (From Stackoverflow)

For following C/C++ code.

for (unsigned i = 0; i < 100000; ++i) {
   // primary loop
    for (unsigned j = 0; j < arraySize; ++j) {
        if (data[j] >= 128)
            sum += data[j];
    }
}

If the data array is sorted, the execution time is 1.93s, if not, the execution time is 11.54s. No matter in C, C++ or Java or other languages, the result is more or less the same.

The reason for the difference in execution time is because of branch prediction. Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication.

You are the operator of a junction and you hear a train coming. You have no idea which way it will go. You stop the train to ask the captain which direction he wants. And then you set the switch appropriately.

Trains are heavy and have a lot of momentum. So they take forever to start up and slow down.

Is there a better way? You guess which direction the train will go!
  • If you guessed right, it continues on.
  • If you guessed wrong, the captain will stop, back up, and yell at you to flip the switch. Then it can restart down the other path.
If you guess right every time, the train will never have to stop.
If you guess wrong too often, the train will spend a lot of time stopping, backing up, and restarting.

Consider an if-statement: At the processor level, it is a branch instruction:



You are a processor and you see a branch. You have no idea which way it will go. What do you do? You halt execution and wait until the previous instructions are complete. Then you continue down the correct path.

Modern processors are complicated and have long pipelines. So they take forever to "warm up" and "slow down".

Is there a better way? You guess which direction the branch will go!

  • If you guessed right, you continue executing.
  • If you guessed wrong, you need to flush the pipeline and roll back to the branch. Then you can restart down the other path.
If you guess right every time, the execution will never have to stop.
If you guess wrong too often, you spend a lot of time stalling, rolling back, and restarting.

This is branch prediction. I admit it's not the best analogy since the train could just signal the direction with a flag. But in computers, the processor doesn't know which direction a branch will go until the last moment.

So how would you strategically guess to minimize the number of times that the train must back up and go down the other path? You look at the past history! If the train goes left 99% of the time, then you guess left. If it alternates, then you alternate your guesses. If it goes one way every 3 times, you guess the same...

In other words, you try to identify a pattern and follow it. This is more or less how branch predictors work.

Most applications have well-behaved branches. So modern branch predictors will typically achieve >90% hit rates. But when faced with unpredictable branches with no recognizable patterns, branch predictors are virtually useless.

Further Reading: http://en.wikipedia.org/wiki/Branch_predictor

As hinted from above, the culprit is this if-statement:

if (data[c] >= 128)
    sum += data[c];
Notice that the data is evenly distributed between 0 and 255. When the data is sorted, roughly the first half of the iterations will not enter the if-statement. After that, they will all enter the if-statement.

This is very friendly to the branch predictor since the branch consecutively goes the same direction many times. Even a simple saturating counter will correctly predict the branch except for the few iterations after it switches direction.

Quick visualization:

T = branch taken
N = branch not taken

data[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ...
branch = N  N  N  N  N  ...   N    N    T    T    T  ...   T    T    T  ...

       = NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT  (easy to predict)
However, when the data is completely random, the branch predictor is rendered useless because it can't predict random data. Thus there will probably be around 50% misprediction. (no better than random guessing)

data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118,  14, 150, 177, 182, 133, ...
branch =   T,   T,   N,   T,   T,   T,   T,  N,   T,   N,   N,   T,   T,   T,   N  ...

       = TTNTTTTNTNNTTTN ...   (completely random - hard to predict)

So what can be done?

If the compiler isn't able to optimize the branch into a conditional move, you can try some hacks if you are willing to sacrifice readability for performance.

Replace:

if (data[c] >= 128)
    sum += data[c];
with:

int t = (data[c] - 128) >> 31;
sum += ~t & data[c];
This eliminates the branch and replaces it with some bitwise operations.

(Note that this hack is not strictly equivalent to the original if-statement. But in this case, it's valid for all the input values of data[].)

Benchmarks: Core i7 920 @ 3.5 GHz

C++ - Visual Studio 2010 - x64 Release

//  Branch - Random
seconds = 11.777

//  Branch - Sorted
seconds = 2.352

//  Branchless - Random
seconds = 2.564

//  Branchless - Sorted
seconds = 2.587
Java - Netbeans 7.1.1 JDK 7 - x64

//  Branch - Random
seconds = 10.93293813

//  Branch - Sorted
seconds = 5.643797077

//  Branchless - Random
seconds = 3.113581453

//  Branchless - Sorted
seconds = 3.186068823
Observations:

  • With the Branch: There is a huge difference between the sorted and unsorted data.
  • With the Hack: There is no difference between sorted and unsorted data.
  • In the C++ case, the hack is actually a tad slower than with the branch when the data is sorted.
A general rule of thumb is to avoid data-dependent branching in critical loops. (such as in this example)

The above explanation is from Mysticial.

Lastly, I recommend one website to you Google Speed. It provides you some tutorials on how to Make the Web Faster.

Author : 陈皓 Source : http://coolshell.cn/articles/7886.html#more-7886

CODE  ANALYSIS  TRICK  EFFICIENCY 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.