This question already has an answer here:

Recently I have been solving some Project Euler problems. I find there are always several ways to do the same thing. For example, one could join two lists like this:

a = {2, 1};
b = {2, 3};
Union[a, b]
Join[a, b]

Which gives

{1, 2, 3}
{2, 1, 2, 3}

So the Union function sorts the element and deletes duplicates. I guess the Join function will be faster, but how much faster? In a loop, would it be more efficient to use Join and DeleteDuplicates at each 100th step?

Is there documentation of the comparative performance of Mathematica functions? Because heuristics as above could be totally wrong, and I don't know how much experimenting it would take to form a good understanding of the complexity of functions.


marked as duplicate by Kuba, Yves Klett, Michael E2, Sjoerd C. de Vries, Dr. belisarius Apr 28 '14 at 14:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ Performance tuning in Mathematica is more complicated than that. Start a fresh kernel and try {Timing[Prime[10000000000]][[1]], Timing[Prime[10000000000]][[1]]}. Also, many of the algorithms use heuristics to switch between strategies when appropriate. $\endgroup$ – Dr. belisarius Apr 1 '13 at 15:21
  • 3
    $\begingroup$ Your specific question is so highly specialized that I doubt you will find an answer no matter how much performance documentation might exist. To get an accurate answer, you will need to do a quick timing study in your particular circumstance. $\endgroup$ – whuber Apr 1 '13 at 15:32
  • 1
    $\begingroup$ Here's one way to tell in any particular case which of two (or more) functions is faster: mathematica.stackexchange.com/questions/22419/… $\endgroup$ – bill s Apr 1 '13 at 17:47