Algorithmic efficiency

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer science, algorithmic efficiency is used to describe those properties of an algorithm which relate to the amount of resources used by the algorithm. An algorithm must be analysed to determine its resource usage. Algorithmic efficiency can be thought of as analogous to engineering productivity for a repeating or continuous process.

Gal-Ezer and Zur have stated: "The design of efficient algorithms to solve algorithmic problems is one of the most important research fields in computer science. Good algorithm design is crucial to the performance of all software systems and therefore essential in every computer science program of study."[1]

For maximum efficiency we wish to minimize resource usage. However, the various resources (e.g. time, space) can not be compared directly, so which of two algorithms is considered to be more efficient often depends on which measure of efficiency is being considered as the most important, e.g. is the requirement for high speed, or for minimum memory usage, or for some other measure?

Note that this article is not about optimization, which is discussed in program optimization, optimizing compiler, loop optimization, and object code optimizer. The term 'optimization' is itself misleading, since all that can generally be done is an 'improvement'.

Contents

Background[edit]

The importance of efficiency with respect to time was emphasised by Ada Lovelace in 1843 as applying to Charles Babbage's mechanical analytical engine:

"In almost every computation a great variety of arrangements for the succession of the processes is possible, and various considerations must influence the selections amongst them for the purposes of a calculating engine. One essential object is to choose that arrangement which shall tend to reduce to a minimum the time necessary for completing the calculation"[2]

Early electronic computers were severely limited both by the speed of operations and the amount of memory available. In some cases it was realised that there was a space–time tradeoff, whereby a task could be handled either by using a fast algorithm which used quite a lot of working memory, or by using a slower algorithm which used very little working memory. The engineering tradeoff was then to use the fastest algorithm which would fit in the available memory.

Modern computers are very much faster than the early computers, and have a much larger amount of memory available (Gigabytes instead of Kilobytes). Nevertheless, Donald Knuth emphasised that efficiency is still an important consideration:

"In established engineering disciplines a 12% improvement, easily obtained, is never considered marginal and I believe the same viewpoint should prevail in software engineering"[3]

Overview[edit]

An algorithm is considered efficient if its resource consumption is at or below some acceptable level. Roughly speaking, 'acceptable' means: will it run in a reasonable amount of time on an available computer. Since the 1950s computers have seen dramatic increases in both the available computational power and in the available amount of memory, so current acceptable levels would have been unacceptable even 10 years ago.

Computer manufacturers frequently bring out new models, often with higher performance. Software costs can be quite high, so in some cases the simplest and cheapest way of getting higher performance might be to just buy a faster computer, provided it is compatible with an existing computer.

There are many ways in which the resources used by an algorithm can be measured: the two most common measures are speed and memory usage; other measures could include transmission speed, temporary disk usage, long-term disk usage, power consumption, total cost of ownership, response time to external stimuli, etc. Many of these measures depend on the size of the input to the algorithm (i.e. the amount of data to be processed); they might also depend on the way in which the data is arranged (e.g. some sorting algorithms perform poorly on data which is already sorted, or which is sorted in reverse order).

In practice, there are other factors which can affect the efficiency of an algorithm, such as requirements for accuracy and/or reliability. As detailed below, the way in which an algorithm is implemented can also have a significant effect on actual efficiency, though many aspects of this relate to optimisation issues.

Theoretical analysis[edit]

In the theoretical analysis of algorithms, the normal practice is to estimate their complexity in the asymptotic sense, i.e. use Big O notation to represent the complexity of an algorithm as a function of the size of the input n. This is generally sufficiently accurate when n is large, but may be misleading for small values of n (e.g. bubble sort may be faster than quicksort when only a few items are to be sorted).

Some examples of Big O notation include:

