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)

6
votes
2answers
191 views

How can this AbsoluteTime call be made faster?

I need to convert tons of date strings into absolute times. This generates an example data set of 10,000 strings: ...
6
votes
2answers
502 views

Web scraping and multi-threading in Mathematica

I'm working in some web scraping using Mathematica, and today, to speed-up the process, I used bash commands (xargs with parallel options with curl and wget) using ...
6
votes
4answers
213 views

Position of Subsets in a List

I have a particular list of integers and a matrix whose entries are lists. Say I know that my particular list is a subset of at least some of the elements in the matrix. I want to find an efficient ...
6
votes
1answer
285 views

Imposing a Periodic Boundary Condition in Nearest Neighbour Search

I am trying to find first nearest neighbour distributions of randomly dispersed point-like objects in an infinite system. To do this I make a finite sized box unit cell with a chosen concentration of ...
6
votes
3answers
241 views

Riffle and Partition

At some point I noticed that I was using Riffle and Partition together a lot. I would do things like ...
6
votes
2answers
306 views

Memory management and speed for Fast GCD

Let's say that we have some $300\,\text{K}$ digits (arbitrary function) and want to trial factor with $100{,}000{,}000$ first prime numbers. ...
6
votes
2answers
113 views

Integrate perfomance

I have a notebook in Mathematica 4. I'm trying to convert it to use in Mathematica 9. One of the problem is the long computation of definite integral in the new version of Mathematica. Here's the ...
6
votes
2answers
448 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
146 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, ...
6
votes
2answers
103 views

Consecutive Print to target same output Cell

Is it possible to conveniently direct the output of consecutive Print statements to the same output without actually accumulating the output in a temporary string/variable. This would be useful in ...
6
votes
3answers
187 views

Enumerate the 1-factors (perfect matchings) of $K_n$

Introduction I would like to enumerate the 1-factors, or (near-)perfect matchings, of the complete graph Kn. The adjacency list representation for Kn is basically { (x, y) | 1 ≤ x < y ≤ n }. For ...
6
votes
1answer
171 views

Does pass-by-value affect the performance of function calls?

I only have a little coding experience in C, and I remember I was told that pass by reference is more efficient than pass by value since the parameters don't need to be copied. Since there is no pass ...
6
votes
1answer
118 views

Better way to get Fisher Exact?

I want to perform a Pearson's $\chi^2$ test to analyse contingency tables; but because I have small numbers, it is recommended to perform instead what is called a Fisher's Exact Test. This requires ...
6
votes
1answer
565 views

Fastest way to do vector / matrix multiplication with constant matrix

Suppose we have a fixed n x n matrix, call it $B$. What is fastest way to evaluate $v^t B\;v$ over many different vectors $v$? Since $B$ is a constant matrix, does this allow the number of operations ...
6
votes
2answers
494 views

Trying to extract information from an MDB file. Import is too slow

I am running a Linux box and as usual the documentation for this type of box is not great. I am trying to open up and extract and possibly include later on information from a "mdb" file. The file ...
6
votes
1answer
118 views

Optimizing the front end on the Raspberry Pi

Using Mathematica's Front End on the Raspberry Pi is truly a test of one's patience. The most basic of notebooks become unresponsive after seemingly trivial additions to the notebook. Consider the ...
6
votes
2answers
400 views

CorrelationFunction for vectors

Mathematica 9 has a new CorrelationFunction. Sadly the joy does not last long, as I can't get it to work with vectors. I would like to make a velocity correlation ...
6
votes
2answers
171 views

Control which variables get embedded inside Manipulate

While trying to analyse several different datasets I got what I thought was a good idea: define a "function" that returns a Manipulate object. But this has some ...
6
votes
2answers
726 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 ...
6
votes
1answer
98 views

Garbage collection for memoized functions on subkernels

I have a top-level function which operates on some data: findBestIntegers[data_]:=ParallelMap[optimize,data] which makes parallelized calls to an optimization ...
6
votes
0answers
50 views

Issuing user-defined messages takes a long time (Message is slow)

Issuing messages takes a very long time in version 9 and version 10. Can others reproduce this? Is there a fix? Update: This is slow only when using the front end, but not when running the kernel ...
6
votes
0answers
167 views

Performance tuning - Explicitly set Mathematica use Nvidia GPU for rendering

I feel hesitated to ask this question, but I cannot figure out a way to increase the performance. The problem is about running this featured example on a computer with both Intel HD GPU and Nvidia ...
6
votes
0answers
181 views

Poor CUDALink performance, when calling a kernel multiple times

When working on a project of mine I noticed that CUDALink shows very poor performance when calling CUDA kernels multiple times in a loop-construct. To demonstrate this I put together this little ...
6
votes
0answers
76 views

Slow NIntegrate over complicated domains (sometimes)

