# Tagged Questions

Questions about optimizing code for faster results, or how to do faster computations.

464 views

### PositionIndex is horribly slow

I do not know how best to formulate this as a question but the new PositionIndex is horribly slow. Using Szabolcs's clever ...
1k views

### What is the fastest way to count square-free words?

Background A word is a string of letters in an alphabet. A square-free word has no adjacent repeating substring. For example, (in the ternary alphabet of {0,1,2}) the words 00, 012121, and 0212012021 ...
915 views

### How to quickly calculate intersections of filled curves?

I am trying to quickly calculate the intersection of polygons with more than 6,000 points. A compiled solution would be preferable. Here is one example of the problem: ...
542 views

### DeleteDuplicatesBy is not performing as I'd hoped. Am I missing something?

Déjà vu: a new-in-v10 function should provide a better solution to an old problem, but my enthusiasm is curbed when I run timings. This time the function is ...
367 views

### Is there a better way to add elements to a Graphics expression repeatedly?

Consider simple example where a set of points is generated and some are displayed. ...
533 views

### How can I speed up the classic GA for graph coloring?

I'm trying to compute the chromatic number of this graph (which is 28): g = Import@"http://www.info.univ-angers.fr/pub/porumbel/graphs/dsjc250.5.col"; My genetic ...
2k views

### Why is this code slow and how could I speed it up?

My goal is given an integer number to deduce if its digits increase (or remain the same) from left to right or not. Example: $1236, 123336$ are both considered numbers with increasing digits, but ...
428 views

### Fast Simulations with Compile

this post relates to another post that I didn't follow up propely. If I wanted to simulate a system of stochastic proesses like the following, and loop over this run many many times would writing the ...
614 views

### Is there a simple example using OpenCLLink that shows performance advantages?

OpenCLLink allows Mathematica to use the OpenCL parallel computing language. I have done some tests on simple examples, but so far, the CPU regularly outperforms ...
109 views

### Exp is slower for some number ranges

I get a hit on performance with the Exp function depending on the range of my inputs. for example, with: ...
492 views

### Fast way of finding position of Interval containing an Integer

I have a large list (about 2.2 million) of pairs of integers. Each pair defines an Interval[{}]. Given some integer x, I need to ...
362 views

### Why is AstronomicalData so slow?

I'm having trouble getting some pretty simple functions using AstronomicalData to perform at anywhere near the speed I need them to. For example, I have many ...
1k views

### Convolution with interpolated function

I have some problems to calculate in reasonable speed the convolution of an interpolated function with a Gauss function. I have here (ExampleData.txt, alternate Pastebin link) data which I ...
161 views

### Why is building a table of function values so much slower than just plotting the function?

Here is a function that take some time to evaluate: n = 1000; coeffs = RandomVariate[NormalDistribution[], n]; f[x_] := Sum[coeffs[[k]] Sin[k x]/k, {k, 1, n}]; ...
328 views

### NIntegrate extremely piecewised functions

I often need to integrate extremely piecewised functions, like the following one (not extreme, but gives an idea): ...
465 views

### Integration strategies for oscillatory multidimensional function

I am seeking to integrate a highly oscillatory, multidimensional function. I am currently using NIntegrate's QuasiMonteCarlo approach. However, this is time-consuming and, given my current resources, ...
322 views

### Speed up ContourPlot3D

I have two issues with the following code. (1) It executes too slowly. If I use "Speed" instead of "Quality" for PerformanceGoal, I get the speed but an undesirable delay in proper rendering of the ...
298 views

### Slow plotting of trigonometric functions

I have two sets of solutions for a and b. I'm plotting solution only if it is positive for both ...
346 views

### Efficiently finding the positions of a large list of targets in another, even larger list

I have a big list. It contains nearly 60,000 sub-lists. It's structured like bigList = {{x1,y1,z1},{x2,y2,z2},{x3,y3,z3},.....,{x60000,y60000,z60000}}; I have ...
98 views

### Why does Rescale performance vary significantly based on the real number values in a list?

I have a list of the form: testList = Table[{Flatten[RandomReal[{-200, 200}, {1, 2}], 1], RandomReal[{-200, 200}, {69, 2}]}, {i, 1, 100}]; And I would like to ...
402 views

### Suppress connection to Wolfram|Alpha when making unit conversions

I want to speed up calculations with unit conversions. I suspect primarily that some units evaluate from Wolfram|Alpha. Any way to prevent this automatically, so that Mathematica throws ...
194 views

### Porting code from MATLAB

I'd like to switch from MATLAB to Mathematica for my research in digital signal processing. I used Mathematica a lot during the '90s but I haven't used it since then, so I might as well be a complete ...
236 views

### How can I efficiently check if a graph is unit distance?

A unit distance graph is a graph that is embeddable in the plane so that every edge has length 1. ...
353 views

### Generating an Ulam spiral