Notation Name Examples
O(1)\, constant Determining if a number is even or odd; Using a constant-size lookup table; Using a suitable hash function for looking up an item.
O(\log n)\, logarithmic Finding an item in a sorted array with a binary search or a balanced search tree as well as all operations in a Binomial heap.
O(n)\, linear Finding an item in an unsorted list or a malformed tree (worst case) or in an unsorted array; Adding two n-bit integers by ripple carry.
O(n\log n)\, linearithmic, loglinear, or quasilinear Performing a Fast Fourier transform; heapsort, quicksort (best and average case), or merge sort
O(n^2)\, quadratic Multiplying two n-digit numbers by a simple algorithm; bubble sort (worst case or naive implementation), Shell sort, quicksort (worst case), selection sort or insertion sort
O(c^n),\;c>1 exponential Finding the (exact) solution to the travelling salesman problem using dynamic programming; determining if two logical statements are equivalent using brute-force search

Benchmarking: measuring performance[edit]

For new versions of software or to provide comparisons with competitive systems, benchmarks are sometimes used, which assist with gauging an algorithms relative performance. If a new sort algorithm is produced for example it can be compared with its predecessors to ensure that at least it is efficient as before with known data—taking into consideration any functional improvements. Benchmarks can be used by customers when comparing various products from alternative suppliers to estimate which product will best suit their specific requirements in terms of functionality and performance. For example in the mainframe world certain proprietary sort products from independent software companies such as Syncsort compete with products from the major suppliers such as IBM for speed.

Some benchmarks provide opportunities for producing an analysis comparing the relative speed of various compiled and interpreted languages for example[4][5] and The Computer Language Benchmarks Game[6] compares the performance of implementations of typical programming problems in several programming languages.

(Even creating "do it yourself" benchmarks to get at least some appreciation of the relative performance of different programming languages, using a variety of user specified criteria, is quite simple to produce as this "Nine language Performance roundup" by Christopher W. Cowell-Shah demonstrates by example)[7]

Implementation issues[edit]

Implementation issues can also have an effect on actual efficiency, such as the choice of programming language, or the way in which the algorithm is actually coded, or the choice of a compiler for a particular language, or the compilation options used, or even the operating system being used. In some cases a language implemented by an interpreter may be much slower than a language implemented by a compiler.[4]

There are other factors which may affect time or space issues, but which may be outside of a programmer's control; these include data alignment, data granuality, garbage collection, instruction-level parallelism, and subroutine calls. [8]

Some processors have capabilities for vector processing, which allow a single instruction to operate on multiple operands; it may or may not be easy for a programmer or compiler to use these capabilities. Algorithms designed for sequential processing may need to be completely redesigned to make use of parallel processing.

Another problem which can arise with compatible processors is that they may implement an instruction in different ways, so that instructions which are relatively fast on some models may be relatively slow on other models; this can make life difficult for an optimizing compiler.

Measures of resource usage[edit]

Measures are normally expressed as a function of the size of the input n.

The two most common measures are:

  • Time: how long does the algorithm take to complete.
  • Space: how much working memory (typically RAM) is needed by the algorithm. This has two aspects: the amount of memory needed by the code, and the amount of memory needed for the data on which the code operates.

For computers whose power is supplied by a battery (e.g. laptops), or for very long/large calculations (e.g. supercomputers), other measures of interest are:

  • Direct power consumption: power needed directly to operate the computer.
  • Indirect power consumption: power needed for cooling, lighting, etc.

In some cases other less common measures may also be relevant:

  • Transmission size: bandwidth could be a limiting factor. Data compression can be used to reduce the amount of data to be transmitted. Displaying a picture or image (e.g. Google logo) can result in transmitting tens of thousands of bytes (48K in this case) compared with transmitting six bytes for the text "Google".
  • External space: space needed on a disk or other external memory device; this could be for temporary storage while the algorithm is being carried out, or it could be long-term storage needed to be carried forward for future reference.
  • Response time: this is particularly relevant in a real-time application when the computer system must respond quickly to some external event.
  • Total cost of ownership: particularly if a computer is dedicated to one particular algorithm.

Time[edit]

Theory[edit]

Analyse the algorithm, typically using time complexity analysis to get an estimate of the running time as a function as the size of the input data. The result is normally expressed using Big O notation. This is useful for comparing algorithms, especially when a large amount of data is to processed. More detailed estimates are needed for algorithm comparison when the amount of data is small (though in this situation time is less likely to be a problem anyway). Algorithms which include parallel processing may be more difficult to analyse.

Practice[edit]

