Presentation on theme: "Algorithms Today we will look at: what we mean by efficiency in programs why efficiency matters what causes programs to be inefficient? will one algorithm."— Presentation transcript:
Algorithms Today we will look at: what we mean by efficiency in programs why efficiency matters what causes programs to be inefficient? will one algorithm always be the most efficient way of doing something? can efficiency be measured?
What is Efficiency? Efficiency is about making the best use of available resources... –a fuel-efficient car will travel further on a litre of fuel –an energy-efficient light-bulb will produce more light from the same amount of electricity In programming, efficiency is a similar idea – but what are the resources? –memory (RAM) –secondary storage (e.g. disc space) –processor time (which, in most cases, equates to the actual amount of time taken to do a task)
Why Does It Matter? As computers get faster and have more memory and storage space, efficiency is becoming less of an issue, but: users find it stressful to wait for computers to complete tasks, and employers have a duty to look after their employees’ health sometimes speed of reaction is important – e.g. with real-time control systems, or with things like simulators and games to ensure a realistic experience. some computers – e.g. web-servers – run more than one program concurrently; if your web-site is inefficient, it could have an impact on the others running on the same server.
What Affects Efficiency? Efficient database design, re-use of variables, the use of binary flags to combine multiple values in a single byte can all of used to reduce memory usage. Efficient file formats and compression techniques can be used to minimise the amount of storage space required (although compression does take time to do!). Hardware is getting cheaper and better, though, so you’re most likely to want to save time through efficient algorithm design.
What Makes Program Slower? Processors perform instructions in cycles, and not all steps in your program will require a single cycle (it depends on the architecture, e.g. RISC v. CISC). Two big factors affecting performance are: –unnecessary repetition: the more times you repeat, e.g. comparisons when sorting – the longer the program will take to run. Try to use as few steps as possible. –complex calculations: some types of calculations (e.g. sines, cosines or square roots) take longer than simple arithmetic. For this reason, if you use a complex calculation more than once in your program, do it once and assign the value to a variable. NB. This will all change when quantum computers appear!
Does the Efficiency of a Program Change? You have probably already noticed that the amount of data affects how long it takes for a program to run – e.g. longer CDs take longer to rip, and larger files take longer to load, etc. – even though the program is doing the same thing. Sometimes the type or properties of the data might have an effect on the efficiency of an program – e.g. compare sorting algorithms for: –values that are nearly ordered already –random values –values in reverse order
Can Efficiency Be Measured? Computer Scientists use a measure called big O to describe how long a program takes to run (or, more strictly-speaking, the time-complexity of the algorithm!) The letter n is used to describe the number of inputs to a program – e.g. if you’re sorting ten numbers then n = 10. The average “complexities” of common sorts are: –Bubble sort: O(n 2 ) –Selection sort: O(n 2 ) –Insertion sort: O(n 2 ) –Quicksort: O(n log n) This also indicates how the time increase with the amount of data – e.g. if you sort twenty numbers instead, a bubble sort will take 4x longer, but a quicksort only 2.6x longer.