30

Here are a few: AppendTo[data, elem], Sort[data, test], in some cases use of ___ (as mentioned in David Wagner's book).

Edit:

A Wolfram Research developer explains here why Union[data,test] has quadratic complexity, and I am pretty sure the same applies to Sort[data,test]. The same Wolfram developer explains here that AppendTo[data,elem] has quadratic complexity. There are many math problems such as Inverse[matrix], FactorInteger[n] that are inherently expensive on large problems.

The most important answers to this question will be features in the core language that can kill the performance of a program. Sometimes you can greatly improve the performance of your program if you avoid such features.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ted Ersek
  • 7,124
  • 19
  • 41
  • 2
    Ted -- The question might inform a wider audience if you provide some background/explanation of n^2 complexity and it hazards, especially as related to a language like Mathematica, which does so much symbolically. I can imagine that Mathematica's paradigm might have significant vulnerabilities to cascading into complexity. – Jagra Jul 19 '13 at 18:53
  • Well, this is going to be a long list. Also: are you sure about Sort[data,test]? Generally, sorting is O(n log n). Check out Trace[Sort[{4, 3, 2, 1}, OrderedQ[{#1, #2}] &]] // Column. My guess is that Mathematica assumes that the function supplied as the second argument is one that yields an ordering on the supplied list, even though the user is free to supply a function that does not satisfy this. For example: Sort[{4, 3, 2, 1}, RandomChoice[{True, False}] &]. Mathematica therefore does not have to compare every two elements. For some fixed tests like 2*Prime[#] < Prime[#2]& ... – Jacob Akkerboom Jul 19 '13 at 19:50
  • We already have a "pitfalls" community wiki. I think this fits under that umbrella and would the answer should be made there. – m_goldberg Jul 20 '13 at 01:43
  • 2
    @m_goldberg Could you provide a link? – Dr. belisarius Jul 20 '13 at 13:19
  • @belisarius. I made my comment when I was half-asleep and now that I re-read it fully awake, I find its not very cogent. What I meant to say was: We already have a "pitfalls" community wiki, and I would prefer see this question treated there as another pitfall, rather than it having a separate existence. I did not mean that it was already answered there. – m_goldberg Jul 20 '13 at 22:32
  • Ted, thank you for adding links. I have checked out the one about AppendTo. Here however, it is not AppendTo that is meant to have quadratic complexity, but rather a construction like Do[AppendTo[list, f[k]],{k,n}]. This makes sense if copying a list has order has order n. Then copying a list n times has quadratic complexity. – Jacob Akkerboom Jul 21 '13 at 20:38
  • Also I am quite sure Sort does not have quadratic complexity. Consider the sentence in your first link: "Union will not sort but rather test a given element against every other (remaining) element". I think the comparison with sort is made to show that in the case of Union it is necessary to compare all elements pairwise, whereas this is not the case when you wish to sort. I'm quite sure the guys at Mathematica implemented a fast sort algorithm with good assymptotic properties and the fact that sorting is O(n log (n)) as can be seen here. – Jacob Akkerboom Jul 21 '13 at 20:48
  • But again, this assumes that the test in Sort[data, test] can be executed in constant time. – Jacob Akkerboom Jul 21 '13 at 20:50
  • 1
    @m_goldberg I would prefer it if there is a separate Q&A for this matter (as is currently the case). Especially because this complexity stuff tends to confuse people, myself included :). For example, this answer most likely confuses the concepts of big O notation and Theta notation and still got 2800 upvotes. – Jacob Akkerboom Aug 08 '13 at 10:34
  • Would you mind if we change this question to something along the lines of: "What are the complexities of these code snippets?" I think this has to be answered first before moving on to composing lists. – Jacob Akkerboom May 20 '15 at 13:46
  • @Jacob Akkerboom you can change the question to anything you like. Also, I would very much like to have a list of coding practices that are shoe to lead to slow programs in Mathematica. – Ted Ersek May 21 '15 at 20:53

0 Answers0