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
197 votes
3 answers
48k 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 ...
user avatar
  • 264k
121 votes
3 answers
8k 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 ...
user avatar
  • 2,243
178 votes
5 answers
17k 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.
user avatar
  • 2,177
119 votes
1 answer
7k 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 ...
user avatar
  • 224k
73 votes
9 answers
11k 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 ...
user avatar
  • 213k
78 votes
2 answers
5k views

Internal`Bag inside Compile

Since Internal`Bag, Internal`StuffBag and Internal`BagPart can be compiled down, it is a ...
user avatar
  • 111k
94 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 ...
user avatar
  • 13.4k
43 votes
5 answers
21k 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. ...
user avatar
  • 18.9k
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 ...
user avatar
  • 224k
53 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?
user avatar
  • 224k
47 votes
1 answer
2k 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 @...
user avatar
  • 224k
117 votes
1 answer
2k 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 ...
user avatar
  • 264k
55 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 ...
user avatar
  • 33.6k
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 ...
user avatar
  • 17.8k
35 votes
6 answers
6k 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 ...
user avatar
  • 224k
21 votes
1 answer
1k views

Using Evaluate and Evaluated -> True in Plot

A few use cases: (1) This, as expected, returns (almost) immediately: f[x_?NumericQ] := Evaluate[(Pause[1]; x)] Plot[f[x], {x, 0, 1}] (2) This takes ...
user avatar
35 votes
3 answers
2k views

How to flush machine underflows to zero and prevent conversion to arbitrary precision?

I'm working on some pretty intense computation in Mathematica; when my code started running slowly, I tracked the source of the problem to Exp[]. I need to ...
user avatar
  • 461
98 votes
4 answers
3k 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 ...
user avatar
  • 33.6k
42 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 ...
user avatar
40 votes
2 answers
7k 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: <...
user avatar
  • 4,221
16 votes
3 answers
3k views

Plots quality during manipulation

I apologize if this is a obvious question and answer, I don't often use Mathematica to display plots or graphics in general to be honest. So, I was tutoring my cousin yesterday in relation to Polar ...
user avatar
  • 163
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?
user avatar
  • 264k
27 votes
3 answers
3k views

Reading periodic elements from a large file

I have a large binary data file (big endian) with 100+ million "rows" of 11 elements, combination of floats and integers. This is the format: ...
user avatar
  • 9,472
96 votes
2 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?
user avatar
  • 41.6k
25 votes
8 answers
4k views

Fastest square number test

What is the fastest possible square number test in Mathematica, both for machine size and big integers? I presume starting in version 8 the fastest will be a dedicated C LibraryLink function.
user avatar
  • 264k
47 votes
3 answers
1k 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: ...
user avatar
  • 264k
40 votes
5 answers
7k 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 ...
user avatar
  • 21.7k
31 votes
3 answers
2k views

Common subexpression from two expressions

I am working with some unpleasantly tedious polynomials, which need to be manipulated in various ways (integrate with respect to some variable, differentiate with respect to another). Since these ...
user avatar
  • 411
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 ...
user avatar
  • 224k
17 votes
2 answers
651 views

Additive SparseArray Assembly

The goal is to assemble a SparseArray in an additive fashion. Let us assume we have a large List of indices (some will be ...
user avatar
  • 728
75 votes
6 answers
15k views

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

Here's some confetti: ...
user avatar
  • 14.6k
20 votes
2 answers
8k views

Speeding up Import and Export in CSV format

I am handling large numerical data in Mathematica. In smaller problems everything worked fine using Export and Import with the ...
user avatar
  • 323
18 votes
5 answers
2k views

Iterate until condition is met

I want to find the first 5 prime numbers of the form $n^6 + 1091$. I have used this code: Timing[Select[Table[n^6 + 1091, {n, 10000}], PrimeQ, 5]] Which gives ...
user avatar
33 votes
1 answer
2k views

Has this implementation of FDM touched the speed limit of Mathematica?

Still, I'll use the implementation of the 1D FDTD method (you can simply understand it as a kind of explicit finite difference scheme for the Maxwell's equation) as the example. Just for completeness, ...
user avatar
  • 51.5k
36 votes
3 answers
1k 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 ...
user avatar
  • 264k
16 votes
4 answers
854 views

Lazy form of Tuples/Outer to loop over list of lists

This is less a question and more asking if someone has implemented this already, with more skill. I need to perform the Outer-like generalized outer product of a ...
user avatar
14 votes
9 answers
1k views

Thread over list in different levels

I have these two lists, both with the same length in the first dimension: ...
user avatar
  • 25.3k
7 votes
2 answers
2k views

Why Can't `DSolve` Find a Solution for this ODE?

I wanted to find a basis for the set of solutions of the following ODE. $$y^{''}+\frac{1}{x^2+1}y^{'}(x)+\left[-1-\frac{1}{x^2+1}\right]y(x)=0$$ But when I try to use ...
user avatar
28 votes
1 answer
2k views

Two ways to map a function on a list: Which one is faster?

There are two ways to make a function listable on the list: Use Map or use SetAttributes. My question is, which one is faster, ...
user avatar
  • 1,609
39 votes
1 answer
1k 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: ...
user avatar
  • 264k
22 votes
6 answers
2k views

How to obtain the cell-adjacency graph of a mesh?

In addition to the accepted answer, see also the answer by Chip Hurst. This functionality is built in, but not documented. Given an arbitrary mesh region, how can I efficiently obtain the graph ...
user avatar
  • 224k
17 votes
2 answers
3k views

Bresenham's line algorithm

Bresenham's line algorithm is producing discretized line for given two points for purpose of plotting for example. Like that: I have to stress that I'm interested in positions, not a plot. ...
user avatar
  • 132k
7 votes
1 answer
868 views

Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
user avatar
  • 264k
88 votes
9 answers
18k 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): ...
user avatar
53 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 ...
user avatar
  • 224k
40 votes
5 answers
21k 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 ...
user avatar
24 votes
6 answers
3k views

How to speed up the function DelaunayTriangulation?

First define a function meshGrid to generate some points: ...
user avatar
  • 2,451
11 votes
2 answers
654 views

Using a given colour table with Image?

Context I would like to represent large images with a given colour table. Now, if I use Image ...
user avatar
  • 21.7k
26 votes
2 answers
3k views

Implementation of Balaban's Line intersection algorithm in Mathematica

I'm trying to implement a Brillouin Zone algorithm within Mathematica, including the generation of Brillouin zones of higher order in 2D and 3D. There is a nice implementation of generating these ...
user avatar
  • 2,751
33 votes
2 answers
842 views

Why is CompilationTarget -> C slower than directly writing with C?

Probably a hard question, but I think it's better to cry out loud. I've hesitated for a while about whether I should post this in StackOverflow with a c tag or not, ...
user avatar
  • 51.5k

1
2 3 4 5
15