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)

16
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 ...
23
votes
1answer
3k views

Finding long strings of identical digits in transcendental numbers

Introduction Describing the three main streams of present-day mathematical philosophy (formalism, Platonism and intuitionism) in a well-known book, The Emperor's New Mind, R. Penrose says: ...it ...
16
votes
1answer
713 views

Slow import of multigigabyte TIF image stacks

Update 6: After contacting Wolfram Technical Support, I was advised that the frame-to-frame import timing variability can be eliminated by reading in the frame stack from frame to frame with the ...
11
votes
1answer
207 views

How to create internally optimized expression for computing with high WorkingPrecision?

I have large dataset and need to fit rather complicated function on it with different values of one of its parameters (this parameter must be fixed in every fit). I use the ...
12
votes
3answers
265 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
2answers
217 views

How to avoid an expensive subset of a Manipulate computation when dependent variables have not changed?

I have a Manipulate where a portion of the calculation that ends up displayed is dependent on a subset of the Manipulate control ...
14
votes
2answers
413 views

Why does iterating Prime in reverse order require much more time?

Say I would like to display the $10$ greatest primes that are less than $10^5$. I could do the following: ...
12
votes
2answers
179 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 ...
9
votes
4answers
700 views

Passing large list by reference

I have the following problem: I would like to control evaluation of a variable that points to a list. For example, frequently in the code I have functions of the form that are supposed to work on ...
7
votes
5answers
1k views

Something faster than Do loops with AppendTo

I am an "old" programmer used to Fortran and Pascal. I can't get rid of For, Do and While ...
8
votes
3answers
294 views

Faster Alternatives to DateDifference

I need a faster implementation of FractionOfYear and FractionOfMonth, which do the following: Input: A time/date specified by ...
8
votes
4answers
372 views

Plotting the components of a function that returns a list in different colors without redundant evaluations of the function

I have a function f which takes a number as input, and returns a list of numbers (the length of the list is constant). f is hard ...
4
votes
3answers
178 views

How to compile a diagonal array efficiently?

For example, if we use some functions defined prior to Compile, we usually have the main evaluators in the compiled codes, pointing to the definition of the ...
2
votes
1answer
1k views

Methods to speed up numerical NDSolve, NIntegrate,

I am not very used to do numerical simulations on Mathematica. Do you have any ideas how to improve i.e. speed up my code? ...
16
votes
4answers
446 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 ...
14
votes
1answer
281 views

Adding three integer sparse matrices is very slow. Adding only two is fast

