Questions tagged [performance-tuning]

Questions on optimizing Mathematica code for higher performance. This may mean faster execution, lower memory usage or both. Not to be confused with mathematical optimization.

Filter by
Sorted by
Tagged with
205 votes
3 answers
53k views

Alternatives to procedural loops and iterating over lists in Mathematica

While there are some cases where a For loop might be reasonable, it's a general mantra – one I subscribe to myself – that "if you are using a ...
Mr.Wizard's user avatar
  • 271k
181 votes
5 answers
18k views

Performance tuning in Mathematica?

What performance tuning tricks do you use to make a Mathematica application faster? MATLAB has an amazing profiler, but from what I can tell, Mathematica has no similar functionality.
John's user avatar
  • 2,429
122 votes
3 answers
9k views

What is a Mathematica packed array?

A simple sounding question with a few sub questions: What is the difference between unpacked vs packed array? Are packed arrays more space efficent, how much so? Are packed arrays more time ...
nixeagle's user avatar
  • 2,263
121 votes
1 answer
8k views

File-backed lists/variables for handling large data

Background Currently I am working with some large data (most of it generated by Mathematica itself). I usually find it a hassle to do this. For example, I just exported a large amount of data to ...
Szabolcs's user avatar
  • 234k
120 votes
1 answer
3k views

Why are numeric division and subtraction not handled better in Mathematica?

There is something that has been troubling me for a while. At least through version 10.0 the performance of a / b and a - b is ...
Mr.Wizard's user avatar
  • 271k
105 votes
3 answers
7k views

Profiling from Mathematica

I've always wished I could do some profiling like you get in Wolfram Workbench, but directly from Mathematica, without using or having Workbench. If it is possible, how can I do it?
Rojo's user avatar
  • 42.6k
99 votes
4 answers
4k views

What best practices or performance considerations are there for choosing between Cases, Position, Pick and Select?

Cases, Select,Pick and Position each have different ...
Verbeia's user avatar
  • 34.2k
96 votes
2 answers
8k views

Shaving the last 50 ms off NMinimize

The epilogue: A paper where the below answer is used was published. The answer below is cited among the references:) The background: I have to fit an objective function for ~10 000 datasets in near ...
Ajasja's user avatar
  • 13.6k
91 votes
11 answers
19k views

How to make an inkblot?

