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.

learn more… | top users | synonyms (2)

5
votes
2answers
720 views

Speeding up mathematica by subsitituting numerical values

According to this Wolfram Blog post, one can speed up Mathematica code by substituting numerical values as soon as possible. How does one substitute before the main expression is evaluated? The ...
7
votes
1answer
192 views

Why do images in a PopupMenu sometimes make a program load sluggishly?

In a demonstration, I would like to display a PopupMenu with a list of small PDF images, each weighing in at approximately 50k. The code of interest is below: ...
15
votes
4answers
611 views

How to reduce the InterpolatingFunction building overhead?

I want a linear interpolation from the following example list: ...
7
votes
2answers
195 views

Speed up Fourier for Booleans

I'm calculating a bit of the power spectrum of a two color cellular automaton using: ...
20
votes
4answers
2k 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. I wrote the following ...
60
votes
2answers
4k 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 ...
19
votes
3answers
443 views

Is there a faster way to create a matrix of indices from ragged data?

I have data that is given as a list of ordered pairs mixed with scalars. The pairs can contain infinite bounds. My goal is to convert the data into an index used in future computations. ...
20
votes
3answers
2k views

Data fitting with Image processing feature detection

I have some 2D data that once plotted looks like the following ...
19
votes
2answers
945 views

Using the Krylov method for Solve: Speeding up a SparseArray calculation

I'm trying to implement this Total Variation Regularized Numerical Differentiation (TVDiff) code in MMA (which I found through this SO answer): essentially I want to differentiate noisy data. The full ...
9
votes
1answer
462 views

GeoDirection and GeoDistance Memory Leaks: How to Recover the Memory?

The functions GeoDirection and GeoDistance have memory leaks. To see this, run the example functions below and watch as the Physical Memory Available in the Windows Task Manager, Performance tab, ...
17
votes
4answers
468 views

Getting lengths of sublists that sum to more than one

I have a long list of numbers, and I'm interested in finding the length of each sublist that totals to more than 1, so for the list ...
7
votes
4answers
591 views

Loop optimization

I wrote this to generate a 6D distribution of particles. ...
54
votes
2answers
2k 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 ...
12
votes
2answers
286 views

Parallelization of distinct array write access from subkernels

I'm working on an implementation of a multivariate FFT, which is (or at least should be) highly parallelizable due to the row-column-algorithm. However, i can't figure out how to implement that. The ...
64
votes
8answers
13k 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): ...
6
votes
1answer
155 views

Efficient conditional division

I was curious about the following little problem. Suppose $n \in \mathbb{N}$ is some large number and $p$ is a (possibly large) prime, and I want to update $n$ according to $$n := \begin{cases} n/p, ...
17
votes
6answers
743 views

Split a string at specific positions

Given a string of alphanumerical characters, how to split it simply and quickly at the center of continuous letter-substrings? Is there an elegant and fast solutions out there in the "computational ...
10
votes
2answers
269 views

Update only one element in a dynamic grid?

I've got a grid of buttons that each display an image from an array. When a button is clicked, its image changes. ...
33
votes
1answer
1k views

Transferring a large amount of data in parallel calculations

This question is inspired by one of @whuber's answers Consider the following code: ...
2
votes
1answer
549 views

Speeding up a slow indefinite integral

I have found an alternative way to express an old solution: ...
11
votes
3answers
465 views

Plot on command within DynamicModule?

Suppose I have a DynamicModule with all sorts of controls to set some parameters for a graphics object. ...
12
votes
1answer
469 views

Making a “cached” version of `Manipulate[]`?

I have a function that is a bit too slow for Manipulate[]. Right now I plan to cache outputs outside the manipulation and just have the manipulator select among ...
7
votes
2answers
690 views

Working with large data, pass-by-reference, compiled functions

I think I know the answer to this, but after two days of Googling, this is my last effort before going down an unwanted path... I have a function that takes lots of large data as arguments as well as ...
8
votes
2answers
720 views

Mathematica rule substitution memory

First I present the specific programming question, and then I provide further background details. Given an expression in Mathematica, say f=3x+7y, there are two ...
12
votes
1answer
321 views

Are there rules of thumb for knowing when RandomVariate is more efficient than RandomReal?

Please consider the following: From a fresh Mathematica kernel, RandomVariate is more efficient for NormalDistribution but ...
6
votes
2answers
491 views

Very long Refine/Solve batch run - is my code broken, or just complicated?