Adding more than two sparse matrices in one step in Mathematica 9 is very slow (in fact I couldn't even wait for it to finish). Here's an example. Let's generate a large sparse matrix: ...
3
votes
1answer
376 views

How to do the polynomial stuff over finite fields extensions fast?

This question is raised from the problem of package FiniteFields being very slow (please, see the corresponding question): I have had an evidence that Mathematica ...
62
votes
2answers
678 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 in version 7, the performance of a / b and a - b is not ...
30
votes
3answers
1k views

xkcdConvert routines perform slower in Mathematica 9

I am experiencing some rather large performance decreases in Mathematica version 9.0 using the xkcd-styled plotting routines. I had used the xkcdConvert code ...
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 ...
19
votes
6answers
1k views

How to improve the performance of solutions to Project Euler (#39)?

This is the problem 39 of Project Euler, which I asked in the chat room two days ago. My original code runs as slowly as snails, and finally I got two answers from JM and Rojo. Unfortunately, both of ...
15
votes
3answers
585 views

Select performance

I am fairly new to mathematica and working my way thru Paul Wellins book on Mathematica programming so experimenting with various language constructs. I was experimenting with ...
13
votes
0answers
272 views

What Mathematica features and idioms have n^2 complexity or worse

Here are a few: AppendTo[data, elem], Sort[data, test], in some cases use of ___ (as ...
12
votes
1answer
285 views

Removing calls to MainEvalute when using inlined compiled closures

This question is tightly related to the answer Shaving the last 50 ms off NMinimize. There @OleksandR shows how inlined closures can be used to eliminate calls to ...
20
votes
4answers
694 views

Efficient circular buffer?

I wish to create an efficient circular buffer. That is, I wish to keep a fixed length list while appending a new value and dropping the oldest, repeatedly. As we know lists in Mathematica are ...
18
votes
3answers
2k views

Data fitting with Image processing feature detection

I have some 2D data that once plotted looks like the following ...
13
votes
3answers
374 views

Quick multiple selections from a list

What is the fastest way to make multiple selections from a list? Compiled methods included. For example, here are two methods for selecting a subset, compared:- ...
11
votes
2answers
540 views

Vlookup function as Excel in Mathematica

How can we create a Vlookup function, similar to what we have in Excel? So if we have a table a and another b, we could add ...
6
votes
3answers
1k views

Simulate MATLAB's meshgrid function

Here is MATLAB's meshgrid. I came up with these implementations in Mathematica: ...
13
votes
2answers
769 views

Speeding up random walk for many particles

I am trying to speed up this code for many particles to take a random walk. I'm not sure why it is so slow for such a simple task. I got a few hints from colleagues to reduce the precision of the ...
17
votes
4answers
372 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 ...
11
votes
4answers
379 views

What's fastest way of defining 10^5 down values?

I want to define isGood[___] = False; isGood[#] = True & /@ list where list is a list of several million integers. ...
9
votes
1answer
592 views

Compiling a recursive formula

My question is related to computing what is called "invariant measure" for a particular well known fractal - the Sierpinski triangle. We have an array m of four two by two matrices, say ...
6
votes
2answers
695 views

Elegant way to handle columns operations

Inspired by this question from @sjdh and by my recurrent use of columns operations in database sets, I was looking for one way to make columns operations more symetric, so I can handle with matrix and ...
4
votes
1answer
194 views

How to speed up calculations with large number of replacement rules?

In my program I have an expression which looks like: ...
18
votes
3answers
427 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. ...
17
votes
6answers
679 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 ...
15
votes
4answers
400 views

How to remove duplicates from set of machine precision 2D points?

I have a set of 2D points with machine precision coordinates. I need to remove all duplicates. Performance is important. This is the most obvious fast solution: ...
11
votes
4answers
531 views

How to reduce the InterpolatingFunction building overhead?

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

FiniteFields package is very slow. Any fast substitute for Mathematica?

I want to compute the inverse of matrix, say with dimensions $100 \times 100$, defined over a large finite field extension such as $GF(2^{120})$. I am using the package FiniteFields, but Mathematica's ...
9
votes
2answers
419 views

Speeding up construction of simple tridiagonal matrix

I have the following code to construct a tridiagonal matrix: ...
5
votes
4answers
161 views

Performance: Collapsing repeated contiguous rows & cols from a matrix

In this answer I needed to remove contiguous zero-valued cols and rows from a matrix, leaving only two of them in place, no matter what the original number was. I made up this code: ...
11
votes
2answers
295 views

Memory leak in FE?

A very abridged example of what was originally a major leak ...
9
votes
2answers
258 views

Why is ParallelMap way slower than Map

I recently tried to use ParallelMap instead of Map and to my surprise encountered that ...
5
votes
2answers
320 views

Variant of the cutting-stock problem in Mathematica

I'm pretty new to Mathematica and am trying to learn to solve problems in a functional way. The problem I was solving was to list the ways in which I could sum elements from a list (with repetitions), ...
11
votes
1answer
578 views

What is the fastest way to find an integer-valued row echelon form for a matrix with integer entries?

Let me begin by saying that this is my first post on StackExchange. I apologize in advance if I unwittingly break any of its unwritten rules of etiquette. Recently, I've been trying to understand an ...
11
votes
1answer
281 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 ...
8
votes
2answers
603 views

Why is Mathematica's expansion of expressions with square roots so slow?

Mathematica expansion of expressions with symbols isn't slow, but with square roots it is very slow. In Maple or Mupad both are fast: with square roots they both take less than a second. Here is an ...
4
votes
2answers
187 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 ...
4
votes
1answer
671 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 ...