How to effectively create a polygon that looks like a realistic inkblot? So far, I could come up with this (borrowing from Ed Pegg Jr.'s Rorschach demonstration): ...
István Zachar's user avatar
85 votes
2 answers
5k views

Internal`Bag inside Compile

Since Internal`Bag, Internal`StuffBag and Internal`BagPart can be compiled down, it is a ...
halirutan's user avatar
  • 113k
77 votes
9 answers
12k views

How to efficiently find positions of duplicates?

Is there an efficient way to find the positions of the duplicates in a list? I would like the positions grouped according to duplicated elements. For instance, given ...
Michael E2's user avatar
  • 234k
75 votes
6 answers
15k views

How can this confetti code be improved to include shadows and gravity?

Here's some confetti: ...
David's user avatar
  • 14.9k
67 votes
1 answer
2k views

Is this the most effective and efficient programming style for prototyping (a ray tracer) in Mathematica?

I am trying out Mathematica as a prototyping tool. As an initial exercise I have put together a brute force ray tracer, with a view to using Mathematica's built in probability distribution functions ...
jrp's user avatar
  • 771
65 votes
2 answers
1k views

New Compiler Technology in V12

I am wondering whether the speed of compilation for the new compiler can be improved. Here are the results from my test. Consider the following function for generating a random draw from a gamma ...
Asim's user avatar
  • 1,150
64 votes
4 answers
4k views

Speeding up this fractal-generating code

I used the code below (which is a sample from this gist containing more similar code) in my answer to my own question about Mandelbrot-like sets for functions other than the simple quadratic on Math....
Isaac's user avatar
  • 3,189
64 votes
2 answers
11k views

How can I use Mathematica's graph functions to cheat at Boggle?

Boggle is a word game played with 16 dice and a 4x4 tray. This question is inspired by a Stack Overflow question about Boggle that I decided to solve using Mathematica. In addition to Mathematica, I ...
Pillsy's user avatar
  • 18.4k
56 votes
3 answers
2k views

Are there guidelines for avoiding the unpacking of a packed array?

Packed arrays are very useful because they save memory and generally allow speedier and more efficient calculation times. If a list of data currently stored as a packed array is unpacked, it can slow ...
Verbeia's user avatar
  • 34.2k
56 votes
1 answer
5k views

Why won't Parallelize speed up my code?

What reasons are there that can cause parallelized Mathematica code not to run with full performance?
Szabolcs's user avatar
  • 234k
54 votes
2 answers
3k views

Adaptive sampling for slow to compute functions in 2D

EDIT: Although I have posted an answer based on my current progress, this in incomplete. Please see the "open issues" section in the answer. Most plotting functions in Mathematica adjust the ...
Szabolcs's user avatar
  • 234k
53 votes
2 answers
2k views

A geometric multigrid solver for Mathematica?

Cross posted to community.wolfram.com Mathematica ships a variety of linear solvers through the interface LinearSolve[A, b, Method -> method] the most ...
Henrik Schumacher's user avatar
51 votes
4 answers
6k views

Benchmarking Mathematica performance on the Raspberry Pi

It is not surprising that Mathematica on the Raspberry Pi is slow and clunky. In Stephen Wolfram's words, the command line is "quite zippy" but the notebook interface is "a trifle sluggish by modern ...
bobthechemist's user avatar
49 votes
1 answer
3k views

Transferring a large amount of data in parallel calculations

Bug fixed in version 11.1 Functions like MemberQ, FreeQ, etc. no longer unpack. Yay! This question is inspired by one of @...
Szabolcs's user avatar
  • 234k
47 votes
3 answers
4k views

Smooth Peter de Jong attractor

Today I was playing with Peter de Jong attractor. At the bottom of the page I've linked there are beautiful examples like: My attempts are not so great: It is around 10^5 points. For more than 5*10^...
Kuba's user avatar
  • 137k
47 votes
2 answers
7k views

Is there any algorithm that runs faster in Mathematica than in C or Fortran?

I'm just curious. My friend just told me that Mathematica is mostly for symbolic calculation and not efficient for Numerical computations. He told me that's the reason most of the people don't use ...
Gummala Navneeth's user avatar
47 votes
3 answers
2k views

Why is the new PositionIndex horribly slow?

This issue has largely been mitigated in 10.0.1. New timings for the final test below are: ...
Mr.Wizard's user avatar
  • 271k
47 votes
2 answers
2k views

How to work with Experimental`NumericalFunction?

This question is intimately connected with previous one: "How to create internally optimized expression for computing with high WorkingPrecision?" Oleksandr R. correctly states in the comment: A ...
Alexey Popkov's user avatar
47 votes
0 answers
2k views

Fast Spherical Harmonics radiative transfer

This is a rather specific question and I apologize for spamming you with some lengthy code. But it could be interesting for some reader and maybe you can help out, so please bear with me. I am using ...
Markus Roellig's user avatar
46 votes
4 answers
2k views

Parallelize evaluation of function with memoization

I have a complicated function that I need multiple times, so I want to memoize it and have the first evaluation done in parallel. Unlike in my example below it's not a continuous function, so ...
Volker's user avatar
  • 463
45 votes
5 answers
9k views

Fastest way to calculate matrix of pairwise distances

It is a very common problem that given a distance function $d(p_1,p_2)$ and a set of points pts, we need to construct a matrix ...
Szabolcs's user avatar
  • 234k
45 votes
2 answers
8k views

Difference between AbsoluteTiming and Timing

I need to evaluate the efficiency of my code. Therefore I would like to evaluate the time the code need for some calculations. Mathematica gives me two possibilities for this kind of evaluation: <...
John's user avatar
  • 4,361
45 votes
5 answers
5k views

Is it possible to speed up ContourPlot on multi-core machines?

It is not very difficult to face a function for which ContourPlot works too slow. And it seems natural that this function can be parallelized well. Anyway, naive <...
faleichik's user avatar
  • 12.6k
45 votes
5 answers
24k views

What is the most efficient way to add rows and columns to a matrix?

Say I have a matrix m and a vector v. ...
Andy Ross's user avatar
  • 19.3k
45 votes
2 answers
5k views

Performance tuning for game solving (peg solitaire | senku)

There's a game I saw at a friend's yesterday, that I often see at people's homes, but never for enough time to think on it too hard. It's called peg solitaire (thanks @R.M). So I came home and I ...
Rojo's user avatar
  • 42.6k
44 votes
9 answers
3k views

What would you ask Mathematica to do on a big system?

Our hardware supplier has given us the opportunity to test Mathematica on a big Windows system. The specs will be 1 to 4 TB of working memory and 12 sockets with 18 cores each. I got 1 full week of ...
pvanbijnen's user avatar
44 votes
2 answers
6k views

How can I improve the speed of eigenvalue decompositions for large matrices?

I often need to compute the eigenvalues of large matrices, and I invariably resort to MATLAB for these, simply because it is much faster. I'd like to change that, so that I can work entirely inside my ...
rm -rf's user avatar
  • 88.6k
43 votes
2 answers
2k views

Mma 10: Half the parallel power (Macs)?

Here is a comparison of the parallel kernels launched under Mathematica under v9 and v10, on the same identical current 2014 R2-D2 Mac Pro ... [ Update: Valerio has commented that the same issue ...
wolfies's user avatar
  • 8,712
43 votes
0 answers
1k views

Performance problems in new Sequence functions

The new-in-10.1 Sequence* family of functions should provide newly optimized methods for handling sequence problems. Happily in some cases they do! For example ...
Mr.Wizard's user avatar
  • 271k
42 votes
5 answers
24k views

Finding Local Minima / Maxima in Noisy Data

I'm trying to find local minima / maxima in noisy data, consisting of data values taken at certain time intervals. Ideally, the function should take a pair of lists (one containing time values and one ...
Vincent Tjeng's user avatar
41 votes
10 answers
2k views

Happy 2K prime question

This being the Q number 2K in the site, and this being the day we got the confirmation of mathematica.se graduating soon, I think a celebration question is in order. So... What is the fastest way to ...
40 votes
5 answers
8k views

Efficiently generating n-D Gaussian random fields

I am interested in an efficient code to generate an $n$-D Gaussian random field (sometimes called processes in other fields of research), which has applications in cosmology. Attempt I wrote the ...
chris's user avatar
  • 22.8k
39 votes
2 answers
7k views

Exchanging numpy arrays between Python and Mathematica?

I've been using np.savetxt(location, result, delimiter=',') in Python followed by Import[location,"CSV"] in Mathematica. Some of ...
Yaroslav Bulatov's user avatar
39 votes
1 answer
2k views

Why is StringExpression faster than RegularExpression?

Edit: as noted by Albert Retey the performance difference is only seen when sub expression extraction is performed. If this test is used below the timings are similar: ...
Mr.Wizard's user avatar
  • 271k
38 votes
1 answer
1k views

Searching for still lifes in Conway's Game of Life

I'm trying to search for still lifes in Conway's Game of Life. An object in Conway's Game of Life can be represented by an array of booleans, say, Array[b, {w, h}]....
alephalpha's user avatar
  • 1,293
37 votes
1 answer
618 views

Why is multidimensional Reverse slow?

Bug introduced in 6.0 and persisting through 14.0. I have found that multidimensional Reverse is ~10 times slower than exact ...
ybeltukov's user avatar
  • 43.6k
36 votes
4 answers
7k views

Why is raytracing so slow?

I'm trying to get some rays to bounce in a circle. But I want to be able to control the reflections, i.e. the direction the rays bounce in the circle. I have a MWE below, and it is severely limited by ...
Tomi's user avatar
  • 4,366
36 votes
6 answers
7k views

Finding all elements within a certain range in a sorted list

Suppose we have a sorted list of values. Let's use list = Sort@RandomReal[1, 1000000]; for this example. I need a fast function ...
Szabolcs's user avatar
  • 234k
36 votes
3 answers
2k views

DeleteDuplicatesBy is not performing as I'd hoped. Am I missing something?

Déjà vu: a new-in-v10 function should provide a better solution to an old problem, but my enthusiasm is curbed when I run timings. This time the function is ...
Mr.Wizard's user avatar
  • 271k
36 votes
1 answer
7k views

Numerical integration --- Mathematica vs Python (w/ Scipy) performance

I'm about to tackle a problem that involves a lot of (multi-dimensional) numerical integrations and also subsequent optimizations, and so I want to make sure at least the integration step is as fast ...
user32416's user avatar
  • 1,203
36 votes
3 answers
2k views

Can a Trie be implemented efficiently?

What is the most space and time efficient way to implement a Trie in Mathematica? Will it be practically faster than what is natively available in appropriate cases?
Mr.Wizard's user avatar
  • 271k
36 votes
4 answers
3k views

How to collect result continuously (interruptible calculation) when running parallel calculations?

This is the most common pattern to compute a table of results: Table[function[p], {p, parameters}] (regardless of how it's implemented, it could be a ...
Szabolcs's user avatar
  • 234k

1
2 3 4 5
59