An Ulam Spiral is quite an interesting construction, revealing unexpected features in the distribution of primes. Here is a related topic with one answer by Pinguin Dirk, who has provided one ...
286 views

### Use of Cellular Automaton or Discrete Convolution to calculate Potts Energy of a 2D Matrix

In a project of Monte Carlo Modelling of Grain Growth (Link) one of the most costly subroutines is the calculation of the Potts-Energy of an Monte Carlo Grid cell by comparing the center cell value ...
112 views

### Why LinearModelFit is slower than NonlinearModelFit?

Why LinearModelFit is slower than NonlinearModelFit? ...
93 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 ...
222 views

### Split list of states by run lengths and discontinuous changes in state

I was asked to look into writing some simulation code for a user, where the simulation output (x) would oscillate between three possible states: ...
152 views

### How does the speed of WriteString compare to other languages?

I'm doing something where the most sensible approach seems to be to open a file and write to it as I process data from some large tables. Old school. While testing the program, I remembered the low ...
149 views

### how to improve efficiency when I want to classify a list according to certain laws

I have a function that generates lists: f[n_] := Join @@ Permutations /@ PadLeft@IntegerPartitions[n]; For example f[3] ...
169 views

### How to optimize computing a product?

Problem description Write a function to caculate the formula My solutions Solution 1: ...
402 views

### symbolic integration of a definite integral, how to speed up

I am wondering what are the general tips & hints for speeding up symbolic integration. As far as I understand, it solely depends on the form a function is represented before it is processed by ...
191 views

### Parallelizing or optimizing a script for calculating the size of all pairwise intersections between collections of sets

I need to speed up or parallelize an operation that looks essentially like this: ...
292 views

### Random number generation with specific distribution

I am writing a program for solving the shortest path in travelling salesman problem, with a twist that there are multiple salesmen who partition the cities among themselves, thus creating two part ...
114 views

### Why is AllTrue much slower than VectorQ?

This seems to be a theme with V10, a new function dedicated to a specific task doesn't live up to expectation performance-wise. Mr. Wizard has already uncovered 2 such functions here and here. So how ...
84 views

### Is there something like MaxProcessorUsed?

For standard procedures it is easy to test if the implementation is better or worse than other by using Timing etc. It does not help us with creating ...
317 views

### How to generalize and speed up this program?

There is a vector whose length is $n*m$, we can part it into $n$ sections in order. I want to define a function to compare the $i$-th and $i+1$-th section and find the minimum. The program for $n=m=3$ ...
487 views

### Why is my solution to PE #5 so slow

I'm new to Mathematica and it was suggested to me to go through the Project Euler problems in order to learn it. However, I can't quite figure out why my solution to #5 is so slow. The problem: ...
243 views

### Why is parallel slower?

I always assumed that distributing a computation is faster, but it isn't necessarily true. When I do Sum[i,{i,10^4}] I retrieve an answer much faster then if I do ...
328 views

### Remove linear trend from data

I have a large data set with 150k lines and two data columns. I noticed that there is a linear trend, which I want to remove. So, I do the following: first fit with a linear model ...
287 views

### How to speed up drawing a tangent plane?

I have this code to produce an interactive visualization of a tangent plane to a function: ...
126 views

...
209 views

### Quickly reducing the number of decimal digits for a set of real numbers

How can I quickly convert a number with $n$ decimal points to a number of with $m$ decimal points? Round works, however, it is slower than I would like. This ...
221 views

### Bresenham's line algorithm

Bresenham's line algorithm is producing discretized line for given two points for purpose of plotting for example. Like that: I have to stress that I'm interested in positions, not a plot. ...
144 views

### Problems extending NelderMeadMinimize into 2D

I've been playing around with Oleksandr's compiled Nelder-Mead Minimization function presented here: Shaving the last 50 ms off NMinimize First, I'll create some sample data. This creates a noisy ...
142 views

### How could I improve this Central Limit Theorem demonstration speed- and content-wise?

My purpose is to demonstrate the Central Limit theorem by assuming two custom distributions, taking some random samples, calculating their means and ploting the results. Ideally, the histograms should ...
252 views

### How to efficiently drag points on a curve within a Manipulate

My intention is to demonstrate on any conic various metrics properties on points belonging to the conic and connected by lines. I want the points to be dynamically moved not the usual way, ...
147 views

### Surface area of intersecting spheres

Given a sphere of radius 1 centered at the origin and $n$ spheres with radii $r_i$ centered at predefined coordinates, $c_i$, in space, I am after the surface area of the unit sphere that is not ...
192 views

### DynamicModule/Dynamic CheckboxBar Slow for Large Lists

I am trying to create a dynamic construct that takes a large list of expressions and allows the user to interactively select any number of entries for further manipulation. This is what I have ...
140 views

### Any way to speed up evaluation in an Animate?

I have a self-defined function which is fairly complicated and gives the analytical form. The result contains two independent variables. I use Animate to plot the ...