Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comparison of Algorithms Objective: Understand how and why algorithms are compared. Imagine you own a company. You’re trying to buy software to process.

Similar presentations


Presentation on theme: "Comparison of Algorithms Objective: Understand how and why algorithms are compared. Imagine you own a company. You’re trying to buy software to process."— Presentation transcript:

1 Comparison of Algorithms Objective: Understand how and why algorithms are compared. Imagine you own a company. You’re trying to buy software to process customer orders You meet Mr. Smith and Mr. Jones. They both sell order processing software: Smith Jones

2 Is it worth the extra $$$? Smith’s software uses an algorithm that processes each order in 3 seconds. But takes 30 seconds to start up Jones’s algorithm also processes each order in 3 seconds and has no start up time but costs $1000 more

3 Probably Not! Especially if you process hundreds of orders at a time. In general, algorithm A is not considered superior to algorithm B if all it can boast that it takes a fixed amount less time. For example, 5 minutes faster, 8 fewer operations, etc.

4 Is Smith’s new algorithm worth it? Smith improves his algorithm so there is no start up time and it takes 2 seconds/order. But now his costs $1000 more Jones’s algorithm still takes 3 seconds/order. He points out that a computer that is 50% faster only costs $500 more!

5 No it’s not… In general, algorithm A is not considered superior to algorithm B even if it can boast that it takes a fraction of the time. –For example half the time, one third the number of operations, etc. –Because run times are dependant on the way a compiler translate code, processor speeds etc. –A faster processor could take half the time, a faster compiler could take 1/3 the operations.

6 Some Time Later… Now suppose your order processing needs become much more complicated. –The old software you bought from Jones is no longer sufficient –You go back to Smith and Jones

7 Now whose do you buy? The best Smith can do is a minute an order. To process 300 orders will take 5 hours! Jones’s takes N 2 seconds where N is the number of orders. I.e. 1 second for 1 order, 4 seconds for 2 orders, 9 seconds for 3.

8 Compare! Number of Orders 1010 Minutes1½ Mins. 601 Hour 1202 Hours4 Hours 1803 Hours9 Hours 3005 Hours25 Hours

9 Jones’s algorithm is only better for small numbers Although Smith’s algorithm performs poorly for small numbers, it grows much more slowly than Jones’s How quickly an algorithm’s runtime grows is independent of : –What type of computer it runs on –What compiler is used –How much time it takes to set up.

10 Therefore… When we compare algorithm A and algorithm B, we conclude that algorithm A is more efficient only if its runtime grows more slowly than algorithm B as the size of the input grows. The growth of an algorithm’s run time is usually expressed in terms of “N”, the number of inputs. For example: –N 2, the square of the number of inputs –LogN, the logarithm of the number of inputs.

11 Why? How long an algorithm takes to set up, or how quickly it runs is not necessarily a property of the algorithm. –Affected by speed of the computer –Affected by efficiency of the compiler The growth of the runtime as the input grows is a property of the algorithm. –Unaffected by the speed of the computer –Unaffected by the efficiency of the compiler –There is no computer fast enough to make an algorithm that takes N 2 steps look like one that takes N steps

12 Some Examples Two algorithms: –One takes N 3 time –One takes 400N 2 time –Second is more efficient. It grows more slowly Two algorithms –One always takes 100000 seconds –One takes 2N seconds –First is more efficient – Grows more slowly

13 Some Examples cont’d Two algorithms: –One takes 3N time –One takes 100N time –These are considered similar –Could get the same improvement with a better compiler or a faster computer –I.e. the difference is not necessarily a characteristic of the algorithm

14 Summary A fixed amount of extra setup time (e.g. N+100 vs. N+200) is not enough to make an algorithm considered to be more efficient Run time multiplied by a smaller constant (e.g. 2N vs. 10N) is also not enough to make an algorithm considered to be more efficient. For an algorithm to be considered more efficient, its runtime must grow more slowly (e.g. N 2 vs. N 3 )


Download ppt "Comparison of Algorithms Objective: Understand how and why algorithms are compared. Imagine you own a company. You’re trying to buy software to process."

Similar presentations


Ads by Google