Overview I have a very large matrix I need to calculate where each element requires the integration of a product of two functions over some complicated domain. I would like to find a set of options ...
6
votes
0answers
95 views

Compiled function crashes kernel when trying to list over different length arguments

I'm trying to speed up a tally/binning operation that I have to run many times. I have many lists of between 1 and 10 numbers from the range [1,5] and I want to ...
6
votes
0answers
432 views

Optimizing NIntegrate for higher PrecisionGoal

By default, NIntegrate works with MachinePrecision and its PrecisionGoal is set to ...
5
votes
4answers
163 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: ...
5
votes
2answers
84 views

ReplaceAll performance problem: packed arrays on the LHS are unpacked when the RHS is too long

The following is one of the most mysterious performance problems I encountered and came up while extending the booleval function mentioned here. I am looking for a ...
5
votes
3answers
511 views

How to solve an eigensystem faster?

I have a module that I need to call 1-10 million times in my program. Currently, it is taking several hours to run so I am hoping that I can cut down some runtime with your help. ...
5
votes
2answers
325 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), ...
5
votes
2answers
353 views

Fast Karatsuba Squaring, how to?

I want to improve timing of squaring for big numbers. So I asked What is the fastest way to split a number into parts and What is the most efficient way to add zeros to end of a number with several ...
5
votes
2answers
125 views

Improving performance in the generation of random (directed acyclic) graphs

I need to generate large (directed, acyclic, transitive) graphs. My code in Mathematica gets very slow for large numbers of vertices. I've tried a number of approaches and I can't find any significant ...
5
votes
2answers
258 views

Faster Eigenvalues with lower precision goal

I compute all eigenvalues of a large matrix, and I decide that the speed is more important than the precision. Then the question is, can I speed up Eigenvalues[] by ...
5
votes
2answers
283 views

Is there a Break[] equivalent for short-circuiting in Table?

I am running Mathematica 7. The following is admittedly a silly example, but suppose that I have a list list whose entries are either ...
5
votes
3answers
345 views

Efficiently extracting an array subset given a separate array

I have two arrays. The first array consists of tens of millions of rows and three columns with a string, a number, and a string. ...
5
votes
2answers
170 views

How to optimize computing a product?

Problem description Write a function to caculate the formula My solutions Solution 1: ...
5
votes
1answer
124 views

Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
5
votes
1answer
169 views

MapThread over large graph

I am trying to build a very large graph with approximately $16\,000\,000$ edges, each being a two-dimensional integer vector. I have a list of edges in the form $\{\{e_{11}, e_{12}\}, \{e_{21}, ...
5
votes
1answer
118 views

How to speed up this TDSE code?

I'm trying to build a 1D time-dependent Schrodinger equation solver, using the Crank-Nicholson method. And I think the code is kind of working now, but the speed is still slow for me. Here is the ...
5
votes
2answers
127 views

Reading non-tabular data with NaN-s from file

A NaN stands for an entry that is not a number/undefined/missing/infinite/etc. On my machine the C code printf("%f", 0/0.) produces ...
5
votes
1answer
163 views

How to speed up multi-curve plotting?

The following example is a very simplified version of the problem I'm working with, though I hope it still captures the essentials. First, the definitions: ...
5
votes
1answer
225 views

How to avoid unpacking from Language`ExtendedFullDefinition

Consider this code: LaunchKernels[]; On["Packing"] a = RandomReal[{0., 1.}, {64, 30000}]; ParallelMap[Fourier, a]; // AbsoluteTiming Developer...
5
votes
1answer
226 views

Can we know the background code associated with basic mathematica functions

I wanted to know how computers calculate basic functions like Sqrt so first I checked how humans do it I found that they use something called "Newton's method" that ...
5
votes
1answer
168 views

What is the fastest way to maintain a large set of expressions?

In my algorithm I need to maintain a set (an unordered list of distinct elements) of expressions supporting two operations: Test an expression for membership in the set Adding a new expression to ...
5
votes
1answer
424 views

Montgomery Modular Exponentiation

I'm trying to write a Montgomery exponentiation based on this which can compete with Mathematica PowerMod. We know that PowerMod ...
5
votes
1answer
81 views
5
votes
1answer
375 views

Tridiagonal symmetric matrix eigenvalue using bisection

I know that Eigenvalues is already quite well implemented in Mathematica, nor am I foolishly trying to improve on it. In order to improve my programming skills, I ...
5
votes
2answers
641 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 ...
5
votes
1answer
221 views

More efficient Omnibus number determination?

Amusing myself with concepts from "Omnibus Sequences, Coupon Collection, and Missing Word Counts", in particular the sequences. To paraphrase the concept, a string is k-Omni over some alphabet a if ...
5
votes
1answer
96 views

Finer granularity with ParallelDo

ParallelDo only parallelizes by the innermost variable. Is it possible to distribute the calculation over all the points (all the variable) separately? let me ...