Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle who care about creating, delivering, and maintaining software responsibly. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I was reading Wikipedia and came across the 90 / 10 rule that states that 90% of a program execution time is spent in executing 10% of the code
(see the second paragraph here)

I really don't understand this. What exactly does this mean? How can 90% of the execution time be spent only executing 10% of the code? What about the other 90% of the code then? How can they be executed in just 10% of the time?

Could someone please explain this to me?

share
13  
Some parts of the code may be executed more often than other parts. That is what loops are for, after all. In practice, almost always some parts are executed way more often than others. – Kilian Foth 5 hours ago
    
1  
As already commented, the point is how often (relatively) various paths through the code are traversed. If 10% of the code gets invoked over and over again, while 90% is just sitting there in case it's needed, then the 90-10 rule makes sense. – Walter Mitty 3 hours ago
    
So your title indicates, like, upspeak? – Carsten S 3 hours ago
19  
Wait until you hear the 90/10 rule for software project duration: “90% of the project will take up the first 90% of the allotted time; the last 10% of the project will take up the other 90% of the allotted time”. – Paul D. Waite 2 hours ago

There are two basic principles in play here:

  • Some code is executed much more often than other code. For example, some error handling code might never be used. Some code will be executed only when you start your program. Other code will be executed over and over while your program runs.
  • Some code takes much longer to run than other code. For example, a single line that runs a query on a database, or pulls a file from the internet will probably take longer than millions of mathematical operations.

The 90/10 rule isn't literally true. It varies by program (and I doubt there is any basis to the specific numbers 90 and 10 at all; someone probably pulled them out of thin air). But the point is, if you need your program to run faster, probably only a small number of lines is significant to making that happen. Identifying the slow parts of your software is often the biggest part of optimisation.

This is an important insight, and it means that decisions that seem counterintuitive to a new developer can often be correct. For example:

  • There is lots of code that it is not worth your time to make "better", even if it is doing things in a dumb, simplistic way. Could you write a more efficient search algorithm for application XYZ? Yes, but actually a simple comparison of every value takes a trivial amount of time, even though there are thousands of values. So it's just not worth it. It can be tough for new developers to avoid unnecessary optimisation, because in their degree program so much time was spent on writing the "correct" (meaning most efficient) algorithm. But in the real world, the correct algorithm is any one that works and runs fast enough.
  • Changes that make your code much longer and more complex may still be a performance win. For example, in application FOO it may be worth adding hundreds of lines of new logic, just to avoid a single database call.
share|improve this answer
2  
Of particular note, with things like sorting functions, it's much faster (in dev time) and easier to make a dumb simple algo do the right thing in all cases than to get an elegant algo fully functional and bugless. (Tho the only reasons to write a sort algo outside of acadamea are if you're building a library or working on a platform without one…) – StarWeaver 3 hours ago
    
I think you need to add the link to shouldioptimize.com :) – Ivan Kolmychek 1 hour ago

This isn't a law of nature, but a rule of thumb born out by wide experience. It is also known as the 80/20 rule, and is only ever a rough approximation.

Loops, Branches and other flow control.

Each place that has an if, you will have one branch that is taken more often than the other branch. Thus more of the execution time is spent executing that part of the program, and not the other part.

Each place that has a loop that runs more than once, you have code that gets executed more than surrounding code. Thus more time is spent there.

As an example, consider:

def DoSomeWork():
    for i in range(1000000):
        DoWork(i)
    except WorkExeption:
        print("Oh No!")

Here the print("Oh No!") will only ever run a maximum of once, and often never, whereas the DoWork(i) will occur about a million times.

share|improve this answer
3  
Calling it the 80/20 rule can cause confusion with the Pareto principle, which applies more broadly than just to programming. Maybe 90 and 10 are just convenient numbers that don't have this overlap in meaning. – trichoplax 4 hours ago
8  
It's an instance of the Pareto principal. Both pairs of numbers are equally arbitrary – Caleth 4 hours ago
    
I think, you should add a paragraph on recursion: After all it's a way of looping that's all too easily missed because it's implicit looping. – cmaster 1 hour ago
    
There's a mathematical basis to the 80/20 split in the Pareto principle. They're not just some imaginary figures to represent "a lot" and "a little". – Moyli 3 mins ago

Loops.

I'm tempted to stop there! :-)

Consider this program

1. do_something

2. loop 10 times
3.    do_another_thing

4.    loop 5 times
5.        do_more_stuff

Line 1 is executed once whilst line 3 is executed 10 times. Looking at each line in turn

1 1   0.8%
2 10  8.3%
3 10  8.3%
4 50 41.3%
5 50 41.3%

Two lines account for 83% of the execution time (assuming all lines take about the same time to run. So 40% of the program takes >80%.

With larger more real world examples this rises so only a small amount of lines accounts for much of the run-time.

The 90/10 rule (or as it's sometimes put 80/20) is a "rule of thumb"- only approximately true.

See also Pareto Principle

share|improve this answer

As you asked about the execution time only, this example might be helpful:

int main() {
    sleep(90); // approximately 10% of the program.
    // other 90% of the program:
    sleep(1);
    sleep(1);
    sleep(1);
    sleep(1);
    sleep(1);
    sleep(1);
    sleep(1);
    sleep(1);
    sleep(1);
    sleep(1);
    return 0;
}

If to be a little more serious, it means that in real-life code you almost always call a heavy function in a loop (instead of sleep(90);), while the rest 10% of time you perform some single-pass computations.

Another example is error handling in some HA service. Any highly-available service is designed to work infinite amount of time under normal conditions. It operates normally 99% of time, but occasionally, in case of an error, it runs some error handling and recovery, which may be even more logically complex than the service itself.

share|improve this answer

Imagine a program like this:

print "H"
print "e"
print "l"
print "l"
print "o"
for i=0 to 1,000,000
    print "How long now?"
next
print "B"
print "y"
print "e"

Notice how there are 11 lines here where 3 out of the 11 are the for loop, where how much time is spent on this rather small piece of code? Quite a bit while the other 8 lines are merely printing a single character. Thus, beware that while some code may be short, that doesn't tell you how often is it executed and how much time it'll take.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.