So I'm trying to run some Mathematica code in batch mode from my university cluster. Specifically, I'm trying to find the equilibria of a system of ordinary differential equations. Inspired by the ...
2
votes
1answer
262 views

AbsoluteTiming affected by surrounding code

In the process of simplifying code posted for another question, I noticed AbsoluteTiming returned different results for the same code being timed, ...
12
votes
3answers
482 views

Considerations when determining efficiency of Mathematica code

I have two segments of code that do the same thing and I want to determine the which is more efficient. What are the considerations when determining efficiency of Mathematica code? ...
10
votes
3answers
239 views

Efficient way to turn a subset into a permutation

For an input vector $\{a_1,a_2,\ldots,a_n,b_{1},b_2,\ldots,b_{m}\}$ and a list of ordered positions $\{k_1,k_2,\ldots, k_n\}$, such that $1\leqslant k_1 < k_2 < \ldots < k_n \leqslant ...
15
votes
2answers
709 views

Efficiently collecting results inside a compiled function

When we don't know the number of results that will be generated, the usual way to collect results is Reap/Sow. Another ...
46
votes
6answers
8k views
12
votes
3answers
814 views

Function does not compile with Greater in it

I have to speed up a matrix-calculation and would like to use Compile for it, though it fails and produces an unknown error message: ...
13
votes
6answers
855 views

Efficient conditional Mean[] on a large data set

Consider a list, AK6, that has 382 sublists of length varying from 2500 to 3000. Each "subsublist" is as such : ...
4
votes
2answers
196 views

Setting a lower limit on calculation time

Sometimes it can come in handy to set a lower limit for a calculation. Consider for example an animation that is generated by some infinite loop, which would run way too fast to display properly for ...
1
vote
1answer
440 views

How can I speed up image importing? [closed]

The following examples show how slow it is to load a simple GIF. Can anything be done to make it faster? System #1: Mathematica 8.0.1.0, HP notebook, Win7 64Bit, 2 x 2.4Ghz Intel Core i5, 2GB RAM, w/ ...
26
votes
1answer
597 views

How are MemberQ and FreeQ so fast?

I would like to understand the implementation that allows MemberQ and FreeQ to be as fast as they are. I noticed this thanks to ...
24
votes
2answers
565 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 ...
7
votes
3answers
355 views

Making sure that Timing[] isn't thrown off by caching

In playing with and comparing the various answers to my question here, I tried applying Timing[] to see how fast things ran, but it seemed that some of the answers ...
23
votes
6answers
1k views

efficient way to count the number of zeros at the (right) end of a very large number

If I want to count the number of zeros at the (right) end of a large number, like $12345!$, I can use something like: Length[Last[Split[IntegerDigits[12345!]]]] ...
12
votes
3answers
279 views

Memoization of Rounded inputs

Memoization is a technique for improving performance by having a function remember its previous arguments. For example, f[x_]:=f[x]=mySlowFunction[x] will be ...
27
votes
4answers
2k 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 ...
4
votes
1answer
814 views

MaxSteps and Computing time issue for Solving Differential equation in Mathematica

When we solve differential equation numerically using NDSolve then sometimes we get error like NDSolve::mxst: Maximum steps reached According to Mathematica docs ...
43
votes
2answers
2k views

Internal`Bag inside Compile

Since Internal`Bag, Internal`StuffBag and Internal`BagPart can be compiled down, it is a ...
10
votes
3answers
508 views

How can I speed up SQL queries in Mathematica?

I have an application that queries a MySQL database many times for data. At the moment, Mathematica and MySQL are running on the same box and yet it takes over 1.6 seconds for the query to run and ...
12
votes
3answers
439 views

With versus Function

I've seen people compare Block with Module and With as scoping constructs, and try to figure ...
18
votes
4answers
411 views

Function that caches when it returns unevaluated

How would you go about implementing a function that caches not the result but the fact that it didn't match a pattern? So that it doesn't waste time recomputing a complex time-consuming pattern ...
28
votes
2answers
922 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?
18
votes
4answers
1k 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 ...
12
votes
2answers
191 views

How do you determine the optimal autocompilation length on your system

When you pack lists there is an overhead therefore packing a list with, say, 2 elements is likely to cost more than you get back in efficiency. Mathematica has default list lengths for which functions ...
25
votes
2answers
478 views

How can I ensure that I am constructing patterns in the most efficient way possible?

Inspired by this answer, I am interested to know if there are best practices or rules of thumb for constructing patterns, for example for use in function definitions (...