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.
16
votes
6answers
524 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 ...
40
votes
6answers
4k views
7
votes
2answers
156 views
Speed up Fourier for Booleans
I'm calculating a bit of the power spectrum of a two color cellular automaton using:
...
9
votes
4answers
199 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 ...
19
votes
3answers
388 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.
...
14
votes
2answers
478 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 ...
12
votes
2answers
232 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 ...
16
votes
4answers
385 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
386 views
6
votes
2answers
335 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 ...
6
votes
1answer
134 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, ...
9
votes
2answers
168 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.
...
11
votes
3answers
211 views
Plot on command within DynamicModule?
Suppose I have a DynamicModule with all sorts of controls to set some parameters for a graphics object.
...
7
votes
3answers
215 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 ...
8
votes
1answer
262 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 ...
3
votes
1answer
191 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, ...
8
votes
2answers
454 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 ...
11
votes
1answer
225 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 ...
13
votes
3answers
309 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?
...
33
votes
1answer
884 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 ...
12
votes
2answers
415 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 ...
10
votes
3answers
426 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:
...
12
votes
6answers
612 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
158 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
320 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/ ...
17
votes
2answers
314 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 ...
11
votes
3answers
219 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 ...
9
votes
3answers
383 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 ...
11
votes
1answer
179 views
Choosing among different function definitions without sacrificing speed
I'm working on some code that numerically integrates a function, let's call it G, which calls another function, F, that can be ...
31
votes
4answers
1k 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 ...
3
votes
1answer
381 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 ...
6
votes
3answers
284 views
With versus Function
I've seen people compare Block with Module and With as scoping constructs, and try to figure ...
17
votes
1answer
252 views
Is there an NDSolve`ProcessEquations analog for NIntegrate?
NDSolve has an interface for repeatedly solving an equation with different initial conditions without having to analyze the equation and set up the solving algorithm each time. This can improve ...
44
votes
4answers
747 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 ...
20
votes
2answers
303 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 (...

