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)

65
votes
1answer
2k 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 ...
42
votes
2answers
2k views

Shaving the last 50 ms off NMinimize

The background: I have to fit an objective function for ~10 000 datasets in near real time (with near real time being on the order of about 10 seconds). I compile the huge objective function, so that ...
40
votes
6answers
4k views
32
votes
2answers
9k views

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

This question is inspired by a Stack Overflow question that I decided to solve using Mathematica. In addition to Mathematica, I thought I'd use some of the new version's graph-related functionality, ...
32
votes
2answers
981 views

Internal`Bag inside Compile

Since Internal`Bag, Internal`StuffBag and Internal`BagPart can be compiled down, it is a ...
29
votes
2answers
862 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 ...
16
votes
3answers
613 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: ...
49
votes
8answers
11k 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): ...
20
votes
3answers
843 views

How to read data file quickly?

I have a tab separated value file with 10 million rows each of which has three tab separated values. The first value is a string, the second an integer, and the third another string. How to read ...
1
vote
1answer
318 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/ ...
33
votes
1answer
561 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. How can I do it? How? It would be really nice!! ...
44
votes
4answers
744 views
33
votes
1answer
881 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 ...
33
votes
2answers
854 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 ...
20
votes
2answers
584 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?
22
votes
6answers
773 views

Faster alternatives for DayOfWeek

It has been noticed on several occasions that DayOfWeek function is rather slow when applied to a large list of dates, e.g. in this recent question. What faster ...
20
votes
2answers
302 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 (...
18
votes
1answer
372 views

Transferring a large amount of data in parallel calculations

This question is inspired by one of @whuber's answers Consider the following code: ...
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 : ...
9
votes
1answer
391 views

How to tell mathematica not to resolve stiffness issues

Very often I solve partial differential equations that are nonlinear and could be up to 4th order. In these cases, it is usual for the solution determined by ...
23
votes
2answers
1k 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 ...
13
votes
4answers
485 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
3answers
578 views

How to speed up the function DelaunayTriangulation?

First define a function meshGrid to generate some points: ...
7
votes
3answers
397 views

PolarPlot render oddities

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 ...
30
votes
9answers
1k 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 ...
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 ...
14
votes
4answers
878 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 ...
12
votes
3answers
492 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 ...
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 ...
8
votes
3answers
236 views

Faster Alternatives to DateDifference

I need a faster implementation of FractionOfYear and FractionOfMonth, which do the following: Input: A time/date specified by ...
16
votes
4answers
384 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 ...
12
votes
1answer
222 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 ...
3
votes
1answer
474 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? ...
21
votes
1answer
340 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 ...
19
votes
6answers
953 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 ...
26
votes
4answers
1k 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 ...
20
votes
2answers
272 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 ...
17
votes
1answer
343 views

How can I effectively performance-profile Dynamics?

How to optimize performance-wise a complex dynamic expression (like a GUI) with lots of dynamically updated components, which require different update-frequencies? I was hesitant to ask this question ...
25
votes
3answers
909 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 ...
17
votes
3answers
347 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 ...
10
votes
1answer
177 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 ...
16
votes
3answers
845 views

Data fitting with Image processing feature detection

I have some 2D data that once plotted looks like the following ...
15
votes
4answers
271 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 ...
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? ...
19
votes
3answers
387 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. ...
9
votes
2answers
264 views

Speeding up construction of simple tridiagonal matrix

I have the following code to construct a tridiagonal matrix: ...
8
votes
4answers
213 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 ...
7
votes
1answer
409 views

How to speed up Minimum Spanning Tree algorithm?

How to speed up MST algorithm? I work with graphs and I have adopted Prim's algorithm for my needs, from here. The graphs which I use are very dense (number of vertexes: $V=500$ and every vertex is ...
11
votes
2answers
560 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 ...
11
votes
1answer
331 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 ...

1 2 3