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

learn more… | top users | synonyms

23
votes
4answers
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 ...
20
votes
2answers
852 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: ...
14
votes
2answers
501 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 ...
11
votes
3answers
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 ...
10
votes
2answers
403 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 ...
10
votes
1answer
551 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 ...
9
votes
5answers
407 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 ...
8
votes
2answers
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 ...
8
votes
1answer
150 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}]; ...
8
votes
4answers
278 views

NIntegrate extremely piecewised functions

I often need to integrate extremely piecewised functions, like the following one (not extreme, but gives an idea): ...
8
votes
1answer
357 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, ...
8
votes
3answers
347 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 ...
7
votes
4answers
268 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 ...
7
votes
1answer
94 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 ...
7
votes
3answers
357 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 ...
7
votes
3answers
228 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. ...
6
votes
2answers
271 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 ...
5
votes
2answers
137 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 ...
5
votes
2answers
169 views

How to optimize computing a product?

Problem description Write a function to caculate the formula My solutions Solution 1: ...
5
votes
1answer
327 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 ...
5
votes
1answer
239 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 ...
5
votes
2answers
181 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: ...
5
votes
1answer
97 views

Why LinearModelFit is slower than NonlinearModelFit?

Why LinearModelFit is slower than NonlinearModelFit? ...
5
votes
0answers
81 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 ...
4
votes
3answers
455 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: ...
4
votes
1answer
192 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 ...
4
votes
4answers
291 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 ...
4
votes
1answer
113 views

Strange phenomenon with Outer

...
4
votes
1answer
97 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] ...
4
votes
2answers
178 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 ...
4
votes
1answer
128 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. ...
3
votes
5answers
304 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$ ...
3
votes
2answers
255 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: ...
3
votes
2answers
163 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 ...
3
votes
1answer
128 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 ...
3
votes
1answer
895 views

Improving NDSolve speed for heavily stiff problems

Having looked around the intergoogles and Mathematica.SE, I thought I'd pose a question with a minimum working example. Here is the situation I am trying to improve: I am solving a 4th order non ...
2
votes
1answer
111 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 ...
2
votes
1answer
149 views

EmpiricalDistribution for Gini Coefficient Runs Slow

I am repurposing code that I found in this blog post (http://datavoreconsulting.com/sports/gini-coefficients-and-the-olympic-medal-race/) to generate the Gini coefficient for other data sets I am ...
2
votes
1answer
195 views

Why is LogLogPlot[] slower than Plot[]?

I notice that LogLogPlot runs quite bit more slowly than ordinary Plot. For example, consider the following two blocks of code ...
2
votes
2answers
163 views

Time differences between code using user-defined probability distribution and code using built-in distribution

Why is the reponse time of of kernel significantly different when I use the first code rather than the second? Shouldn it not be the same? Is there any way that the response time can be made smaller ...
2
votes
0answers
156 views

Taking real and imaginary parts of indexed functions and speeding up ComplexExpand

I am setting up a large system of ODEs and in order to use the IDA method (which is sig. faster for my system and thus attractive), I must split my equations into real and imaginary parts. I am ...
2
votes
0answers
160 views

Project Euler (memory and speed issues)

Let n be a positive integer. An integer triple (a, b, c) is called a factorisation triple of n if: 1 ≤ a ≤ b ≤ c a·b·c = n. Define f(n) to be a + b + c for the factorisation triple (a, b, c) of n ...
1
vote
2answers
117 views

How can I speed up this plot manipulation? Why does Evaluate not help?

If I were able to "Save" the computed inverse function somewhere and then plot it, the manipulation would become faster. As I understand, Evaluate should do the job, but for some reason it does not. ...
1
vote
2answers
166 views

What's the best way to generate all the upper triangular matrix whose singular values are given?

For example, given $\lambda_1 = 1, \lambda_2 = 2, \lambda_3 = 3$, what's the best way to generate all the upper triangular matrix ($3\times 3$) whose singular values are $\lambda_i$? Note:Given a ...
1
vote
2answers
118 views

Speeding up a simple simulation with random extractions

I wonder if anybody can help, I need a hand with a simple simulation I am writing with Mathematica (I am using version 8). Basically it creates a list (called l) with numbers from 1 to n and a random ...
1
vote
1answer
104 views

What is the best way to produce a symmetric semi-definite matrix using as few variables as possible?

As we know, there are only $\frac{n(n+1)}{2}$ variables in a symmetric $n$-dimensional semi-definite matrix. Is it possible to produce a $n$-dimensional semi-definite matrix whose trace is $1$ using ...
1
vote
1answer
236 views

Speeding up code by avoiding repeated evaluation of a function

I want to speed up my code and I have two ideas, but I don't know how I can implement them. Here is a little part of my code, which I want to improve: ...
1
vote
1answer
85 views

Plotting a sum as a function of its upper bound

Apologies if this is easy to find in the documentation, but is there a quick way of doing the following up to any given 'n'? ...
1
vote
0answers
52 views

Increase optimization processing speed

As you know, sometimes numerically solving of optimization problems takes several minutes. For this reason 2 question came up to me: 1 - When one is abort the optimization procedure, how it is ...