Time the use of an algorithm. Many programming languages have an available function which provides CPU time usage. For long-running algorithms the elapsed time could also be of interest. Results should generally be averaged over several tests.

This sort of test can be very sensitive to hardware configuration and the possibility of other programs or tasks running at the same time in a multi-processing and multi-programming environment.

This sort of test also depends heavily on the selection of a particular programming language, compiler, and compiler options, so algorithms being compared must all be implemented under the same conditions.

Space[edit]

This section is concerned with the use of main memory (often RAM) while the algorithm is being carried out. As for time analysis above, analyse the algorithm, typically using space complexity analysis to get an estimate of the run-time memory needed as a function as the size of the input data. The result is normally expressed using Big O notation.

There are up to four aspects of memory usage to consider:

  • The amount of memory needed to hold the code for the algorithm.
  • The amount of memory needed for the input data.
  • The amount of memory needed for any output data (some algorithms, such as sorting, often just rearrange the input data and don't need any space for output data).
  • The amount of memory needed as working space during the calculation (this includes both named variables and any stack space needed by routines called during the calculation; this stack space can be significant for algorithms which use recursive techniques).

Early electronic computers, and early home computers, had relatively small amounts of working memory. E.g. the 1949 EDSAC had a maximum working memory of 1024 17-bit words, while the 1980 Sinclair ZX80 came initially with 1024 8-bit bytes of working memory.

Current computers can have relatively large amounts of memory (possibly Gigabytes), so having to squeeze an algorithm into a confined amount of memory is much less of a problem than it used to be. But the presence of three different categories of memory can be significant:

  • Cache memory (often static RAM) - this operates at speeds comparable with the CPU.
  • Main physical memory (often dynamic RAM) - this operates somewhat slower than the CPU.
  • Virtual memory (often on disk) - this gives the illusion of lots of memory, and operates thousands of times slower than RAM.

An algorithm whose memory needs will fit in cache memory will be much faster than an algorithm which fits in main memory, which in turn will be very much faster than an algorithm which has to resort to virtual memory. To further complicate the issue, some systems have up to three levels of cache memory, with varying effective speeds. Different systems will have different amounts of these various types of memory, so the effect of algorithm memory needs can vary greatly from one system to another.

In the early days of electronic computing, if an algorithm and its data wouldn't fit in main memory then the algorithm couldn't be used. Nowadays the use of virtual memory appears to provide lots of memory, but at the cost of performance. If an algorithm and its data will fit in cache memory, then very high speed can be obtained; in this case minimising space will also help minimise time. An algorithm which will not fit completely in cache memory but which exhibits locality of reference may perform reasonably well.

Examples of efficient algorithms[edit]

Optimization techniques[edit]

The word optimize is normally used in relation to an existing algorithm/computer program (i.e. to improve upon completed code). In this section it is used both in the context of existing programs and also in the design and implementation of new algorithms, thereby avoiding the most common performance pitfalls. It is clearly wasteful to produce a working program—at first using an algorithm that ignores all efficiency issues—only to then have to redesign or rewrite sections of it if found to offer poor performance. Optimization can be broadly categorized into two domains:-

  • Environment specific—that are essentially worthwhile only on certain platforms or particular computer languages
  • General techniques—that apply irrespective of platform

Software validation versus hardware validation[edit]

An optimization technique that was frequently taken advantage of on 'legacy' platforms was that of allowing the hardware (or microcode) to perform validation on numeric data fields such as those coded in (or converted to) packed decimal (or packed BCD). The choice was to either spend processing time checking each field for a valid numeric content in the particular internal representation chosen or simply assume the data was correct and let the hardware detect the error upon execution. The choice was highly significant because to check for validity on multiple fields (for sometimes many millions of input records), it could occupy valuable computer resources. Since input data fields were in any case frequently built from the output of earlier computer processing, the actual probability of a field containing invalid data was exceedingly low and usually the result of some 'corruption'. The solution was to incorporate an 'event handler' for the hardware detected condition ('data exception)' that would intercept the occasional errant data field and either 'report, correct and continue' or, more usually, abort the run with a core dump to try to determine the reason for the bad data.

Similar event handlers are frequently utilized in today's web based applications to handle other exceptional conditions but repeatedly parsing data input, to ensure its validity before execution, has nevertheless become much more commonplace—partly because processors have become faster (and the perceived need for efficiency in this area less significant) but, predominantly—because data structures have become less 'formalized' (e.g. .csv and .tsv files) or uniquely identifiable (e.g. packed decimal). The potential savings using this type of technique may have therefore fallen into general dis-use as a consequence and therefore repeated data validations and repeated data conversions have become an accepted overhead. Ironically, one consequence of this move to less formalized data structures is that a corruption of say, a numeric binary integer value, will not be detected at all by the hardware upon execution (for instance: is an ASCII hexadecimal value '20202020' a valid signed or unsigned binary value—or simply a string of blanks that has corrupted it?)

Readability, trade offs and trends[edit]

One must be careful, in the pursuit of good coding style, not to over-emphasize efficiency. Frequently, a clean, readable and 'usable' design is much more important than a fast, efficient design that is hard to understand. There are exceptions to this 'rule' (such as embedded systems, where space is tight, and processing power minimal) but these are rarer than one might expect.

However, increasingly, for many 'time critical' applications such as air line reservation systems, point-of-sale applications, ATMs (cash-point machines), Airline Guidance systems, Collision avoidance systems and numerous modern web based applications—operating in a real-time environment where speed of response is fundamental—there is little alternative.

Implications for algorithmic efficiency[edit]

A recent report, published in December 2007, from Global Action Plan,[9] a UK-based environmental organisation found that computer servers are "at least as great a threat to the climate as SUVs or the global aviation industry" drawing attention to the carbon footprint of the IT industry in the UK.[10][11] According to an Environmental Research Letters report published in September 2008, "Total power used by information technology equipment in data centers represented about 0.5% of world electricity consumption in 2005. When cooling and auxiliary infrastructure are included, that figure is about 1%. The total data center power demand in 2005 is equivalent (in capacity terms) to about seventeen 1000 MW power plants for the world."[12]

Some media reports claim that performing two Google searches from a desktop computer can generate about the same amount of carbon dioxide as boiling a kettle for a cup of tea, according to new research;[13] however, the factual accuracy of this comparison is disputed,[14] and the author of the study in question asserts that the two-searches-tea-kettle statistic is a misreading of his work.[15]

Greentouch,[16][17] a recently established consortium of leading Information and Communications Technology (ICT) industry, academic and non-governmental research experts, has set itself the mission of reducing reduce energy consumption per user by a factor of 1000 from current levels. "A thousand-fold reduction is roughly equivalent to being able to power the world’s communications networks, including the Internet, for three years using the same amount of energy that it currently takes to run them for a single day". The first meeting in February 2010 will establish the organization’s five-year plan, first year deliverables and member roles and responsibilities. Intellectual property issues will be addressed and defined in the forum’s initial planning meetings. The conditions for research and the results of that research will be high priority for discussion in the initial phase of the research forum’s development.[18]

Computers having become increasingly more powerful over the past few decades, emphasis was on a 'brute force' mentality. This may have to be reconsidered in the light of these reports and more effort placed in future on reducing carbon footprints through optimization. It is a timely reminder that algorithmic efficiency is just another aspect of the more general thermodynamic efficiency. The genuine economic benefits of an optimized algorithm are, in any case, that more processing can be done for the same cost[19] or that useful results can be shown in a more timely manner and ultimately, acted upon sooner.

Criticism of the current state of programming[edit]

  • David May FRS a British computer scientist and currently Professor of Computer Science at University of Bristol and founder and CTO of XMOS Semiconductor, believes one of the problems is that there is a reliance on Moore's law to solve inefficiencies. He has advanced an 'alternative' to Moore's law (May's law) stated as follows:[20]

    Software efficiency halves every 18 months, compensating Moore's Law

    He goes on to state

    In ubiquitous systems, halving the instructions executed can double the battery life and big data sets bring big opportunities for better software and algorithms: Reducing the number of operations from N x N to N x log(N) has a dramatic effect when N is large... for N = 30 billion, this change is as good as 50 years of technology improvements

  • Software author Adam N. Rosenburg in his blog "The failure of the Digital computer", has described the current state of programming as nearing the "Software event horizon", (alluding to the fictitious "shoe event horizon" described by Douglas Adams in his Hitchhiker's Guide to the Galaxy book[21]). He estimates there has been a 70 dB factor loss of productivity or "99.99999 percent, of its ability to deliver the goods", since the 1980s—"When Arthur C. Clarke compared the reality of computing in 2001 to the computer HAL in his book 2001: A Space Odyssey, he pointed out how wonderfully small and powerful computers were but how disappointing computer programming had become".
  • Conrad Weisert gives examples, some of which were published in ACM SIGPLAN (Special Interest Group on Programming Languages) Notices, December 1995 in: "Atrocious Programming Thrives"[22]
  • Marc Andreessen co-founder of Netscape is quoted in "Mavericks at Work" (ISBN 0060779616) as saying "Five great programmers can completely outperform 1,000 mediocre programmers."[2]

Competitions for the best algorithms[edit]

The following competitions invite entries for the best algorithms based on some arbitrary criteria decided by the judges:-

See also[edit]

References[edit]

  1. ^ Gal-Ezer, Judith; Zur, Ela (2003), "The efficiency of algorithms—misconceptions", Computers and Education (Elsevier) 42 (3): 215–226, retrieved 17 May 2013 
  2. ^ Green, Christopher, Classics in the History or Psychology, retrieved 19 May 2013 
  3. ^ Knuth, Donald (1974), "Structured Programming with go-to Statements", Computing Surveys (ACM) 6 (4), retrieved 19 May 2013 
  4. ^ a b "Floating Point Benchmark: Comparing Languages (Fourmilog: None Dare Call It Reason)". Fourmilab.ch. 2005-08-04. Retrieved 2011-12-14. 
  5. ^ "Whetstone Benchmark History". Roylongbottom.org.uk. Retrieved 2011-12-14. 
  6. ^ "The Computer Language Benchmarks Game". benchmarksgame.alioth.debian.org. Retrieved 2011-12-14. 
  7. ^ http://www.osnews.com/story/5602
  8. ^ Guy Lewis Steele, Jr. "Debunking the 'Expensive Procedure Call' Myth, or, Procedure Call Implementations Considered Harmful, or, Lambda: The Ultimate GOTO". MIT AI Lab. AI Lab Memo AIM-443. October 1977.[1]
  9. ^ "Environmental Charity". Global Action Plan. Retrieved 2011-12-14. 
  10. ^ "Computer servers 'as bad' for climate as SUVs - environment - 03 December 2007 - New Scientist". Environment.newscientist.com. Retrieved 2011-12-14. 
  11. ^ "Innovators: Efficiency Matters - March April 2007 - Sierra Magazine". Sierra Club. Retrieved 2011-12-14. 
  12. ^ "Worldwide electricity used in data centers". Iop.org. Retrieved 2011-12-14. 
  13. ^ "Research Reveals Environmental Impact of Google Searches". Fox News. 2009-01-12. 
  14. ^ Timmer, John (2009-01-12). "Analysis: environmental impact of Googling hard to quantify". Arstechnica.com. Retrieved 2011-12-14. 
  15. ^ "Technology News: Green Tech: Harvard Physicist Sets Record Straight on Internet Carbon Study". Technewsworld.com. Retrieved 2011-12-14. 
  16. ^ "About Us". Green Touch. Retrieved 2011-12-14. 
  17. ^ "Gee Rittenhouse, Bell Labs, on Shannon's Law and the Green Touch initiative". YouTube. 2010-01-11. Retrieved 2011-12-14. 
  18. ^ "Challenges and Opportunities". Green Touch. Retrieved 2011-12-14. 
  19. ^ "Energy Efficiency in the Data Center". YouTube. 2008-07-22. Retrieved 2011-12-14. 
  20. ^ http://www.cs.bris.ac.uk/~dave/iee.pdf
  21. ^ http://www.the-adam.com/adam/rantrave/computers.htm
  22. ^ "Atrocious Programming Thrives". Idinews.com. 2003-01-09. Retrieved 2011-12-14. 
  23. ^ Fagone, Jason (2010-11-29). "Teen Mathletes Do Battle at Algorithm Olympics". Wired. 

External links[edit]